Tuesday, November 8, 2011

Difference between sendRedirect and forward

sendRedirect is the marriage which is leagal as both the parties know each other and faithful.
forward: where one of the party cheats and contacts to third party.. a illicit relationship..

Probably this will make a clear concept to us, as all time we like to thinks in different way.

So, from the above we are sure about the family where Hubby and wife don't hide anything from each other. They share to each others every thing. Suppose, coming back from office, Husband request to make a cup of tea to his lovely wife. Wife is busy with her favorite TV serials. So, she tells the maid servant to mak the tea for him. Beside this Wife also inform his hubby about the maker of the tea and request to get tea from the servant. Then hubby will take the update of the tea from the servant.

On the contrary, in another family getting the request of tea from the husband, wife asked to make tea another one but after completion she must provide the tea directly to his wife.

First Family is sendRedirect and the second one is forward.

sendRedirect() sends a redirect response back to the client's browser. The browser will normally interpret this response by initiating a new request to the redirect URL given in the response.

forward() does not involve the client's browser. It just takes browser's current request, and hands it off to another servlet/jsp to handle. The client doesn't know that they're request is being handled by a different servlet/jsp than they originally called.

Sendredirect( ) : javax.Servlet.Http.HttpServletResponce interface
- RequestDispatcher.SendRedirect( ) works on the browser.
- The SendRedirect( ) allows you to redirect trip to the Client.
- The SendRedirect( ) allows you to redirect to any URL.
- After executing the SendRedirect( ) the control will not return back to same method.
- The Client receives the Http response code 302 indicating that temporarly the client is being redirected to the specified location , if the specified location is relative , this method converts it into an absolute URL before redirecting.
- The SendRedirect( ) will come to the Client and go back,.. ie URL appending will happen.





Forward( ) : javax.Servlet.RequestDispatcher interface.

- RequestDispatcher.forward( ) works on the Server.
- The forward( ) works inside the WebContainer.
- The forward( ) restricts you to redirect only to a resource in the same web-Application.
- After executing the forward( ), the control will return back to the same method from where the forward method was called.
- The forward( ) will redirect in the application server itself, it doesn't come back to the client.
- The forward( ) is faster than Sendredirect( ) .

No comments: