From: http://cantellow.javaeye.com/blog/372941
I don't need to say much about the differences between the two. A lot of searches are probably:
Redirection is a client jump, and forwarding is a server jump
The forwarding and redirection statements in servlet are as follows:
Request. getrequestdispatcher ("New. jsp"). Forward (request, response); // forward to new. jsp
Response. sendredirect ("New. jsp"); // redirect to new. jsp
On the JSP page, you will also see the following method for forwarding:
<JSP: Forward page = "New. jsp"/>
Of course, you can also implement redirection on the JSP page:
<% Response. sendredirect ("New. jsp ");//
Redirect to new. jsp
%>
In fact, I think the URL address in JSP is also a redirection, because clicking this request scope is invalid. parameters must be used to pass the information to the request.
In short, JSP/servlet all have the means of forwarding and redirection, so I will summarize the path problem.
There are two methods in servlet to get the forwarding object (requestdispatcher): One is through httpservletrequest
Getrequestdispatcher () is obtained by getrequestdispatcher () of servletcontext.
;
There is only one method for redirection: httpservletresponse's sendredirect () method.
The parameters of these three methods are URL strings, but they are different in relative or absolute paths.
★Httpservletresponse. sendredirect (string)
Parameters can be specified as relative paths, absolute paths, or other web applications.
Assume that http: // localhost/MyApp/cool/bar. Do
The servlet to which the request arrives.
Relative Path: response. sendredirect ("foo/stuff. Do ")
The container adds parameters to the directory of the original request URL to generate the complete URL-http: // localhost/MyApp/cool/Foo/stuff. Do.
Absolute path: Response. sendredirect ("/Foo/stuff. Do ")
Create a complete URL -- http: // localhost/Foo/stuff. Do.
Other web applications: Response. sendredirect ("http://www.javaeye.com
")
The container directs the URL directly.
★Httpservletrequest. getrequestdispatcher (string)
The parameter can be specified as a relative or absolute path.
The complete URL generated in relative paths is the same as the redirection method.
The absolute path and redirection are different. The container will generate a complete URL for the root directory of the Web application with parameters, that is:
The URL generated by request. getrequestdispatcher ("/Foo/stuff. Do") is http: // localhost/MyApp/Foo/stuff. Do.
.
★Servletcontext. getrequestdispatcher (string)
The parameter can only be specified as an absolute path. The generated complete URL is the same as httpservletrequest. getrequestdispatcher (string.
Appendix: http://wenwen.soso.com/z/q66415822.htm
Their calls are as follows:
Request. getrequestdispatcher ("New. jsp"). Forward (request, response); // forward to new. jsp
Response. sendredirect ("New. jsp"); // redirect to new. jsp
On the JSP page, you will also see the following method for forwarding:
<JSP: Forward page = "apage. jsp"/>
Redirection process:
Client browser sends HTTP request ----
After the Web Server accepts the request, it sends a 302 response requesting the client browser to send a new HTTP request ----"
After the client browser accepts the response, it sends a new HTTP request to the server ----"
The server searches for resources and sends them to the customer based on the request,
It can be redirected to any URL,
Data within the request range cannot be shared.
Forwarding process:
Client browser sends HTTP request ----
After the Web Server accepts this request, it calls an internal method to complete request processing and forwarding in the container ----"
Send the target resource to the customer. It can only be used in the same web application and can share data within the request range.
How can I choose redirection or forwarding? Generally, the forwarding speed is faster and the objects in the request can be kept,
So he is the first choice. However, after forwarding, the URL in the browser still points to the start page,
If you reload the current page, the start page will be called again. If you do not want to see such a situation, select forwarding.