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 (); returns a requestdispatcher object.
2. requestdispatcher. Forward () is run on the server; httpservletresponse. sendredirect () is done by sending a command to the client browser.
Therefore, requestdispatcher. Forward () is "Transparent" for the browser, while httpservletresponse. sendredirect () is not.
3. The URLs in servletcontext. getrequestdispatcher (string URL) can only use absolute paths, while those in servletrequest. getrequestdispatcher (string URL) can use relative paths. Because servletrequest has the concept of relative paths, while servletcontext does not.
RequestdispatcherObjects GET request requests from the client and pass them to the servlet, HTML, or JSP on the server. It has two methods:
1. Void forward (servletrequest request, servletresponse response) is used to pass the request. One servlet can receive the request, and the other servlet uses this 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) is used to record the request and response, and the status information in response cannot be modified in the future.
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 );
Differences:
Response. sendredirect (URL) redirects to the specified URL address to generate a new request. Therefore, to pass a parameter, only add parameters after the URL, such as: URL? Id = 1.
Request. getrequestdispatcher (URL ). forward (request, response) directly forwards the request to the specified URL. Therefore, the request can directly obtain the data of the previous request, that is, the request is forwarded, And the request object always exists, will not be re-created. Sendredirect () creates a request object, so the data in the previous request is lost.
Specifically, redirect will first send a response to the browser, and then the browser receives the response and sends a requeset to the server. 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. at this time, the request received by the page is not directly sent from the browser, and may have been used. setattribute stores data in the request. you can directly use request. getattribute to obtain data.
The most basic usage is as above. Other notes should be noted as follows: Jump mode http: // localhost: the 8080/test application can only redirect to one resource in the same web application using the forward method. 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 be used (then 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 to http: // localhost: 8080/test/UU );
(Using the forward method of the requestdispatcher Interface) method Forward () cannot be redirected to a JSP file with a frame, and 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. If this is not the case, you can use response in the program. 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 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, the statement will not work,
Pay special attention to this.
Note:
It cannot change the browser address. Refresh will lead to repeated submission 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. jsp
(Sendredirect using the httpservletresponse Interface) method 302 works on the user's browser. sendredirect () can be passed with parameters, such as Servlet? Name = Frank is uploaded to the next page, and can be redirected to different hosts. sendredirect () can be used to redirect 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 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. Use: <meta http-equiv = "refresh" content = "5; url = http://www.dreamdu.com/"/> corresponding Java code string content = staytime + "; url = "+ URL; response. setheader ("refresh", content );
Use response. the sendredirect () address bar will change the request. getrequestdispatcher (). the information in the forward (request, response) Address Bar remains unchanged. request. the setattribute stored items can be retrieved from the new page only by redirecting through method 2.