Difference between forward and sendRedirect

13/05/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

Excellent Joe….Real world example is too good..

Srikanth S on August 19th, 2010 9:26 pm

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

nagarjuna on September 24th, 2010 10:06 am

Excellent example for forward and redirect. Great Work and Great thought…Keep the good work…

Saravanan on October 27th, 2010 8:23 am

Excellent work!!! really it will help lots of people who are trying to understand forward and sendRedirect….really great job….even the last example is awesome.

Saubhik Aon on November 30th, 2010 1:11 pm

Hi
Very good discription ;
thanks

shyama on December 6th, 2010 10:40 am

Excellent Joe. Real world example is very clear.
Thank you for your work.

Bhanumat on December 10th, 2010 5:31 am

I liked the milk man example..vry short and 100% clear…

Murali on December 14th, 2010 12:34 pm

Wow This is very helpful to understand the core concept of forward and sendRedirect. The examples above gave very clear understanding of the concepts. Keep posting lots of topics.

Bala on December 16th, 2010 9:58 am

nice example…. thank you for your excellent work…

naba on January 4th, 2011 11:59 am

Hello,
Thanks I liked your Example.

Pragati on January 14th, 2011 12:42 pm

Thanks…Nice explanation

preity on January 14th, 2011 1:29 pm

Thanks…… and I can understand your explanation easily

Chirtrarasan on January 18th, 2011 10:44 am

I find this site to be unlike from the others. I would like to appreciate your work & passion…simply superb!!!

arvind on January 22nd, 2011 7:09 am

HI Joe,

I really liked the example and enjoyed learnng the concept.

vinay s salunke on January 23rd, 2011 8:49 am

Haha.. Very Funny Example and very indepth explaination

Rajaram on January 25th, 2011 12:28 pm

Excellent Real world example is too good..
i mean it understands very easily……….
thanks buddy…

Palmeet on January 28th, 2011 6:38 am

Very good example of difference. nice..

Uttam on February 2nd, 2011 6:37 am

this is very nice explanation example of forwad and senddirect.is there any other type of examples are there.pls forward to my mail.thanks

veeraswamy on February 2nd, 2011 8:18 am

Very nice article, Could you please include spring and hibernate in this site.

parvathy on February 2nd, 2011 11:02 am

Very good explanation. As somebody suggests plz. update springs and hibernate in this blog.

Thanx again

Siva on February 3rd, 2011 12:51 pm

Thanx a lot.. This info really helped!!

Shaye on February 5th, 2011 2:49 am

HI Joseph,
Excellent work!
thank you!
Could you please include spring,struts and hibernate in this site with real world examples!

anbu on February 21st, 2011 10:23 am

this is very usefull for me.

venkat on March 3rd, 2011 3:35 pm

excellent example…

Anonymous on March 21st, 2011 11:11 am

this is it!!!!!!!!!!!!!!!!!!!! what many looking for as well as i.

Mukesh on March 21st, 2011 6:17 pm

thanx… a lot

Magesh on March 24th, 2011 11:16 am

Thanks a lot for explaining the difference between forward and redirect in simple way..

Ashok on March 29th, 2011 10:01 am

hi…
Its very nice to uderstand with examples…
its very nice if u explation other topic (j2ee and advance topics)….
very very nice….

Rangappa on March 31st, 2011 6:41 am

I could understand your example very well. But there is a confusion with a real time situation that I’m thinking about.

If you go to irctc.co.in for payment it will redirect you to netbanking application. At that time since this is a redirect it’s a new request and the old req/resp will be lost. But once payment is done it is going back to the irctc.co.in. Where this url is preserved? in the session?

It would be very helpful if you clarify the situation mentioned above.

Priya J on April 1st, 2011 1:55 pm

Thanks Joseph

Amutha on April 7th, 2011 9:43 am

Nice Example with good explanation

Anonymous on April 15th, 2011 5:06 pm

Very good explanation…………..

Anonymous on April 18th, 2011 11:42 am

Thanks for good explaination with good examle

C.P.Singh on April 20th, 2011 6:20 am

gd explanation….besides i demond code examples….

bsr on April 21st, 2011 8:48 pm

Simply awesome !!

Anonymous on April 26th, 2011 8:37 am

Hi boss.
I got a good explanation after a long time.
Thanks.

Sekahr on April 27th, 2011 4:13 pm

Very nice explanation…
thx.

Raju on April 28th, 2011 5:22 am

very nice example,it helps me a lot

nani on April 28th, 2011 6:46 am

Hi,

Thank you for your excellent work.

shashi on April 30th, 2011 10:49 am

Super job!! the examples are very clear to understand the concepts

Sunil on May 3rd, 2011 3:00 am

really nice …explanation and example

Manoj on May 5th, 2011 11:37 am

rocks !!!

Aarya on May 6th, 2011 12:38 pm

brilliant presense of mind

krishan on May 9th, 2011 7:32 am

nice explanation, thank u very much..

abhishek on May 14th, 2011 2:44 pm

its really awsm….
thnks..

Jyotshna on May 16th, 2011 11:03 am

i never seen such a simple example like this for forward and redirect… thanks a lot

Udhay on May 20th, 2011 12:38 pm

Good Explaination.

Kishore on May 24th, 2011 8:05 am

nice work!

kuzma on May 25th, 2011 7:25 am

Excellent you have given such nice explanation.The way your writing and explaining every thing perfect

sureshsp on May 27th, 2011 5:12 pm

nice explanation but if you can give example of forward then it is good …thanks

Kinjan Ajudiya on May 28th, 2011 11:46 am

I could understand your example very well. But there is a confusion with a real time situation that I’m thinking about.

If you go to irctc.co.in for payment it will redirect you to netbanking application. At that time since this is a redirect it’s a new request and the old req/resp will be lost. But once payment is done it is going back to the irctc.co.in. Where this url is preserved? in the session?

It would be very helpful if you clarify the situation mentioned above.

elangovan on June 3rd, 2011 8:25 am

@elangovan
Thats a very nice question relating to the context.

Here this is a redirect and there is no doubt about it. irctc (web application) is integrated with the payment gateway. While integrating, there will be provision to set the callback url and the list of arguments that needs to be passed back to the web application. So, once the payment is done, the payment gateway will call the registered url (in this case irctc). So there is no need to preserve the url in session. The call will be done by payment gateway.

Some additional information:

Internally there will be another url involved called notify url. On payment, this notify url (in the irctc web application) will be called along with status variables. This will happen in a notify-acknowledgement mode. Using the status variables, webapplication (irctc) can update its database or execute process like sending an email (ticket) to the user.

Joe on June 3rd, 2011 1:42 pm

Great example !!! Nice work…

Reji on June 7th, 2011 7:01 am

nice work…n a very gud example to explain the difference.

nikita on June 9th, 2011 6:48 am

Nice way of explaining the difference b/w forward and redirect…

Dhanunjay on June 14th, 2011 6:08 am

awesome example, which makes crystal clear between forward and Redirect.

sridhar on June 17th, 2011 12:58 pm

can anybody tell me where the request and response objects will go in both the cases

what i think about
-redirect
that in this case previous req n res will be destroyed when control is transferred to another page

-forward
req n res will transferred to another page along with the control

n pls tell me about the include also.. i think in this case previous req n res comes back to the same page after transferring the control

please correct me if am wrong….

Nitin on June 20th, 2011 7:19 pm

nice example

balu on July 1st, 2011 11:53 am

the examples are very clear

Priya on July 5th, 2011 11:04 am

good solution

malli on July 10th, 2011 4:46 am

Thanks to share the good explanation with real example
@C.P.Singh

C.P.Singh on July 10th, 2011 5:10 am

Hi Joseph,

When i click redirect in jsp it will go to struts and will call funtion redirect i..,
public void redirect() throws IOException, ServletException
{
HttpServletResponse res = null;
res.sendRedirect(“www.google.co.in”);

}

here i want to redirect(i want to change the url in address bar using sendredirect) but it is throwing nullpointer exception)
how can i get from this.
pls help me……..

swathi on July 12th, 2011 9:00 am

kalakara kolandha

eshwar on July 14th, 2011 4:18 am

Its really helpful for me to study thank you dude

eshwar on July 14th, 2011 4:21 am

theory was good thank u for that and could u please post some example programs :)

Deepak on July 14th, 2011 4:25 am

very very nice example.wonderful xplanation.

swathi chintala on July 26th, 2011 4:56 pm

Nicely explained… Good Job

Rama on July 28th, 2011 5:37 am

Once if we have this type of real time explanations, at that time we can’t forget the concept. . .
Anyhow superb explanation. . .

krishna on July 29th, 2011 5:09 am

Very good example. Thanks

Om on July 30th, 2011 2:34 am

Thank you a lot. This is really very useful. keep blogging.
Thanks again…:)

radhakrishna on August 10th, 2011 12:46 pm

Excellent Example

Badari on August 25th, 2011 12:55 pm

very very good and clear difference b/w both of them and with a good example

Thanks very much…for sharing these things….!!!!

anonymous on September 13th, 2011 5:15 pm

gud example

Rashmi on September 18th, 2011 4:42 am

Good example……..

Nandhini on September 23rd, 2011 10:25 am

excellent example gr8!!
Sanjay

Anonymous on September 25th, 2011 2:05 pm

Great….

Example is super, is it helps lot to remember the concept……

Great again

GK Than on September 26th, 2011 4:54 am

It a Good Example for ever

Anonymous on September 27th, 2011 10:19 am

Hi

It is Very Good Example and we can Remember easily.

Anonymous on September 27th, 2011 10:23 am

What a funny example!

Anonymous on September 30th, 2011 9:32 am

Thank u very much for giving such an exellent example

Saoni De on October 8th, 2011 5:43 am

asdf

adsf on October 14th, 2011 3:32 pm

nice realworld example………

rajesh on October 18th, 2011 10:33 am

Hi Joseph,
I appreciate your initiative for the beginners help,Your blog is very much helpful for me to remove confusion about this topic . you right great example of payment process . It is very much practical example i have found .

Thanx allot.

Naveen Sharma on October 20th, 2011 10:06 am

This is really a good solution. Thanks for this solution

shakti prasad rath on October 24th, 2011 3:53 am

Hi thanks a lot. This is really good
example.

Anonymous on November 1st, 2011 3:52 am

aaj antar samjh aa gaya forward or redirect mein …………. wonderfull example

satyendra on November 2nd, 2011 5:32 am

gud work:-)))

vivek on November 4th, 2011 4:07 am

superb explanation. . .

kondaveeti somasekhar chowdary on November 4th, 2011 5:59 am

nice Example :)

Sandhya on November 9th, 2011 7:59 am

Great man….nice and very very helpful example.

Partha on November 10th, 2011 2:14 pm

nice explanation(example is very good easy understandable)

Anonymous on November 10th, 2011 6:28 pm

perfect example.. too good

Anonymous on November 11th, 2011 9:18 am

nice work!!!!!!!!

pramiya on November 16th, 2011 3:00 am

Nice example.

Rajkumar.G on November 20th, 2011 4:48 am

awsummmmmm

shilpa on November 22nd, 2011 9:41 am

very well example!

samina on November 22nd, 2011 7:37 pm

good explanation ,

chaitu on November 23rd, 2011 10:17 am

Hi,

Thanks Joe.

I have gone through most to the link to get my answer ‘to the point’ but i never got my answer up to satisfactory label.
In this link i got my answer ‘to the point’ with wonderful example.
Thank you Joe once again. Please continue as long as possible.

Thanks & Regards
Anup Shrivastava.

Anup on November 26th, 2011 6:10 pm

THIS IS AWESOME REAL TIME SCIENARIO EXAMPLE .

MANJUNATHA on November 27th, 2011 9:23 am

Hi Joe,

Example explained the whole concept clearly . Thanks a lot .

Praveen on November 28th, 2011 10:31 am

Excellent Example

Anonymous on November 29th, 2011 6:43 am

really great work sir…

shivanand on November 30th, 2011 2:00 pm

Excellent Example

Sudhir Rana on November 30th, 2011 4:43 pm

good one

Dushyant on December 1st, 2011 6:25 am

really gr8 work

Darshan on December 2nd, 2011 4:06 am

Excellent differences….

Apurva on December 6th, 2011 9:14 am

Its really great difference…

Yogendra on December 8th, 2011 4:25 am

Great example..thank u

Anonymous on December 9th, 2011 10:04 am

Please provide Spring concepts also..

Anonymous on December 9th, 2011 11:18 am

i like ur webSite.it id really helpfull

Ganesh on December 10th, 2011 4:23 pm

It seems to be like a pictorial example. good work keep going

Muruga on December 12th, 2011 10:11 am

very nice explanation

Anonymous on December 12th, 2011 6:44 pm

Nice explanation with example

harjinder on December 12th, 2011 6:45 pm

Very nice explanation with exaple

Deepak on December 13th, 2011 1:51 pm

Hello,
I am a beginner in JAVA and the concepts explained here are very good and real life examples make it even more clear.

thanks…
i am lucky to have come across this website..
its indeed really helpful…

shilpa on December 13th, 2011 3:37 pm

Hi.. Nice example to understand better…:):):)

reshma begum on December 14th, 2011 6:39 am

Hi Jo, Very good! Thanks a lot. keep it up…

Tekuri on December 22nd, 2011 3:28 pm

Nice explanation

Anonymous on December 24th, 2011 9:24 am

thank u …..

bhaskar on December 27th, 2011 4:37 pm

What a brilliant example.

Thanks

Rajeev on January 2nd, 2012 7:09 am

What a wonderful example. I like this example. Thank you so much

Dalayya on January 2nd, 2012 8:05 am

Hi Boss.

very thanks boss. Super example.

Kulanthaivelu on January 2nd, 2012 1:11 pm

Thanks….

Pramod Rajane on January 5th, 2012 1:39 pm

Very Good explanation

vishwash on January 5th, 2012 2:48 pm

Good Explanation

Thanks

Kavitha on January 5th, 2012 8:29 pm

REALLY SUPERBBBB… No one can give example more than this…

prathip on January 9th, 2012 2:20 pm

Simple & worthy explanation. Good Work!!

indu on January 10th, 2012 6:33 am

tnx .

its every easy to understand forward and redirect example

pradeep on January 10th, 2012 10:47 am

good explanation with great examples… thank u..

Jeen on January 10th, 2012 12:02 pm

Explained the concept clearly and in a very simple way. Good Work

Anonymous on January 12th, 2012 12:10 pm

good work..thanq

naveen on January 13th, 2012 9:01 am

Very nice explanation in very simple language.

Anonymous on January 16th, 2012 11:25 am

Hi Joe,

That was awesome explanation with the real time senario in simple language.

Ravali on January 18th, 2012 12:59 pm

hello sir..

itz really a very good explaination.. thank u so much sir..

rekha on January 19th, 2012 10:41 pm

S………….. of course it’s good

Anonymous on January 21st, 2012 1:26 pm

awesome example of class level and real time level.
I am really impressed by this example

Salim on January 23rd, 2012 7:11 pm

Very nice explanation. Please keep posting these kind of explanations.

Senthil on January 24th, 2012 8:24 pm

very good explanation with great examples… thank u..

Ramesh on January 25th, 2012 10:41 pm

good explanation ………Thank q….

Anonymous on February 2nd, 2012 6:25 pm

very nice Explanation ,,,,,,,,,,,

Mujeeb on February 2nd, 2012 6:26 pm

Good one

Muthu on February 4th, 2012 12:15 am


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home