This question already has an answer here:
I have got a problem with my page jump when I use JAVA, if I use:
response.sendRedirect("login.jsp")
then I get this url: http://localhost:8080/login.jsp
But if I use
request.getRequestDispathcer("login.jsp").forward(request, response)
then I get this url: http://localhost:8080/Shopping/login.jsp
(the "Shopping" is the name of my module).
What's the difference?
To simply explain the difference,
response.sendRedirect("login.jsp");
doesn't prepend the contextpath (refers to the application/module in which the servlet is bundled)
but, whereas
request.getRequestDispathcer("login.jsp").forward(request, response);
will prepend the contextpath of the respective application
Furthermore, Redirect request is used to redirect to resources to different servers or domains. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url.
Forward request is used to forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved.
forward
Control can be forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved. This is the major difference between forward and sendRedirect. When the forward is done, the original request and response objects are transfered along with additional parameters if needed.
redirect
Control can be redirect to resources to different servers or domains. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url. Since it is a new request, the old request and response object is lost.
For example, sendRedirect can transfer control from http://google.com to http://anydomain.com but forward cannot do this.
‘session’ is not lost in both forward and redirect.
To feel the difference between forward and sendRedirect visually see the address bar of your browser, in forward, you will not see the forwarded address (since the browser is not involved) in redirect, you can see the redirected address.
Simply difference between Forward
(ServletRequest request, ServletResponse response) and sendRedirect
(String url) is
forward():
- The
forward()
method is executed in the server side. - The request is transfer to other resource within same server.
- It does not depend on the client’s request protocol since the
forward ()
method is provided by the servlet container. - The request is shared by the target resource.
- Only one call is consumed in this method.
- It can be used within server.
- We cannot see forwarded message, it is transparent.
- The forward() method is faster than
sendRedirect()
method. - It is declared in
RequestDispatcher
interface.
sendRedirect():
- The
sendRedirect()
method is executed in the client side. - The request is transfer to other resource to different server.
- The
sendRedirect()
method is provided underHTTP
so it can be used only withHTTP
clients. - New request is created for the destination resource.
- Two request and response calls are consumed.
- It can be used within and outside the server.
- We can see redirected address, it is not transparent.
- The
sendRedirect()
method is slower because when new request is created old request object is lost. - It is declared in
HttpServletResponse
.
1.redirect return the request to the browser from server,then resend the request to the server from browser.
2.forward send the request to another servlet (servlet to servlet).
Redirect and Request dispatcher are two different methods to move form one page to another. if we are using redirect to a new page actually a new request is happening from the client side itself to the new page. so we can see the change in the URL. Since redirection is a new request the old request values are not available here.
来源:https://stackoverflow.com/questions/20371220/what-is-the-difference-between-response-sendredirect-and-request-getrequestdis