(12) Differences between the Sendredirect () and forward () methods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.