Difference between getrequestdispatcher () and sendredirect ()

Source: Internet
Author: User

1. Request. getrequestdispatcher () is request forwarding, and a request is shared between the front and back pages;
Response. sendredirect () is a redirection, and the front and back pages are not a request.

Request. getrequestdispather (); A requestdispatcher object is returned.

2. requestdispatcher. Forward () is run on the server;
Httpservletresponse. sendredirect () is accomplished by sending a command to the client browser.
Therefore, requestdispatcher. Forward () is "Transparent" for the browser ";
Httpservletresponse. sendredirect () is not.

 

3. the URL in servletcontext. getrequestdispatcher (string URL) can only use an absolute path.

Relative paths can be used for URLs in servletrequest. getrequestdispatcher (string URL. Because

Servletrequest has the concept of relative paths, while servletcontext does not.

 

The requestdispatcher object obtains the request from the client and passes them to the servlet, HTML, or

JSP. It has two methods:


1. Void forward (servletrequest request, servletresponse response)
The request can be passed by one servlet to receive the request, and the other servlet uses the request to generate the response. Request. response is the information returned by the client. Forward is called before response arrives at the client, that is, before response body output has been flushed. If not, it reports an exception.


2. Void include (servletrequest request, servletresponse response)
Used to record the request and response. You cannot modify the status information in response later.

 

To transfer requests to an address in another web app, follow these steps:
1. Obtain the servletconext object (currentservletcontext. getcontext (uripath) of another web app )).

2. Call the servletcontext. getrequestdispatcher (string URL) method.

Eg: servletcontext. getrequestdispatcher ("smserror. jsp"). Forward (request, response );

 

Http://lanew.blog.ccidnet.com/blog-htm-do-showone-type-blog-itemid-3690834-uid-327434.html

 

Differences:
Response. sendredirect (URL) redirects to the specified URL address to generate a new request. Therefore, to pass the parameter, only add the parameter after the URL
Number, such:
URL? Id = 1.
Request. getrequestdispatcher (URL). Forward (request, response) directly forwards the request to the specified URL.
The data of the previous request can be directly obtained, that is, the request object is always present and will not be re-created. While
Sendredirect () creates a request object, so data in the previous request is lost.
Specifically, this is the case:
Redirect will first send a response to the browser, then the browser receives the response and then sends a requeset to the server, and then
The server sends a new response to the browser. At this time, the request received by the page is sent from the browser.
Forward occurs inside the server and is sent to the response of another page of the browser without the knowledge of the browser.
The received request is not directly sent from the browser. It may have been stored in the request using request. setattribute.
You can directly use request. getattribute to obtain data.
The most basic usage is as follows. Other notes are as follows:
Redirect Mode
Http: // localhost: 8080/test application
The forward method can be used to redirect only one resource in the same web application. The sendredirect method allows you to redirect to any
URL.
Form Action = "/UU"; sendredirect ("/UU"); indicates the root path relative to the server. For example, http: // localhost: 8080/test should
Submitted to http: // localhost: 8080/UU );
The "/UU" in the forward code indicates the path relative to the Web application. For example, http: // localhost: 8080/test application (submit
Http: // localhost: 8080/test/UU );
(Use the forward method of the requestdispatcher Interface)
Forward () cannot be redirected to a JSP file with a frame. It can be redirected to an HTML file with a frame,
At the same time, forward () cannot be followed by parameters, such as Servlet? Name = Frank.
Response. setattribute ("name", name) to pass to the next page.
The URL in the browser address bar remains unchanged after redirection.
The forward method can be called only when the client has no output. If the buffer on the current page is not empty, you are calling
You must clear the buffer before using the forward method.
"/" Indicates the path relative to the Web Application
Requestdispatcher RD = request. getrequestdispatcher ("/OOO ");
Rd. Forward (request, response); Submit to http: // localhost: 8080/test/ooo
Requestdispatcher RD = getservletcontext (). getrequestdispatcher ("/OOO ");
Rd. Forward (request, response); Submit to http: // localhost: 8080/test/ooo
Requestdispatcher RD = getservletcontext (). getnameddispatcher ("testservlet"); (testservlet is
<Servlet-Name>)
Rd. Forward (request, response); Submit to the servlet named testservlet
If there are a lot of output before <JSP: Forward>, and the previous output will automatically output to the client when the buffer is full, the statement will not work,
Pay special attention to this.
Note: The browser address cannot be changed. Refresh will cause repeated submission.
Forward from http: // localhost: 8080/test/GW/page. jsp
<JSP: Forward page = "otherpage. jsp"/> after the JSP page is parsed, it is converted to pagecontext. Forward ("otherpage. jsp ");
"/Otherpage. jsp" submitted to http: // localhost: 8080/test/otherpage. jsp
"Otherpage. jsp" submitted to http: // localhost: 8080/test/GW/otherpage. jsp
(Sendredirect using the httpservletresponse Interface) method 302
It works on the user's browser. sendredirect () can be passed with parameters, such as Servlet? Name = Frank passed to the next page,
At the same time, it can be redirected to different hosts, and sendredirect () can be redirected to JSP files with frame.
Assume that the forwarding Code contains the registered servlet-URL:/ggg/TT; JSP:/ggg/TT. jsp:
Absolute path: Response. sendredirect ("http://www.brainysoftware.com
Root path: Response. sendredirect ("/OOO") sent to http: // localhost: 8080/ooo
Relative Path: response. sendredirect ("ooo") sent to http: // localhost: 8080/test/ggg/Ooo,
Sendredirect is equivalent to this method.
Response. setstatus (httpservletresponse. SC _moved_permanently );
String newlocn = "/newpath/JSA. jsp ";
Response. setheader ("location", newlocn );
(Meta Refresh) Method 200
This method is provided by HTML, and meta itself is an HTML Tag. Usage: <meta http-equiv = "refresh" content = "5;
Url = http://www.dreamdu.com/"/>
Corresponding Java code
String content = staytime + "; url =" + URL;
Response. setheader ("refresh", content );
Bytes ------------------------------------------------------------------------------------------------
--------------------------
Use response. sendredirect () to change the address bar
The information in the address bar of request. getrequestdispatcher (). Forward (request, response) remains unchanged.
Bytes ------------------------------------------------------------------------------------------------
--------------------------
Request. setattribute
You can only use method 2 to jump to a new page.

Difference between getrequestdispatcher () and sendredirect ()

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.