The Sendredirect () method of the HttpServletResponse interface and the Forward () method of the RequestDispatcher interface can take advantage of additional resources (Servlet, JSP page or HTLM file) to serve the client, but these two methods are fundamentally different.
The working schematic of the SENDREDIRECTT () method and the forward () method are given below, respectively.
How the Sendredirect () method works
The interactive process is as follows:
① browser access to Servlet1.
②servlet1 wants Servlet2 to serve the client.
③servlet1 calls the Sendredirect () method to redirect the client's request to Servlet2.
④ browser access to Servlet2.
The ⑤servlet2 responds to the client's request.
As you can see from the diagram, calling the Sendredirect () method actually tells the browser where Servlet2 is located and lets the browser revisit Servlet2. The Sendredirect () method is called, and the location response header is set in the response.
Note that this process is transparent to the user, and the browser automatically completes the new access. In the address bar of the browser, the URL that is displayed is the URL after redirection.
How the Forward () method works
The interactive process is as follows:
① browser access to Servlet1.
②servlet1 wants Servlet2 to respond to the client's request, it calls the forward () method and forwards the request to Servlet2 for processing.
The ③servlet2 responds to the request.
The interactive process can be seen, call the Forward () method, is transparent to the browser, the browser does not know that the servlet for its service has been replaced by Servlet2, it only knows that a request was made and a response was obtained.
In the browser's address bar, the URL that is displayed is always the URL of the original request.
Another difference between the Sendredirect () method and the Forward () method is that the Sendredirect () method can redirect not only between different Web applications located on the same host, You can also redirect clients to Web application resources on other servers.
(12) Differences between the Sendredirect () and forward () methods