Request forwarding and request redirection

The main function of servlets is to process client requests and respond to clients

HttpServletResponse is used to encapsulate http response information

HttpServletRequest is used to encapsulate http request information

Request response flowchart

Note: Each servlet will only create one instance object, however, for each http request, the web server will call the service (HttpServletRequest request, HttpServletResponse response) method of the requested servlet instance to recreate a request, response object

 

request redirection

response.sendRedirect(String path); to achieve request redirection

request forwarding

 

request.getRequestDispatcher(String path).forward(request,response);

The forward method can forward the request, and can also forward the page to share data with the forwarded page

the request contains

Request inclusion refers to the use of the include() method to forward the servlet request to other web resources for processing. Different from request forwarding, the request includes the returned response information, including the response message of the current servlet and other web resources. response message

Note: When using include() to request inclusion, the browser's URI address does not change

The difference between request forwarding and request redirection:

1. Redirect two requests and forward one request

2. The redirected address bar will change, but the forwarded address will not change

3. The second request redirected must be sent by GET

4. The resource requested to be forwarded can only be in this application, and the redirection can also be directed to other application resources

5. Request forwarding can use request for data sharing, but redirection cannot

6. Request forwarding is the same as the request for AServlet and BServlet, either all GET or all POST


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324462824&siteId=291194637