Difference between forward and sendredirect in JSP-Servlet

Source: Internet
Author: User

Although both the httpservletresponse. sendredirect method and requestdispatcher. Forward method allow the browser to obtain the resources pointed to by another URL, the internal running mechanism of the two is very different.

The following is a summary and comparison of request redirection implemented by the httpservletresponse. sendredirect method and request forwarding implemented by the requestdispatcher. Forward method:
(1) requestdispatcher. the forward method can only forward requests to components in the same web application, while httpservletresponse. the sendredirect method not only redirects to other resources in the current application, but also to resources in other applications on the same site, or even resources that are redirected to other sites using an absolute URL.
If it is passed to httpservletresponse. the relative URL of the sendredirect method starts with "/", which is the root directory relative to the entire web site. If the relative URL specified when the requestdispatcher object is created starts, it is relative to the root directory of the current web application.
(2) Call httpservletresponse. after the access process of sendredirect method redirection ends, the URL displayed in the address bar of the browser changes from the initial URL address to the destination URL of the redirection, and requestdispatcher is called. after the request forwarding process of the forward method is completed, the initial URL address remains unchanged in the browser address bar.
(3) The httpservletresponse. sendredirect method directly responds to the browser's request. The response result is to tell the browser to re-send an access request to another URL. The requestdispatcher. Forward method forwards requests to another resource on the server side. The browser only knows that the request is sent and the response is returned, but does not know that the forwarding behavior occurs in the server program.
(4) requestdispatcher. the callers and callers of the forward method share the same request object and response object. They belong to the same access request and response process, while httpservletresponse. the sendredirect method callers and callers use their respective request objects and response objects. They belong to two independent access requests and response processes.
For the redirection between internal resources of the same web application, especially before the redirection, some pre-processing requests should be performed, and httpservletrequest should be used. if the setattribute method is used to pass the preprocessing result, use requestdispatcher. forward method.
The httpservletresponse. sendredirect method should be used for redirection between different Web applications, especially when resources are redirected to another Web site.
(5) Neither the requestdispatcher. Forward method nor the httpservletresponse. sendredirect method can output any content to the client before calling them. If some content already exists in the buffer, the content will be cleared from the buffer.

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.