时间:2023-02-09 06:27:01 | 来源:建站知识
时间:2023-02-09 06:27:01 来源:建站知识
一.实现重定向1void sendRedirect(String var1) throws IOException;
2.新建一个类,类名为:Redirectservlet。1protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {2 resp.sendRedirect("/Response/img");3}
5.注册url,添加映射:1 <servlet>2 <servlet-name>Redirect</servlet-name>3 <servlet-class>com.gowork.servlet.Redirectservlet</servlet-class>4 </servlet>5 <servlet-mapping>6 <servlet-name>Redirect</servlet-name>7 <url-pattern>/red</url-pattern>8 </servlet-mapping>
6.启动项目,访问localhost:8080/Response/red。1public class requesttext extends HttpServlet {2 @Override3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {4 System.out.println("现在已经进入这个请求了!");5 }6}
如果我们进入过这个重定向页面就会输出:现在已经进入这个请求了。1<servlet>2 <servlet-name>newRedirect</servlet-name>3 <servlet-class>com.gowork.servlet.requesttext</servlet-class>4</servlet>5<servlet-mapping>6 <servlet-name>newRedirect</servlet-name>7 <url-pattern>/loign</url-pattern>8</servlet-mapping>
如果我们访问/rednew,就会向后端的com.gowork.servlet.requesttext发出请求。1<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->2<dependency>3 <groupId>javax.servlet.jsp</groupId>4 <artifactId>jsp-api</artifactId>5 <version>2.2.1-b03</version>6 <scope>provided</scope>7</dependency>
我们进入pom.xml,导入上面的jsp。1String name =req.getParameter("username");2String pass =req.getParameter("password");34System.out.println(name+":"+pass);56resp.sendRedirect("");
重定向要注意路径问题,否则容易404问题。关键词:原理,运用,实战