Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between RequestDispatcher.forward() and HttpServletResponse.sendRedirect()? [duplicate]

What is the difference between RequestDispatcher's forward() and HttpServletResponse's sendRedirect() method?
Can anyone explain with a example and best usage of these methods with a real time example?

like image 523
Raj Avatar asked Aug 28 '11 10:08

Raj


People also ask

What is the key difference between using forward and HttpServletResponse sendRedirect ()?

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 RequestDispatcher and sendRedirect () method?

The RequestDispatcher interface allows you to do a server side forward/include whereas sendRedirect() does a client side redirect. SendRedirect() will search the content between the servers. it is slow because it has to intimate the browser by sending the URL of the content.

How sendRedirect () method of HttpServletResponse differ with forward () method of RequestDispatcher?

The forward() method is faster than sendRedirect() method. The sendRedirect() method is slower because when new request is created old request object is lost. It is declared in RequestDispatcher interface. It is declared in HttpServletResponse.

Why use RequestDispatcher to forward a request to another resource instead of sendRedirect?

Why use RequestDispatcher to forward a request to another resource, instead of using a sendRedirect? JSP. Redirects are no longer supported in the current servlet API. The RequestDispatcher does not use the reflection API.


3 Answers

Redirection is a type of response sent back to the client, whereas the forward delegation takes place completely on the server side and the result of the forward action returns to the client as if it came only from the original URL.

Another difference is that forward delegation can be used only for in-applications resources, whereas redirection command can redirect the client browser outside the current domain.

Examples:

// Sends a temporary redirect to the HTTP client. Only absolute URLs are allowed.
ServletResponse.sendRedirect(String location);


// Delegates one HttpRequest to another dynamic or static resource
HttpRequest.getRequestDispatcher("example.jsp").forward(request, response);


// Includes/enriches current response with another dynamic or static resource
HttpRequest.getRequestDispatcher("example.html").include(request, response);


Another good explanation can be found here:
Difference between sendRedirect() and forward()
like image 75
dov.amir Avatar answered Oct 04 '22 11:10

dov.amir


SendRedirect ():
This method is declared in HttpServletResponse Interface

Signature: void sendRedirect(String url)

This method is used to redirect client request to some other location for further processing ,the new location is available on different server or different context.our web container handle this and transfer the request using browser ,and this request is visible in browser as a new request. Some time this is also called as client side redirect.

Forward(): This method is declared in RequestDispatcher Interface.

Signature: forward(ServletRequest request, ServletResponse response)

This method is used to pass the request to another resource for further processing within the same server, another resource could be any servlet, jsp page any kind of file.This process is taken care by web container when we call forward method request is sent to another resource without the client being informed, which resource will handle the request it has been mention on requestDispatcher object which we can get by two ways either using ServletContext or Request. 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. The request sent is a completely new request.

B forward() is handled internally by the container whereas sednRedirect() is handled by browser.

C We should use forward() when accessing resources in the same application because it’s faster than sendRedirect() method that required an extra network call.

D In forward() browser is unaware of the actual processing resource and the URL in address bar remains same whereas in sendRedirect() URL in address bar change to the forwarded resource.

E forward() can’t be used to invoke a servlet in another context, we can only use sendRedirect() in this case.

Details Explanation Here

like image 37
Sameer Kazi Avatar answered Oct 04 '22 10:10

Sameer Kazi


We can use request dispatcher only when the other servlet to which the request is being forwarded lies in the same application. On the other hand Send Redirect can be used in both the cases if the two servlets resides in a same application or in different applications.

like image 30
Saurabh Avatar answered Oct 04 '22 11:10

Saurabh