1.request.getrequestdispatcher () is the request forwarding, the front and back page share a request;
Response.sendredirect () is redirected, and the front and back pages are not a request.
Request.getrequestdispather (); Returns a RequestDispatcher object.
2.requestdispatcher.forward () is running on the server side;
Httpservletresponse.sendredirect () is done by sending a command to the client's browser.
So Requestdispatcher.forward () is "Transparent" to the browser;
and Httpservletresponse.sendredirect () is not.
URLs in the 3.servletcontext.getrequestdispatcher (String URL) can only use absolute paths;
The URLs in the Servletrequest.getrequestdispatcher (String URL) can use relative paths. Because
ServletRequest has a concept of relative path, while ServletContext object has no sub-concept.
The RequestDispatcher object obtains request requests from the client and passes them to the servlet,html on the server or
Jsp. It has two methods:
1.void forward (servletrequest request,servletresponse response)
To pass the request, a servlet can receive the request, and another servlet uses the request to generate the response. Request passes the requested, response is the information returned by the client. Forward to be called before response arrives at the client, i.e. before response body output has been flushed. If not, it will report an exception.
2.void include (servletrequest request,servletresponse response)
It is used to record the reservation request and response, and can no longer modify the information in response to indicate the state.
If you need to transfer a request to an address in another Web app, you can do this as follows:
1. Get another web app's Servletconext object (Currentservletcontext.getcontext (Uripath)).
2. Call ServletContext. The 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
The difference:
response.sendredirect (URL) jumps to the specified URL address, resulting in a new request, so the parameter is passed only after the URL is added
Number, such as:
Url?id=1.
Request.getrequestdispatcher (URL). Forward (Request,response) is a direct forward request to the specified URL, so the request
the ability to directly obtain the data from the last request, that is, request forwarding, the requests object is always present and is not recreated. and
Sendredirect () creates a new request object, so the data in the previous request is lost.
more specifically, this is the case:
redirect will first send a response to the browser, then the browser receives this response and then sends a requeset to the server, and then
The server sends a new response to the browser. At this point the request received from the page is a new one sent from the browser.
forward occurs inside the server and sends a response to the browser's other page without the browser's knowledge. This page
the request received was not sent directly from the browser and may have been durable request.setattribute the data in the request. On the page to go
data can be obtained directly with Request.getattribute.
The basic usage is as above, and some of the other places to be noted are as follows:
Jump Mode
http://localhost:8080/TestApplication
use the Forward method to redirect only one resource in the same Web application. And the Sendredirect method allows you to redirect to any
URL.
form form action= "/uu"; Sendredirect ("/uu"), relative to the server root path. Ashttp://localhost:8080/Testshould be
(then submit toHttp://localhost:8080/uu);
The "/uu" in the forward code represents the relative path to the Web App. Ashttp://localhost:8080/Testapply (then submit to
Http://localhost:8080/Test/uu);
(using the RequestDispatcher interface Forward) method
forward () cannot redirect to a frame-capable JSP file and can be redirected to a frame-capable HTML file .
at the same time forward () can not be passed in the back with the parameters, such as Servlet?name=frank, so no, the program through
Response.setattribute ("name", name) is passed to the next page.
The browser address bar URL does not change after redirection.
The forward method can be called only if the client does not have output. If the buffer of the current page is not empty, then you are calling the
The buffer must be emptied before the forward method.
"/" stands for relative to web app path
RequestDispatcher rd = Request.getrequestdispatcher ("/ooo");
Rd.forward (request, response); Submit tohttp://localhost:8080/Test/ooo
RequestDispatcher rd = Getservletcontext (). Getrequestdispatcher ("/ooo");
Rd.forward (request, response); Submit tohttp://localhost:8080/Test/ooo
RequestDispatcher Rd =getservletcontext (). Getnameddispatcher ("Testservlet"); (Testservlet is a
<servlet-name>)
Rd.forward (request, response); submit to servlet named Testservlet
If there is a lot of output before <jsp:forward>, the previous output has filled the buffer and will automatically output to the client, then the statement will not work.
This should be paid special attention.
Also note: It can not change the browser address, refresh will lead to repeated submissions
fromhttp://localhost:8080/Test/gw/page.jspForwarding in
<jsp:forward page= "otherpage.jsp"/> Translated into Pagecontext.forward ("otherpage.jsp") after the JSP page was parsed;
"/otherpage.jsp" submitted tohttp://localhost:8080/Test/OtherPage.jsp
"otherpage.jsp" submitted tohttp://localhost:8080/Test/gw/OtherPage.jsp
(using the HttpServletResponse interface Sendredirect) method 302
is to work in the user's browser, sendredirect () can be passed with parameters, such as Servlet?name=frank to the next page,
at the same time it can redirect to a different host, Sendredirect () can redirect the JSP file with frame.
assume that the forwarding code is included in the registered Servlet-url of/ggg/tt;jsp for/ggg/tt.jsp:
Absolute path: Response.sendredirect ("http://www.brainysoftware.com
Root path: Response.sendredirect ("/ooo") sent tohttp://localhost:8080/ooo
relative path: Response.sendredirect ("ooo") sent tohttp://localhost:8080/Test/ggg/ooo,
Sendredirect is equivalent to this way
Response.setstatus (httpservletresponse.sc_moved_permanently);
String newlocn = "/newpath/jsa.jsp";
Response.setheader ("location", NEWLOCN);
(Meta Refresh) method
This method is provided by HTML, and meta itself is an HTML tag. The method of use is: <meta http-equiv= "Refresh" content= "5;
url=http://www.dreamdu.com/"/>
The corresponding Java code
String content=staytime+ "; Url= "+url;
Response.setheader ("REFRESH", content);
------------------------------------------------------------------------------------------------
--------------------------
using the Response.sendredirect () address bar will change
Use the information in the Request.getrequestdispatcher (). Forward (Request,response) address bar to remain unchanged.
------------------------------------------------------------------------------------------------
--------------------------
Request.setattribute The things that exist
only use the method 2 to jump to the new page to take out
The difference between JSP's Getrequestdispatcher () and Sendredirect ()