请求转发与重定向的区别和执行流程
文章目录
- 请求转发与重定向的区别和执行流程
- 1 请求转发与重定向区别
- 2 现实生活的例子
- 2.1 重定向
- 2.2 请求转发
- 3 重定向-流程图、代码、结果
- 3.1 流程图
- 3.2 代码BoyServlet
- 3.3 代码GirlServlet
- 3.4 web.xml文件
- 3.5 结果
- 4 请求转发-流程图、代码、结果
- 4.1 请求转发-流程图
- 4.2 代码BoyServlet
- 4.3 代码GirlServlet
- 4.4 web.xml文件
- 4.5 结果
本文通过现实生活的例子引出重定向和请求转发,通过图解的方式讲解了整执行流程,希望可以帮助到大家
1 请求转发与重定向区别
序号 | 问题 | 重定向 | 请求转发 |
---|---|---|---|
1 | 第二次请求谁请求的? | 浏览器 | 服务器 |
2 | 浏览器发送了几次请求? | 2次以上 | 1次 |
3 | servlet可以共享request吗? | 不可以 | 可以 |
4 | 地址栏是否发生改变? | 是 | 不是 |
5 | 浏览器地址栏显示的哪一次访问地址? | 最后一次 | 第一次 |
6 | 可以跳转到什么资源? | 任意资源 | 项目内部 |
7 | 第二次的请求路径是? | 绝对路径 | 内部路径 |
2 现实生活的例子
2.1 重定向
①:A对B说,帮忙拿下快递
②:B对A说,没有空,你找下C吧
③:A对C说,帮忙拿下快递
④:C然后拿了快递给A
2.2 请求转发
①:A对B说,帮忙拿下快递
②:第二步,B没有空,直接找让C帮A拿下快递
③:C拿到了快递并返回给A
3 重定向-流程图、代码、结果
3.1 流程图
3.2 代码BoyServlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class BoyServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 获取请求携带的idString id = request.getParameter("id");System.out.println("A对B说:帮我拿下快递,id号是:" + id);System.out.println("B对A说:我没有空,你找下C");System.out.println("----分割线----");// 重定向方式1:response.setStatus(302);response.setHeader("location", "/redirect/girl");// 重定向方式2:(正常用此方法)// response.sendRedirect("/redirect/girl");}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}
}
3.3 代码GirlServlet
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class GirlServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置浏览器解析的格式,否则浏览器会出现乱码response.setContentType("text/html;charset=utf-8");//获取请求携带的idString id = request.getParameter("id");System.out.println("A对C说,帮我拿下快递,id号是:"+id);System.out.println("拿到快递了");//获取输出流PrintWriter writer = response.getWriter();//将信息返回给浏览器writer.println("拿到快递了");}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}
}
3.4 web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>redirect</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><description></description><display-name>BoyServlet</display-name><servlet-name>BoyServlet</servlet-name><servlet-class>com.redirect.BoyServlet</servlet-class></servlet><servlet-mapping><servlet-name>BoyServlet</servlet-name><url-pattern>/boy</url-pattern></servlet-mapping><servlet><description></description><display-name>GirlServlet</display-name><servlet-name>GirlServlet</servlet-name><servlet-class>com.redirect.GirlServlet</servlet-class></servlet><servlet-mapping><servlet-name>GirlServlet</servlet-name><url-pattern>/girl</url-pattern></servlet-mapping>
</web-app>
3.5 结果
浏览器输入
浏览器返回结果
控制台打印结果
4 请求转发-流程图、代码、结果
4.1 请求转发-流程图
4.2 代码BoyServlet
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class BoyServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 获取请求携带的idString id = request.getParameter("id");System.out.println("A对B说:帮我拿下快递,id号是:" + id);System.out.println("B对A说:好的");System.out.println("----分割线----");//---后面B直接在服务器将请求转发给C---//获取转发对象,后面括号指定了转发的路径RequestDispatcher dispatcher = request.getRequestDispatcher("/girl");//执行转发功能,并将请求和响应一并转发过去dispatcher.forward(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}
}
4.3 代码GirlServlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class GirlServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 设置浏览器解析的格式,否则浏览器会出现乱码response.setContentType("text/html;charset=utf-8");// 获取请求携带的idString id = request.getParameter("id");System.out.println("B对C说:帮A拿下快递,id号是:" + id);System.out.println("拿到快递了");// 获取输出流PrintWriter writer = response.getWriter();// 将信息返回给浏览器writer.println("拿到快递了");}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}
}
4.4 web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>dispatcher</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><description></description><display-name>BoyServlet</display-name><servlet-name>BoyServlet</servlet-name><servlet-class>com.dispatcher.BoyServlet</servlet-class></servlet><servlet-mapping><servlet-name>BoyServlet</servlet-name><url-pattern>/boy</url-pattern></servlet-mapping><servlet><description></description><display-name>GirlServlet</display-name><servlet-name>GirlServlet</servlet-name><servlet-class>com.dispatcher.GirlServlet</servlet-class></servlet><servlet-mapping><servlet-name>GirlServlet</servlet-name><url-pattern>/girl</url-pattern></servlet-mapping>
</web-app>
4.5 结果
浏览器输入
浏览器返回结果
控制台结果