JavaEE Request Forwarding and Response Redirection

Posted by ale_jrb on Tue, 14 Dec 2021 18:44:44 +0100

Request Forwarding

forword process:
  1. Empty the buffer where Response stores response body data.
  2. If the target resource is SServlet or JP, their service() method is called to send the result of the response generated by the method to the client. If a static HTML document is in the target resource file system, the data in the document is read and sent to the client.
forword processing features:
  1. Since the forword() method first empties the buffer used to store the response body, the response results generated by the source Servlet are not sent to the client, only those generated by the target resource are sent to the client.
  2. If the source Servlet has submitted a response node (flushBuffer(),close() method) before forwarding the request, the forward() method throws an IllegalStateException. To avoid this exception, the response results should not be submitted in the source Servlet.
include process:
  1. If the target resource is a Servlet or JSP, their corresponding service() method is called to add the response body generated by the method to the response result of the source Servlet; If the target component is an HTML document, the content of the document is added directly to the response result of the source Servlet.
  2. Return to the service method of the source Servlet and proceed with the subsequent code block.
include processing features

include has the following features compared to forward forwarding:

  1. The output data of both the source Servlet and the contained target resource are added to the response result.
  2. Modifications to the response status code or response header in the target resource are ignored.
Case:

servlet1 request forwarded to servlet2

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;

@WebServlet(urlPatterns = "/servlet1.do")
public class Servlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet1 service invoked");
        String money = req.getParameter("money");
        System.out.println("money:"+money);
        // Set response type and encoding (include mode)
      /*  resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");*/
        // Increase response content
        //resp.getWriter().println("servlet1 adds response content before forwarding");
        // Request forwarded to another component
        // Get a Request Forwarder
        //RequestDispatcher requestDispatcher = req.getRequestDispatcher("servlet2.do");
        //RequestDispatcher requestDispatcher = req.getRequestDispatcher("aaa.html");
        //RequestDispatcher requestDispatcher = req.getRequestDispatcher("index.jsp");
        //RequestDispatcher requestDispatcher = req.getRequestDispatcher("WEB-INF/bbb.html");
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("https://www.baidu.com");
        // Forwarding action by request forwarder
        requestDispatcher.forward(req,resp);// Managed to target resources (forward more)
        //requestDispatcher.include(req,resp); // Let target resources do some work
        // Continue to increase response information (include mode)
        //resp.getWriter().println("servlet1 adds response content after forwarding");
        // In forward forwarding mode, requests should be handled entirely by the target resource. We do not respond to any requests in the source component
        // After the forward method call, do nothing else with the req and resp objects
        // In include forwarding mode, set the response either before or after forwarding
    }
}
Request Forwarding Features:
  1. Request forwarding is the behavior of a server and screens browsers
  2. The browser's address bar will not change
  3. Requested parameters can be passed from the source component to the target component
  4. Request and response objects are not recreated but passed to the target component
  5. Request forwarding can help us complete page jumps
  6. Request forwarding can be forwarded to WEB-INF
  7. Request forwarding can only be forwarded to internal resources of the current project, not external resources
  8. Common forward, not include

Response redirection

Life Case:

Zhang San borrowed 1,000 yuan from Li Si. Li Si said, "Neither do I. Would you like to go to Wang Wu to see it?" Then, according to Li Si's instructions, Zhang San went to find Wang Wu to borrow money.

Response redirection is achieved through the HttpServletResponse object sendRedirect("path"), which is a way for the server to notify the browser and let it request other resources independently

The process for responding to redirection is as follows:
  1. The user enters a specific URL on the browser side and requests access to a server-side Servlet.
  2. The server-side Servlet returns a response result with a status code of 302, which means that the browser requests access to another Web resource and provides the URL of another Web resource in the response result. Another Web resource may be on the same Web server or may no longer be on the same Web server.
  3. Once the browser receives this response, it automatically requests access to another Web resource.
  4. The browser side receives a response from another Web resource.
Case:
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.io.PrintWriter;

@WebServlet(urlPatterns = "/servlet3.do")
public class Servlet3 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet3 service invoked");
        String money = req.getParameter("money");
        System.out.println("money:"+money);
        // Response redirection
        resp.sendRedirect("servlet4.do?money="+money);
        //resp.sendRedirect("WEB-INF/bbb.html");
        //resp.sendRedirect("https://www.baidu.com");
    }
}
Response redirection summary
  1. A redirection is a browser behavior in which the server redirects the request to the browser and the address bar changes
  2. When redirecting, both request and response objects are generated again, and the parameters in the request are not carried.
  3. Redirecting can also help us complete page jumps
  4. Redirecting does not help us access resources in WEB-INF
  5. Redirection can be directed to external resources

Topics: Java JavaEE server JavaSE