Difference between getRequestDispatcher () and sendRedirect ()
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.