Turn from: http://cantellow.javaeye.com/blog/372941
About the difference between the two I don't have to say, a lot of search, probably is:
Redirect is client jump, forwarding is server-side jump
The following statements are invoked in the servlet for forwarding and redirection:
Request.getrequestdispatcher ("new.jsp"). Forward (request, response);//Forwarding to new.jsp
Response.sendredirect ("new.jsp");//Redirect to new.jsp
In the JSP page you will also see the following way to implement forwarding:
<jsp:forward page= "new.jsp"/>
Of course, you can also implement redirects in the JSP page:
<%response.sendredirect ("new.jsp")//redirect to new.jsp%>
In fact, I think the URL address in the JSP is also redirected, because click on this request scope invalidation, to pass to the information to request must use parameters to do.
In short jsp/servlet all have forwarding and redirect means, then I summed up the path problem.
There are two ways to get forward objects (RequestDispatcher) in the servlet: one is obtained by the HttpServletRequest Getrequestdispatcher () method, One is obtained by the ServletContext Getrequestdispatcher () method;
There is only one way to redirect: The HttpServletResponse Sendredirect () method.
The arguments for these three methods are a string in the form of a URL, but there is a difference between using a relative path or an absolute path.
★ Httpservletresponse.sendredirect (String)
Parameters can be specified as relative paths, absolute paths, or other Web applications.
Assume that the servlet to which this method belongs is reached through a http://localhost/myApp/cool/bar.do request.
Relative path: Response.sendredirect ("Foo/stuff.do")
The container generates a complete url--http://localhost/myapp/cool/foo/stuff.do relative to the original request URL's directory plus parameter.
Absolute path: Response.sendredirect ("/foo/stuff.do")
The container establishes a complete url--http://localhost/foo/stuff.do relative to the Web application itself.
Other Web applications: Response.sendredirect ("http://www.javaeye.com")
The container is directed directly to the URL.
★ Httpservletrequest.getrequestdispatcher (String)
Parameter can be specified as a relative or absolute path.
The complete URL generated in the case of a relative path is the same as the redirection method.
Absolute paths differ from redirects in that the container generates a complete URL relative to the root of the Web application, namely:
The URL generated by Request.getrequestdispatcher ("/foo/stuff.do") is http://localhost/myApp/foo/stuff.do.
★ Servletcontext.getrequestdispatcher (String)
Parameter can only be specified as an absolute path and the complete URL generated is the same as Httpservletrequest.getrequestdispatcher (String).
Attached: http://wenwen.soso.com/z/q66415822.htm
Their invocation is as follows:
Request.getrequestdispatcher ("new.jsp"). Forward (request, response);//Forwarding to new.jsp
Response.sendredirect ("new.jsp");//Redirect to new.jsp
In the JSP page you will also see the following way to implement forwarding:
<jsp:forward page= "apage.jsp"/>
REDIRECT process:
client browser sends HTTP request----"
The Web server sends a 302 response after it accepts the request that the client browser send a new HTTP request----"
After the client browser accepts this response, it sends a new HTTP request to the server----
The server looks for resources and sends them to customers based on this request.
It can be redirected to any URL,
Data in the request scope cannot be shared.
Forwarding process:
client browser sends HTTP request----"
After the Web server accepts this request, a method inside the call completes the request processing and forwarding action within the container----"
Send the target resource to the customer, which can only be used in the same Web application and share the data in the request scope.
How to choose whether to redirect or forward. Forwarding is usually faster and can keep objects within the request,
So he's the first choice. However, since the URL in the browser still points to the start page after forwarding,
At this point, if the current page is overloaded, the start page will be called back. If you do not want to see such a situation, select forward.