What is the difference between response.sendRedirect() and request.getRequestDispatcher().forward(request,response)

JavaJspRequestResponseForward

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

> 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():

  1. The forward() method is executed on the server-side.
  2. The request is transferred to another resource within the same server.
  3. It does not depend on the client’s request protocol since the forward () method is provided by the servlet container.
  4. The request is shared by the target resource.
  5. Only one call is consumed in this method.
  6. It can be used within the server.
  7. We cannot see the forwarded messages, it is transparent.
  8. The forward() method is faster than sendRedirect() method.
  9. It is declared in the RequestDispatcher interface.

sendRedirect():

  1. The sendRedirect() method is executed on the client-side.
  2. The request is transferred to another resource to a different server.
  3. The sendRedirect() method is provided under HTTP so it can be used only with HTTP clients.
  4. New request is created for the destination resource.
  5. Two request and response calls are consumed.
  6. It can be used within and outside the server.
  7. We can see redirected address, it is not transparent.
  8. The sendRedirect() method is slower because when new request is created old request object is lost.
  9. 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 - Java

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).

Solution 5 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrogerView Question on Stackoverflow
Solution 1 - JavaKeerthivasanView Answer on Stackoverflow
Solution 2 - JavaAnish RaiView Answer on Stackoverflow
Solution 3 - JavaMaulik KakadiyaView Answer on Stackoverflow
Solution 4 - JavaspriteView Answer on Stackoverflow
Solution 5 - JavaLijoView Answer on Stackoverflow