Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 615
roger Avatar asked Dec 04 '13 09:12

roger


People also ask

What is the difference between response sendRedirect () and forward ()?

SendRediret() When we use the forward method request is transferred to other resources within the same server for further processing. In case of sendRedirect request is transferred to another resource to a different domain or different server for further processing.

What is the difference between sendRedirect () and forward () in a servlet?

Difference between forward() and sendRedirect() methodThe forward() method works at server side. The sendRedirect() method works at client side. It sends the same request and response objects to another servlet. It always sends a new request.

What is difference between ServletResponse sendRedirect () and RequestDispatcher forward () method?

This is also called server side redirect. A RequestDispatcher forward() is used to forward the same request to another resource whereas ServletResponse sendRedirect() is a two step process. In sendRedirect(), web application returns the response to client with status code 302 (redirect) with URL to send the request.

What does Response sendRedirect do?

sendRedirect() method redirects the response to another resource, inside or outside the server. It makes the client/browser to create a new request to get to the resource. It sends a temporary redirect response to the client using the specified redirect location URL.


1 Answers

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.

like image 115
Keerthivasan Avatar answered Sep 22 '22 22:09

Keerthivasan