Request请求转发和Request_共享数据(域对象)
Request请求转发
请求转发:一种在服务器内部的资源跳转方式
1.步骤:
1.通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2.使用RequestDispatcher对象来进行转发:forward(ServletRequest request,ServletResponse response)
2.特点:
1.浏览器地址栏路径不发生变化
2.只能转发到当前服务器内部资源中。
3.转发是一次请求
RequestD8 :
@WebServlet("/requestD8") public class RequestD8 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("request8被访问...."); //转发到request9 request.getRequestDispatcher("/requestD9").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
RequestD9:
@WebServlet("/requestD9") public class RequestD9 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("request9被访问...."); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
访问request8
Request共享数据(域对象)
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
方法:
1.void setAttribute(String name,Object obj):存储数据
2.object getAttitude(String name):通过键获取值
3.void removeAttribute(String name):通过键移除键值对
Request8
Request9