The difference between the forward and redirect of Common face questions in Java

Source: Internet
Author: User

Tag: Rip tells return to specified background to avoid calling processing client

Original source: Ahvari

Forward and redirect represent two kinds of request forwarding methods: direct forwarding and indirect forwarding.

Direct Forwarding (Forward), the client and the browser only make one request, Servlet, HTML, JSP or other information resource, the second information resource response should request, in Request object requests, the saved object is shared for each information resource.

The Indirect forwarding method (Redirect) is actually two HTTP requests, the server side in response to the first request, the browser to another URL to make a request, so as to achieve the purpose of forwarding.

To cite a popular example:

Direct forwarding is equivalent to: "A looking for B to borrow money, B said no, B to find C borrow, borrowed to borrow will send the message to a";

indirect forwarding is equivalent to: "A looking for B to borrow money, B said no, let a to find C lend".

Here is a detailed explanation of the two principles:

One: Indirect request forwarding (Redirect)

Indirect forwarding, sometimes called redirection, is typically used to avoid non-normal access by users. For example, if a user accesses a background resource without logging in, the servlet can redirect the HTTP request to the login page, allowing the user to log in and then access it later. In the servlet, by calling the Sendredirect () method of the response object to tell the browser to redirect access to the specified URL, the sample code is as follows:

Methods for handling GET requests in//servlet public void doget (HttpServletRequest request,httpservletresponse response) {//Request redirected to another resource    response.sendredirect ("URL of the Resource");} ........

The process for indirectly forwarding requests as shown is as follows:

    • The browser makes an access request to the SERVLET1;
    • Servlet1 calls the Sendredirect () method to redirect the browser to Servlet2;
    • The browser makes a request to the Servlet2;
    • Eventually the Servlet2 responds.
Two: Direct request forwarding (Forward)

Direct forwarding method with more, generally speaking of the request forwarding refers to the direct forwarding method. Most Web applications will have a controller. The controller to control the request should be forwarded to that information resource. This information resource is then processed by the request, and it may be forwarded to another information resource to return to the user after processing, which is the classic MVC pattern.

The Javax.serlvet.RequestDispatcher interface is the interface that the request forwarder must implement, and the Web container provides the servlet with the object that implements the interface, and by invoking the forward () method of the interface to reach the purpose of the request forwarding, the sample code is as follows:

......    Method of processing GET request in servlet public void doget (HttpServletRequest request, httpservletresponse response) {     //Get Request Forwarder object, The forwarder's pointer is set through the parameters of Getrequestdispatcher ()   requestdispatcher requestdispatcher =request.getrequestdispatcher (" URL of the resource ");    Call the Forward () method to forward the request         Requestdispatcher.forward (request,response);    } ......

The process for directly forwarding the request is as follows:

    • The browser makes an access request to the SERVLET1;
    • Servlet1 calls the forward () method and forwards the request to the Servlet2 on the server side;
    • Eventually the Servlet2 responds.

tip : In fact, through the browser can observe the server-side use of the kind of request forwarding, when clicked on a hyperlink, the browser's address bar will appear the current request address, if the server-side response is completed, found that the address bar address changed, it is proved that the indirect request forwarding. Conversely, if the address does not change, it represents a direct request forward or no forwarding.

Q: What are the principles and differences between direct forwarding and indirect forwarding?

A: Forward and redirect represent two kinds of request forwarding methods: direct forwarding and indirect forwarding. Corresponding to the code, respectively, is the forward () method of the RequestDispatcher class and The Sendredirect () method of the HttpServletRequest class.

For the indirect way, the server side in response to the first request, the browser to another URL to make a request, so as to achieve the purpose of forwarding. It is essentially two HTTP requests, corresponding to two request objects.

For the direct way, the client browser only makes one request, and the servlet forwards the request to the servlet, HTML, JSP, or other information resource, and the 2nd information resource should respond to the request, and two information resources share the same request object.

The difference between the forward and redirect of Common face questions in Java

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.