What is the difference between response.sendRedirect() and request.getRequestDispatcher().forward(request,response) [duplicate]
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?
Solution 1:
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.
Solution 2:
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.
Solution 3:
The main difference between the forward() and sendRedirect() methods is that in the case of forward(), redirect happens at the server end and is not visible to the client, but in the case of sendRedirect(), redirection happens at the client end and it's visible to the client.
Other difference between Forward(ServletRequest request, ServletResponse response)
and sendRedirect(String url)
is
forward():
- The
forward()
method is executed on the server-side. - The request is transferred to another resource within the 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 the server.
- We cannot see the forwarded messages, it is transparent.
- The
forward()
method is faster thansendRedirect()
method. - It is declared in the
RequestDispatcher
interface.
sendRedirect():
- The sendRedirect() method is executed on the client-side.
- The request is transferred to another resource to a different server.
- The sendRedirect() method is provided under HTTP so it can be used only with HTTP 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.
Which one is good? It depends upon the scenario for which method is more useful.
If you want control is transfer to a new server or context, and it is treated as a completely new task, then we go for sendRedirect. Generally, a forward should be used if the operation can be safely repeated upon a browser reload of the web page and will not affect the result.
Solution 4:
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).