服务器内部请求的转发
这里请求传递所使用的方法:
// 通过request对象获取请求转发器对象
RequestDispatcher getRequestDispatcher(String path)
// 使用RequestDispatcher对象来进行转发
forward(ServletRequest request, ServletResponse response)
实例引入
-
创建一个Web项目:RequestDelivery
-
创建两个类,在这两个类之间进行请求的传递:
-
RequestDemo01.java
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/demo01") public class RequestDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo01被访问了!"); // 获取请求发送对象 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/demo02"); // 发送请求 requestDispatcher.forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
-
RequestDemo02.java
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/demo02") public class RequestDemo02 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是demo02,demo01的请求对象已经传递过来了!"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
-
-
启动服务器,访问:http://localhost:8080/RequestDelivery_war_exploded/demo01
控制台输出:
demo01被访问了! 我是demo02,demo01的请求对象已经传递过来了!
页面不会跳转,还在在demo1页面。
特点
- 浏览器地址栏路径不发生变化
- 只能转发到当前服务器内部资源中。不能转发到其他服务器。
- 用户的发送请求是一次,被转发的请求也是用户发送的请求对象。
Request共享数据
-
备注:
- 域对象:一个有作用范围的对象,可以在一定范围内共享数据。
- request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据。
-
方法:
// 存储数据 void setAttribute(String name,Object obj) // 通过键获取值 Object getAttitude(String name) // 通过键移除键值对 void removeAttribute(String name)
实例引入
创建两类:
-
RequestDemo03.java
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/demo03") public class RequestDemo03 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo03被访问了!"); // 获取请求发送对象 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/demo04"); // 发送请求 requestDispatcher.forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
-
RequestDemo04.java
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/demo04") public class RequestDemo04 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是demo04,demo03的请求对象已经传递过来了!"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
存储数据
-
修改
RequestDemo03.java
中的doGet()方法:@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo03被访问了!"); // 存储数据到request域中 request.setAttribute("Set's Message", "Hello, setAttribute!"); // 获取请求发送对象 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/demo04"); // 发送请求 requestDispatcher.forward(request, response); }
-
修改
RequestDemo04.java
中的doGet()方法:@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是demo04,demo03的请求对象已经传递过来了!"); // 获取request域数据 System.out.println(request.getAttribute("Set's Message")); }
-
启动服务器,访问:http://localhost:8080/RequestDelivery_war_exploded/demo03
控制台输出:
demo03被访问了! 我是demo04,demo03的请求对象已经传递过来了! Hello, setAttribute!
参考文献
无