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

I have a problem with my page 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 ("Purchase" is the name of my module).

What's the difference?

+44
java jsp forward response request
Dec 04 '13 at 9:14
source share
6 answers

To just explain the difference,

  response.sendRedirect("login.jsp"); 

does not add context path (refers to the application / module in which the servlet is connected)

but whereas

  request.getRequestDispathcer("login.jsp").forward(request, response); 

add context path to the corresponding application

In addition, the Redirection Request is used to redirect resources to different servers or domains. This transfer of the management task is delegated to the browser by the container. That is, the redirect sends the header back to the browser / client. This header contains the URL of the resource that will be redirected by the browser. The browser then initiates a new request to the specified URL.

The forwarding request is used to forward the resources available on the server from which the call is made. This transfer of control is carried out by the container inside, and the browser / client is not involved.

+64
Dec 04 '13 at 9:16
source share

forward

The control can be redirected to resources available on the server from which the call is made. This transfer of control is carried out by the container inside, and the browser / client is not involved. This is the main difference between forward and sendRedirect. When the forward is completed, the original request and response objects are passed along with additional parameters, if necessary.

redirect

Management can be redirected to resources on different servers or domains. This transfer of the management task is delegated to the browser by the container. That is, the redirect sends the header back to the browser / client. This header contains the URL of the resource that will be redirected by the browser. The browser then initiates a new request to the specified URL. Since this is a new request, the old request and response object is lost.

For example, sendRedirect may transfer control from http://google.com to http://anydomain.com , but forwarding is not possible.

'not lost in both direct and forwarding.

To feel the difference between forward and sendRedirect, visually see the address bar of your browser, forward, you will not see the redirected address (since the browser is not involved) when redirecting you can see the redirected address.

+16
Apr 09 '14 at 7:32
source share

A simple distinction between Forward (ServletRequest request, ServletResponse response) and sendRedirect (string URL)

forward ():

  • The forward() method is executed on the server side.
  • A request is a transfer to another resource on the same server.
  • It is independent of the client request protocol since the forward () method is provided by the servlet container.
  • The request is shared by the target resource.
  • This method uses only one call.
  • It can be used on the server.
  • We cannot see the redirected message, it is transparent.
  • The forward () method is faster than the sendRedirect() method.
  • It is declared in the RequestDispatcher interface.

sendRedirect ():

  • The sendRedirect() method is executed on the client side.
  • A request is a transfer of another resource to another server.
  • The sendRedirect() method is provided in HTTP , so it can only be used with HTTP clients.
  • A new request is created for the target resource.
  • Two request and response requests are consumed.
  • It can be used both inside and outside the server.
  • We can see the redirected address, it is not transparent.
  • The sendRedirect() method is slower because when you create a new request, the old request object is lost.
  • Announced in HttpServletResponse .
+12
Dec 28 '15 at 6:47
source share

1.redirect returns the request to the browser from the server, and then resubmits the request to the server from the browser.

2. send the request to another servlet (servlet servlet).

+3
Dec 04 '13 at 9:22
source share

The redirect and request manager are two different methods for moving one page to another. if we use redirection to a new page, in fact there is a new request from the client side to a new page. so we can see the URL change. Since redirection is a new request, old request values โ€‹โ€‹are not available here.

+2
Dec 04 '13 at 9:52
source share

redirect: return the request to the browser / client from the server, and then resend the request to the server from the browser / client.

forward: send a request to another servlet (servlet to servlet) and do not send the request to another browser / client.

0
Dec 17 '15 at 16:53
source share



All Articles