Difference between response. sendredirect () and request. getrequestdispatcher (). Forward ()

Source: Internet
Author: User
Response in JSP. sendredirect () and request. getrequestdispatcher (). both of the Forward (request, response) objects can redirect pages, but there is a big difference between the two objects. In terms of the split, there are the following points: ① response. sendredirect (URL) ----- redirects to the specified URL request. getrequestdispatcher (URL ). forward (request, response) ----- the request is forwarded to the specified URL ② response. sendredirect (URL) ----- Is a client jump request. getrequestdispatcher (URL ). forward (request, response) ----- Is the server jump ③ response. after the sendredirect (URL) Jump to the specified URL address All requests in the original page) will 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 address bar of response. sendredirect () changes the URL in the address bar of request. getrequestdispatcher (). Forward (request, response. ⑤ 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. 6. The sendredirect () method can redirect you 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. The sendredirect () method using the httpservletresponse interface sendredirect () is used on the user's browser, and can be redirected to different hosts, sendredirect () you can 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") is sent to the http://www.brainysoftware.com root path: response. sendredirect ("/OOO") is 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 to redirect a JSP file with a frame 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 relative path 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>, the previous output has filled the buffer, it will be automatically output to the client, this statement will not work, so pay special attention to this. Note: The browser address cannot be changed. Refresh will cause repeated submission. Starting from http: // localhost: 8080/test/GW/page. forward in 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. in addition to the two page Jump methods, JSP also has a convenient method: Meta Refresh method, which is provided by HTML, and meta itself is an HTML Tag. Use: <meta http-equiv = "refresh" content = "5; url = http://www.dreamdu.com/"/> the corresponding Java code is: String content = staytime + "; url = "+ URL; response. setheader ("refresh", content );


From: http://jorton468.blog.163.com/blog/static/72588135201101711810508/

Difference between response. sendredirect () and request. getrequestdispatcher (). Forward ()

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.