Request forwarding
-
Request forwarding: after a request from the client arrives, it is found that other servlets are needed to realize the function.
- The client browser sends a request to servlet a to realize some functions with the help of servlet a, but servlet a cannot complete this function. At this time, it finds that servlet B can realize these functions, so servlet a forwards a servlet B for this request
-
characteristic:
- The browser address bar remains unchanged: (it is also the address of servlet a)
- The data in the domain object is not lost: (you can set some shared data in servlet a, and servlet B can obtain these data)
- The response body of the Servlet responsible for forwarding will be lost before and after forwarding: (because ServletB finally responds to the client, the body of ServletA will be lost)
- The forwarding destination responds to the client
-
Implementation method
-
code implementation
Forwarded Servlet
@WebServlet("/qqzf1") public class qqzfDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Set up shared data req.setAttribute("encoding","gbk"); //Get request scheduling object RequestDispatcher rd = req.getRequestDispatcher("/qqzf2"); //Realize forwarding function rd.forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
Actually executed Servlet
@WebServlet("/qqzf2") public class qqzfDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Get shared data Object encoding = req.getAttribute("encoding"); System.out.println(encoding); System.out.println("qqzf2 Yes"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } } /* gbk qqzf2 Yes */
Request contains
-
The request includes: functions in other servlets can be combined to respond to the client.
- The client browser sends a request and finds that ServletA can implement some functions, but ServletA can only complete some functions, and the other part cannot. Replacing ServletA, it finds that ServletB can implement other functions, so ServletA includes ServletB at this time. It can combine the functions of other servlets in Servlet A and respond to the client together
-
characteristic:
- The browser address bar remains unchanged (or the address of servlet a)
- The data in the domain object is not lost (some shared data can be set in servlet a, and servlet B can obtain these data)
- The contained Servlet response header will be lost (the disappearance of Servlet b)
-
Implementation method
-
Code test
@WebServlet("/qqzf1") public class qqzfDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Set up shared data req.setAttribute("encoding","gbk"); //Get request scheduling object RequestDispatcher rd = req.getRequestDispatcher("/qqzf2"); //Realize forwarding function rd.include(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
@WebServlet("/qqzf2") public class qqzfDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Get shared data Object encoding = req.getAttribute("encoding"); System.out.println(encoding); System.out.println("qqzf2 Yes"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } } /* gbk qqzf2 Yes */
request redirections
- Request redirection: after a request from the client arrives, it is found that other servlets are needed to realize the function.
- characteristic
- 1. The browser address bar will change,
- 2. Data cannot be shared in the two request domain objects
- 3. You can redirect to other servers.
- Redirection is two requests
- characteristic
@WebServlet("/xiangy1") public class xiangyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("xiangy1 Yes"); ///Set redirection //This is the input address of the browser, which is what I want to access reqduix and input in the browser address bar resp.sendRedirect(req.getContextPath()+"/reqduix"); resp.sendRedirect("https:.//www.baidu.com "); / / if you visit this Servlet, redirect to Baidu's page } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
How to choose redirection or request forwarding
Do you need to share data when viewing jump resources? If necessary, you can use request forwarding. You can use redirection if you don't need it