Monday, May 22, 2006

sendRedirect() and forward()

Difference between sendRedirect() and forward() "Redirect is an indication from the server to the browser to redirect to the defined URL. When a request comes from the browser, the server receives the request, fetches the URL to be redirected and again informs the browser to direct to that particualr URL, then browser redirects to that URL. On the whole, this is a round about process since request is forwarded twice first frm browser to server and server back to Browser.

Forward is a mechanism in which the server directly directs the browser to the new url deifined and since its an implicit jsp object, the whole action is govened by the container /server.

Some more Explanation :-

Both the sendRedirect and forward methods bring the user to a new resource. There is a fundamental difference between the two, however, and understanding this can help you write a more efficient servlet.
The sendRedirect method works by sending a status code that tells the browser to request another URL. This means that there is always a round trip to the client side. Additionally, the previous HttpServletRequest object is lost. To pass information between the original servlet and the next request, you normally pass the information as a query string appended to the destination URL.
The forward method, on the other hand, redirects the request without the help from the client's browser. Both the HttpServletRequest object and the HttpServletResponse object also are passed to the new resource.

Forward is faster compared to Redirection.

0 Comments:

Post a Comment

<< Home