The difference between redirection and forwarding in a JSP: __js

Source: Internet
Author: User
the difference between redirection and forwarding in a JSP:

One: Indirect request Forwarding (Redirect) Two: Direct Request forwarding (Forward)

The user sends an HTTP request to the server that may be processed by multiple information resources before being returned to the user, and each information resource uses the request forwarding mechanism to forward requests to each other, but the user does not feel the request forwarding. Depending on the forwarding method, you can differentiate between direct request forwarding (Forward) and indirect request Forwarding (Redirect), so what are the differences between the two forwarding methods? This article in answer to this question at the same time comprehensively explained two kinds of request to transmit the principle and the difference.

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

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

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

As a popular example:

 Direct forwarding is equivalent to: "A Find B to borrow money, B said no, B to find C borrow, borrow to borrow will pass the message to a";

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

Here is a detailed description of the two principles:

One: Indirect request Forwarding (Redirect) indirect forwarding, sometimes called redirection, which is generally used to avoid the user's irregular access. For example, when a user accesses a background resource without logging on, the servlet can redirect the HTTP request to the login page, allowing the user to log in later to access it. In the servlet, the sample code is as follows by invoking the Sendredirect () method of the response object, telling the browser to redirect access to the specified URL:

......
Method of processing GET request in servlet public
void doget (httpservletrequest request,httpservletresponse response) {
// The request is redirected to another resource
    response.sendredirect ("URL of the Resource");
}
........

The browser sends an access request to Servlet1, Servlet1 invokes the Sendredirect () method, redirects the browser to Servlet2, the browser makes a request to Servlet2, and the Servlet2 responds.

Two: Direct request forwarding (Forward) direct forwarding way with more, generally speaking of the request forwarding refers to the direct forwarding method. Most Web applications will have a single controller. The controller controls that the request should be forwarded to that information resource. Then the information resources processing request, after processing can also be forwarded to another information resources to return to the user, this process 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 an object to implement the interface, and the forward () method that invokes the interface arrives at the request forwarding destination, the sample code reads as follows:

......
    Methods for handling GET requests in servlet public
 void doget (HttpServletRequest request, httpservletresponse response) {
     // Gets the request forwarder object that points to the Getrequestdispatcher () parameter setting
   requestdispatcher RequestDispatcher = Request.getrequestdispatcher ("URL of resource");
    invokes the forward () method, forwarding the request      
   Requestdispatcher.forward (request,response);    
}
......

The browser sends an access request to the SERVLET1, Servlet1 calls the forward () method, sends the request to the Servlet2 on the server, and the Servlet2 responds.

tips: In fact, through the browser can be observed on the server side of the use of that type of request forwarding, when the click of a hyperlink, the browser's address bar will appear the current request address, if the server-side response completed, found the address bar address changed, then prove to be indirect request forwarding. Conversely, if the address is not changed, it represents either a direct request forwarding or no forwarding.

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

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

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

In the direct way, the client browser only makes one request, and the servlet sends the request to the servlet, HTML, JSP, or other information resource, and the 2nd information resource rings the request and two information resources share the same request object.


(i). Redirection and forwarding have an important difference:
1. When forwarding is used, the JSP container uses an internal method to invoke the target page, and the new page continues to process the same request, and the browser will not know about the process. 
2. In contrast, the redirect means that the first page notifies the browser to send a new page request.
because, when you use redirection, the URL displayed in the browser becomes the URL of the new page, and when you use forwarding, the URL stays the same. Redirection
is slower than forwarding because the browser has to issue a new request. Also, because of a new request being redirected, the objects within the request will not be available after a redirect.

(b). How do you choose to redirect or forward? Forwarding is usually faster and can keep objects within the request, so he is the first choice.
However, since the URL in the browser still points to the start page after forwarding, the start page will be called back if the current page is overloaded. If you do not want to see such a situation, select forward. 

(iii). The difference between forwarding and redirection
instead of using the session scope just to pass the variable to the next page, it would increase the scope of the variable for no reason, and forwarding might help you solve the problem.
Redirect: variables stored in previous request are all invalidated and entered a new request scope.
forwarding: The variables stored in previous request will not fail, just like putting two pages together.

Click to open the link

Related Article

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.