Wish to have some nice time? Check out what I'm reading online!

Difference between forward and sendRedirect

May 13th, 2008

forward

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

  1. If you need to transfer control to different domain
  2. 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.

Sarma on November 12th, 2009 12:06 am

Brilliant example! :)

Aatish on December 3rd, 2009 6:10 am

Very good explanations for forward and execellent example for the difference

Thank you!

Tan Ho on January 9th, 2010 12:52 am

Ha ha ha thats a gr8 example….

chandana on January 14th, 2010 3:16 pm

really good explanation for difference between forward and redirect and practical example.

Ramkrishna on January 15th, 2010 5:46 am

Very good! Thanks a lot.

mtl on February 25th, 2010 6:08 pm

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

dipak sonawane on March 18th, 2010 5:24 am

Finally, I know the difference between forward and sendRedirect :P . Thanks a lot.

Priyanka on April 3rd, 2010 8:51 pm

Nice Explanation. Thank You

sarayu on April 12th, 2010 8:08 am

Good points detailing out the differences and mentioning when to use what. Thank you.

Anonymous on April 13th, 2010 6:11 am

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

SAILESH on May 8th, 2010 11:18 am

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

Prasanth on May 20th, 2010 11:45 am

It is an excellent and crisp definition.thanks for such a explanation.

ali on May 31st, 2010 5:29 am

Really very logical real time explanation of forward & sendRedirect , great work

Jagdish Adhikari on June 2nd, 2010 1:43 am

This site is the best among many !!
Great !

Biswabrata Banerjee on June 13th, 2010 7:11 pm

Simply superb. Nice Explanation with real world example for forward and sendredirect

srinivas on June 20th, 2010 9:15 am

Fabulous!!!! Really great thinking in real world against the technologies.

Vamshidhar on June 22nd, 2010 12:10 pm

Hi..

Really too good.In many websites they just give the definition.And only in this blog i got it cleared.Thanks.

Supri on June 30th, 2010 9:01 am

difference between forward and redirect is now crystal clear.
vvvvvvvvvvvvv good explanation with genaral example.

srinivas on July 3rd, 2010 6:41 am

You rock Joe! That was really helpful :)

Praveen Kumar Jayaram on July 15th, 2010 4:36 am

Awesome example…. keep up the good work!

aryan on July 16th, 2010 3:12 pm

thanks guys.

Joe on July 16th, 2010 6:04 pm





hidden and gravatar enabled.