Monday, July 18, 2011

Difference between Forward and sendRedirect

Forward:

Since forward() method of RequestDispatcher is handled on the server ,
therefore the request and its associated session are available to the forwarded
resource and you can pass data between them using request.setAttribute().
forward()separates the responsibilities for handling the requests among several
components. This method generally sends a request and response object to
resources (servlets or JSP's) of the same ServletContext. You can also decide to
forward to one page in one condition and a different page in another. RequestDispatcher
transfers control immediately whenever the forward() method is called to the resource
referenced by the RequestDispatcher.A forward is performed internally by the container
the browser is completely unaware that it has taken place, so its original URL remains
intact and any browser reload of the resulting page will simply repeat the original request,
with the original URL. When the forward is done, the original request and response objects are
transfered along with additional parameters if needed.


sendRedirect

The transfer of control 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. It is like keying in a new
URL in the browser. A sendRedirect() also updates the browser history and transfers control
only when the whole service method completes. There is only one way to pass data is through the
session or using web parameters.RequestDispatcher.forward() and PageContext.forward() are effectively
the same. PageContext.forward is a helper method that calls the RequestDispatcher method.
When you invoke RequestDispatcher.include(), the servlet engine transfers control of this HTTP request
internally from your current servlet or JSP to another servlet or JSP or static file, while invoking
response.sendRedirect() method sends an HTTP response to the browser to make another request at a different URL.
 
Aredirect is a two step process, where the web application instructs the browser to fetch a second URL, which
differs from the original. A browser reload of the second URL will not repeat the original request, but will
rather fetch the second URL.Redirect is slower than a forward, since it requires two browser requests, not one .
Objects placed in the original request scope are not available to the second request
 
For example, sendRedirect can transfer control from http://javapapers.com to http://anydomain.com but forward
cannot do this.
 
‘session’ is not lost in both forward and redirect.
To identify difference between forward and sendRedirect keep an eye on the address bar of your browser, in forward, you will not see the forwarded address in redirect, you can see the redirected address.

No comments:

Post a Comment