Request_请求转发和Request_共享数据(域对象)
请求转发:一种再服务器内部的资源跳转方式
- 步骤:
- 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
- 使用RequestDispatcher对象来进行转发:forward(ServletRequest request,ServletResponse response)
- 特点:
- 浏览器地址栏路径没有发生变化
- 只能转发到当前服务器内部资源中
- 转发是一次请求
package com.ailyt.request;
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;
import java.util.Map;
@WebServlet(value = "/r5")
public class RequestStudy5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("r5.......................");
//转发到r9资源
/*RequestDispatcher requestDispatcher = req.getRequestDispatcher("/r6");
requestDispatcher.forward(req,resp);*/
req.getRequestDispatcher("/r6").forward(req,resp);
}
}
package com.ailyt.request;
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(value = "/r6")
public class RequestStudy6 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("r6................");
}
}
共享数据:
- 域对象:一个有作用范围的对象,可以在范围内共享数据
- request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
- 方法:
- setAttribute(String name,Object obj):存储数据
- Object getAttribute(String name):通过键获取值
- removeAttribute(String name):通过键移除键值对
package com.ailyt.request;
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(value = "/r6")
public class RequestStudy6 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据
Object msg = req.getAttribute("msg");
System.out.println(msg);
System.out.println("r6................");
}
}
package com.ailyt.request;
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;
import java.util.Map;
@WebServlet(value = "/r5")
public class RequestStudy5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("r5.......................");
//转发到r9资源
/*RequestDispatcher requestDispatcher = req.getRequestDispatcher("/r6");
requestDispatcher.forward(req,resp);*/
//存储数据到request域
req.setAttribute("msg","hello");
req.getRequestDispatcher("/r6").forward(req,resp);
}
}