Difference between forward and sendRedirect
May 13th, 2008forward
Control can be forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved. This is the major difference between forward and sendRedirect. When the forward is done, the original request and response objects are transfered along with additional parameters if needed.
redirect
Control can be redirect to resources to different servers or domains. This transfer of control task 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.
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 feel the difference between forward and sendRedirect visually see the address bar of your browser,
in forward, you will not see the forwarded address (since the browser is not involved)
in redirect, you can see the redirected address.
When can we use forward and when can we use sendRedirect?
Technical scenario: redirect should be used
- If you need to transfer control to different domain
- To achieve separation of task.
For example, database update and data display can be separated by redirect. Do the PaymentProcess and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PyamenProcess will not be repeated. But if you use forward in this scenario, both PaymentProcess and displayPaymentInfo will be re-executed sequentially, which may result in incosistent data.
For other than the above two scenarios, forward is efficient to use since it is faster than sendRedirect.
Example for forward and sendRedirect based on real world
Consider the real world scenario, the milk man comes and asks for monthly payment to you in your house. Here house is the container and you are a resource existing in the container. Milk man is the client or browser.
He asks for the monthly payment to you, this is the request made by the browser to resource A. If you go inside your house and ask your mother (another resource B inside the same container) for the cash and come back and deliver to milkman this is called forward.
If you ask the milkman to speak himself to your mother inside your house or you ask the milkman to speak to your father who is in his office (different domain) then this is called redirect.



Hi,
Thank you for your excellent work. I liked the example you have given here for the difference between forward and redirect.
Brilliant example! :)
Very good explanations for forward and execellent example for the difference
Thank you!
Ha ha ha thats a gr8 example….
really good explanation for difference between forward and redirect and practical example.
Very good! Thanks a lot.
awesome style to explain this scenario,
I was really searching on internet for the real example for sendRedirect and forward. Finally I found from this article … thanks a ton
Finally, I know the difference between forward and sendRedirect :P . Thanks a lot.
Nice Explanation. Thank You
Good points detailing out the differences and mentioning when to use what. Thank you.
Hi,
Forward and redirect difference example is good .
I have read this blog and this is really very good to read as in the example understood.
I think if every topics related to java or advance java will be written like this with example then it is easy for everybody to understand.
Thanks
In the technical scenario given above – for payment & display of result, why do we need client interaction? Correct me if I am wrong.
for example, the user does a payment by clicking Pay Now button. In most of the online payment sites, there will be a warning saying – do not press fresh button or back till you get a confirmation message – I think in this scenario, forward is happening. Please let me know whether my understanding is correct? ( email pn8572@gmail.com)
regards,
Prasanth
It is an excellent and crisp definition.thanks for such a explanation.
Really very logical real time explanation of forward & sendRedirect , great work
This site is the best among many !!
Great !
Simply superb. Nice Explanation with real world example for forward and sendredirect
Fabulous!!!! Really great thinking in real world against the technologies.
Hi..
Really too good.In many websites they just give the definition.And only in this blog i got it cleared.Thanks.
difference between forward and redirect is now crystal clear.
vvvvvvvvvvvvv good explanation with genaral example.
You rock Joe! That was really helpful :)
Awesome example…. keep up the good work!
thanks guys.