Response. sendredirect () and request. getrequestdispatcher (). Forward ()

Source: Internet
Author: User

Response in JSP. sendredirect () and request. getrequestdispatcher (). both the forward (request, response) objects can redirect pages, but there is a big difference between the two objects. In terms of separation, there are the following points:

Response. sendredirect (URL) ----- redirect to the specified URL

Request. getrequestdispatcher (URL). Forward (request, response) ----- the request is forwarded to the specified URL

Response. sendredirect (URL) ----- client jump

Request. getrequestdispatcher (URL). Forward (request, response) ----- server jump

After response. sendredirect (URL) jumps to the specified URL address, all the requests on the previous page (the original page before the jump) end, the original request object will die, and the data will disappear. Next, a new request object is created on the new page, that is, a new request object is generated.

[Detailed process: Redirect will first send a response to the browser, then the browser receives the response and then sends a requeset to the server, and the server receives the new response to the browser. In this case, a new request is obtained from the browser. At this time, all the items saved with request. setattribute on the previous page are lost. If request. getattribute is used in the current new page, the result will be null .]

Request. getrequestdispatcher (URL ). forward (request, response) uses the request forwarding method. When you jump to the page, it redirects with the original page request and response. The request object always exists and will not be created again.

[Detailed process: Forward occurs inside the server and is sent to the response of another page of the browser without the knowledge of the browser. at this time, the request received by the page is not directly sent from the browser, it may be that the request has been used during page conversion. setattribute puts data in the request, and you can directly use the request on the page to be transferred. the data is obtained by getattribute.]

Using the URL in the response. sendredirect () address bar will change

Use the URL in the request. getrequestdispatcher (). Forward (request, response) Address Bar to remain unchanged.

If you need to pass parameters when using response. sendredirect (), you can only add parameters after the URL, such as: URL? Id = 1, but not through the request or response method.

Use request. getrequestdispatcher (). forward (request, response) if you need to pass parameters, you can use response. setattribute ("name", name) to pass to the next page. instead of passing parameters in the backend, such as Servlet? Name = Frank.

The sendredirect () method allows you to redirect to any URL, while the forward () method can only redirect to a resource in the same web application.

Action = "/UU"; sendredirect ("/UU"); indicates the root path relative to the server. If the root path of the server is http: // localhost: 8080/test, submit it to http: // localhost: 8080/Uu; the "/UU" in the forward code represents the path relative to the Web application. For example, the http: // localhost: 8080/test application is submitted to http: // localhost: 8080/test/UU.

7. Use the sendredirect () method of the httpservletresponse Interface

Sendredirect ()It works on the user's browser and can be redirected to different hosts. sendredirect () can be used to redirect JSP files with frames.

Assume that the forwarding Code contains the registered servlet-URL:/ggg/TT; JSP:/ggg/TT. jsp.

Absolute path: Response. sendredirect ("http://www.brainysoftware.com") to http://www.brainysoftware.com
Root path: Response. sendredirect ("/OOO") sent to http: // localhost: 8080/ooo
Relative Path: response. sendredirect ("ooo") is 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 );

Forward uses the forward () method of the requestdispatcher Interface 

Forward ()You cannot redirect to a JSP file with a frame. You can redirect to an HTML file with a frame,

The forward method can be called only when the client has no output. If the buffer on the current page is not empty, you must clear the buffer before calling 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 a <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, this statement will not work, so 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

 

 

 

 

In addition to the two page Jump methods, there is also a convenient method:

Meta Refresh Method
This method is provided by HTML, and meta itself is an HTML Tag. The usage is as follows:

<Meta http-equiv = "refresh" content = "5; URLs = http://www.dreamdu.com/"/>
The corresponding Java code is:
String content = staytime + "; url =" + URL;
Response. setheader ("refresh", content );
 

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.