一、RequestDispatcher:
解释:getRequestDispatcher ().forward (request.response)这个语句意思是将客户端的请求转向(forward)到getRequestDispatcher()方法中参数定义的页面或者链接。
二、代码解释:
①:附:源码!源码图!
package com.laugh.servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ForwardRequests extends Helloservlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext Context = this.getServletContext(); System.out.println("我进来喽!转发是不会改变访问路径的呦,重定向路径会发生改变!"); //请求转发 //RequestDispatcher requestDispatcher = Context.getRequestDispatcher("/url"); //转发的请求路径 //requestDispatcher.forward(req,resp); //调用forward 实现上面的请求转发 //合并请求转发 getRequestDispatcher的括号里面写 转发的地址 Context.getRequestDispatcher("/url").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
②:web.xml配置 附:源码!源码图!
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--我这个头是新的--> <!--设置web初始化参数--> <context-param> <param-name>url</param-name> <param-value>https://www.cnblogs.com/superyonng</param-value> </context-param> <!--我是提供获得初始化路径的方法 InitServlet--> <servlet> <servlet-name>url</servlet-name> <servlet-class>com.laugh.servlet.InitServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>url</servlet-name> <url-pattern>/url</url-pattern> </servlet-mapping> <!--合并请求转发 ForwardRequests--> <servlet> <servlet-name>mergs</servlet-name> <servlet-class>com.laugh.servlet.ForwardRequests</servlet-class> </servlet> <servlet-mapping> <servlet-name>mergs</servlet-name> <url-pattern>/mergs</url-pattern> </servlet-mapping> </web-app>
③:运行结果:
重点:
重定向:补充
package com.laugh; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class redirectServel extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendRedirect("/FileOutputStream_war/yzm"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
<servlet> <servlet-name>yzm</servlet-name> <servlet-class>com.laugh.YanZhengMa</servlet-class> </servlet> <servlet-mapping> <servlet-name>yzm</servlet-name> <url-pattern>/yzm</url-pattern> </servlet-mapping> <servlet> <servlet-name>redirectServel</servlet-name> <servlet-class>com.laugh.redirectServel</servlet-class> </servlet> <servlet-mapping> <servlet-name>redirectServel</servlet-name> <url-pattern>/red</url-pattern> </servlet-mapping>
第一步:我们所请求的路径
http://localhost:8080/FileOutputStream_war/red
//解释:http://localhost:8080/ 为本地路径;FileOutputStream_war 为项目名称; red 为web.xml配置
第二步:查看路径(当执行red的时候,重定向会给我们改变路径)
http://localhost:8080/FileOutputStream_war/yzm