JSP redirection, Request forwarding

Source: Internet
Author: User

Request forwarding: Request.getrequestdispatcher (URL address). Forward (request, response)

Principle:

    1. The client sends the request and SERVLET1 makes the processing.
    2. Servlet1 calls the Sendreadirect () method, forwards the request to the Servlet2 to process the request, serving the client.
    3. The Servlet2 responds to the client browser.

Request forwarding allows requests to be forwarded to other Web Components in the same application. This technique is typically used for the servlet process controller of the Web application control layer, which examines the HTTP request data and forwards the request to the appropriate target component, which executes the specific request processing action and generates the response result.

The Servlet class uses the Javax.servlet.RequestDispatcher.forward () method to forward the HTTP requests it receives. The forwarding target component processes the request and generates a response result, or continues forwarding the request to another component. The ServletRequest and Servletresponse objects that were originally requested are passed to the forwarding target component, which allows the target component to access the entire request context. It is important to note that requests can only be forwarded to components in the same web app, not to components of other Web applications.

In JSP pages, you can use the <jsp:forward> tag to forward requests. Example: <jsp:forward page= "hello.jsp" >

For request forwarding, the forwarded source component and the target component share the shared data within the request scope.

Redirect: Response.sendredirect (URL address)

Principle:

    1. The client sends the request and SERVLET1 makes the processing.
    2. Servlet1 calls the Sendreadirect () method to relocate the client's request to Servlet2.
    3. The client browser accesses Servlet2.
    4. The Servlet2 responds to the client browser.

Web Components can redirect requests to any URL, not just URLs in the same app.

The same HttpServletRequest object is not shared between the redirected source and target components, so shared data within the request scope cannot be shared.

HttpServletResponse's Sendredirect () method returns information containing redirects to the browser, which, based on this information, quickly sends a new HTTP request that requests access to the redirected target component.

Contains

The include relationship allows a Web component to aggregate output data from other Web Components in the same application and use the aggregated data to create response results. This technique is commonly used in template processors, which control the layout of Web pages. The content of each page area in the template comes from a different URL, forming a single page. This technology provides a consistent look and feel for your application. The source and target components that contain the relationship share the same HttpServletRequest object, so they shared the shared data within the request scope.

The difference between forwarding and redirection:

    1. Request Dispatcher.forward () is the control of the container in the steering, in the client browser address bar will not show the post-turn address, the server internal forwarding, request forwarding, the entire process is in the same request.
    2. Response.sendredirect () is a full jump, the browser will get the address of the jump, and resend the request link. This way, you can see the link address after the jump from the address bar of the browser. Not on the same request. Redirect, the client will actually send two requests to the server side.
    3. Forward () is more efficient and uses the Requestdispatcher.forward () method whenever it is possible to meet the needs.
    4. RequestDispatcher is obtained by invoking the Getrequestdispatcher () method of the HttpServletRequest object, which is the method that belongs to the request object.
    5. Sendredirect () is the method of the HttpServletResponse object, the method that responds to the object, and since the method of the response object is called, it indicates that the entire request process is over, and the server begins to return the result of execution to the client.

(1) The Requestdispatcher.forward method can only forward requests to components in the same Web application, and the Httpservletresponse.sendredirect method is not only redirected to other resources in the current application, You can also redirect resources to other applications on the same site, even resources that are redirected to other sites by using an absolute URL. If the relative URL passed to the Httpservletresponse.sendredirect method begins with "/", it is relative to the root of the entire Web site, and if the relative URL specified when the RequestDispatcher object is created starts with "/", it is relative to the root directory of the current Web application.

(2) The URL displayed in the browser's address bar will change when the access process that calls the Httpservletresponse.sendredirect method redirects, changing from the initial URL address to the destination URL of the redirect The browser address bar keeps the initial URL address unchanged after the request forwarding process that calls the Requestdispatcher.forward method ends.

(3) The Httpservletresponse.sendredirect method responds directly to the request of the browser, and the result of the response is to tell the browser to re-issue the request for another URL, a process like a person nicknamed "the browser" who wrote to find Zhang San to borrow money, Zhang San replied that there is no money, let "browser" to find John Doe borrow, and will John Doe now the address of the communication to the "browser." So, "browser" also press Zhang San to provide correspondence address to John Doe letter to borrow money, John Doe received the letter to the money to the "browser". As can be seen, the browser sent out two letters and received two replies, and the browser also knew that the money he borrowed was from John Doe's hand. The Requestdispatcher.forward method forwards the request to another resource on the server side, and the browser only knows that the request has been made and the result has been answered, and does not know that there is a forwarding behavior inside the server program. This process is like nicknamed "Browser" people write to find Zhang San borrow money, Zhang San no money, so Zhang San find John Doe borrowed some money, even can add some of their own money, and then remit the money to the "browser." As can be seen, the "browser" only sent a letter and received a reply, he only knew from Zhang San to borrow money, do not know that a part of the money from John Doe hand.

(4) The caller and the callee of the Requestdispatcher.forward method share the same request object and the response object, which belong to the same access request and response process Whereas the Httpservletresponse.sendredirect () method caller and the callee use their own request object and the response object, which belong to two separate access request and response procedures. For a jump between internal resources of the same Web application, especially before jumping to pre-preprocess the request, and to pass the preprocessing results using the Httpservletrequest.setattribute method, you should use the Requestdispatcher.forward method. Redirection between different Web applications, especially if you are redirecting resources to another Web site, should use the Httpservletresponse.sendredirect method.

(5) Neither the Requestdispatcher.forward method nor the Httpservletresponse.sendredirect method can have the content actually output to the client until they are called. If something is already in the buffer, the content will be purged from the buffer.

JSP redirection, Request forwarding

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.