请求重定向,本质是发了两次请求。因为是两次请求,则请求里的属性不会保存。
举个栗子: 一个老板刚刚成立了一家公司,带着所有的公司资料去申请营业执照,可是他走错地方,来到税务局大厅申请营业执照,办公人员说:"您走错地方了,请到工商局大厅办理!"。于是老板拿着退回的资料,又去工商局大厅从新办理。
在上面这个故事里,老板办理营业执照就是发送请求,前后一共发送了两次请求。
实例:
@WebServlet(value="/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
//请求重定向
resp.sendRedirect(req.getContextPath()+"/test");
}
}
@WebServlet(name = "TestServlet",value = "/test",loadOnStartup = 1)
public class TestServlet extends HttpServlet {
public TestServlet(){
System.out.println("执行TestServlet的构造方法...");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username =null;
if(request.getParameter("username")!=null){
username = request.getParameter("username");
}
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
地址输入HelloServlet的访问url. http://localhost:8080/helloservlet/hello?username=zhangsan
控制输出:
null
并且地址栏的url路径也发生了改变,变为:http://localhost:8080/helloservlet/test 这就是说明是发送了两次请求。
请求转发的本质是发送了一次请求,因此请求里的属性可以保存。 举个栗子: 一个老板刚刚成立了一家公司,带着所有的公司资料去申请营业执照,可是他走错地方,来到税务局大厅申请营业执照,办公人员知道他走错地方了,可他并没有告诉老板,而是自己把资料转交给旁边的工商大厅的办公人员,等业务办好后,把资料连同营业执照返回给老板。
在上面这个故事里,老板办理营业执照就是发送请求,前后一共发送了一次请求(在他认为找税务大厅的工作人员成功办理了营业执照)。
实例:
@WebServlet(value="/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
//请求转发
req.getRequestDispatcher("/test").forward(req,resp);
}
}
@WebServlet(name = "TestServlet",value = "/test",loadOnStartup = 1)
public class TestServlet extends HttpServlet {
public TestServlet(){
System.out.println("执行TestServlet的构造方法...");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username =null;
if(request.getParameter("username")!=null){
username = request.getParameter("username");
}
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
地址输入HelloServlet的访问url. http://localhost:8080/helloservlet/hello?username=zhangsan
控制输出:
zhangsan
并且地址栏的url路径也没有发生了改变。这就是说明是发送了一次请求。

| 对比项 | forward | redirect |
|---|---|---|
| 浏览器地址栏 | 不会变化 | 会变化 |
| Request | 同一个请求 | 两次请求 |
| API | Request 对象 | Response 对象 |
| 位置 | 服务器内部完成 | 浏览器完成 |
| WEB-INF | 可以访问 | 不能访问 |
| 共享请求域数据 | 可以共享 | 不可以共享 |
| 目标资源 | 必须是当前 Web 应用中的资源 | 不局限于当前 Web 应用 |