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
- 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.
Sarma on November 12th, 2009 12:06 amBrilliant example! :)
Aatish on December 3rd, 2009 6:10 amVery good explanations for forward and execellent example for the difference
Thank you!
Tan Ho on January 9th, 2010 12:52 amHa ha ha thats a gr8 example….
chandana on January 14th, 2010 3:16 pmreally good explanation for difference between forward and redirect and practical example.
Ramkrishna on January 15th, 2010 5:46 amVery good! Thanks a lot.
mtl on February 25th, 2010 6:08 pmawesome style to explain this scenario,
dipak sonawane on March 18th, 2010 5:24 amI 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.
Priyanka on April 3rd, 2010 8:51 pmNice Explanation. Thank You
sarayu on April 12th, 2010 8:08 amGood points detailing out the differences and mentioning when to use what. Thank you.
Anonymous on April 13th, 2010 6:11 amHi,
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 amIn 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 on May 20th, 2010 11:45 amPrasanth
It is an excellent and crisp definition.thanks for such a explanation.
ali on May 31st, 2010 5:29 amReally very logical real time explanation of forward & sendRedirect , great work
Jagdish Adhikari on June 2nd, 2010 1:43 amThis site is the best among many !!
Biswabrata Banerjee on June 13th, 2010 7:11 pmGreat !
Simply superb. Nice Explanation with real world example for forward and sendredirect
srinivas on June 20th, 2010 9:15 amFabulous!!!! Really great thinking in real world against the technologies.
Vamshidhar on June 22nd, 2010 12:10 pmHi..
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 amdifference between forward and redirect is now crystal clear.
srinivas on July 3rd, 2010 6:41 amvvvvvvvvvvvvv good explanation with genaral example.
You rock Joe! That was really helpful :)
Praveen Kumar Jayaram on July 15th, 2010 4:36 amAwesome example…. keep up the good work!
aryan on July 16th, 2010 3:12 pmthanks guys.
Joe on July 16th, 2010 6:04 pmExcellent Joe….Real world example is too good..
Srikanth S on August 19th, 2010 9:26 pmFinally, I know the difference between forward and sendRedirect :P . Thanks a lot.
nagarjuna on September 24th, 2010 10:06 amExcellent example for forward and redirect. Great Work and Great thought…Keep the good work…
Saravanan on October 27th, 2010 8:23 amExcellent 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 pmHi
shyama on December 6th, 2010 10:40 amVery good discription ;
thanks
Excellent Joe. Real world example is very clear.
Bhanumat on December 10th, 2010 5:31 amThank you for your work.
I liked the milk man example..vry short and 100% clear…
Murali on December 14th, 2010 12:34 pmWow 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 amnice example…. thank you for your excellent work…
naba on January 4th, 2011 11:59 amHello,
Pragati on January 14th, 2011 12:42 pmThanks I liked your Example.
Thanks…Nice explanation
preity on January 14th, 2011 1:29 pmThanks…… and I can understand your explanation easily
Chirtrarasan on January 18th, 2011 10:44 amI 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 amHI Joe,
I really liked the example and enjoyed learnng the concept.
vinay s salunke on January 23rd, 2011 8:49 amHaha.. Very Funny Example and very indepth explaination
Rajaram on January 25th, 2011 12:28 pmExcellent Real world example is too good..
Palmeet on January 28th, 2011 6:38 ami mean it understands very easily……….
thanks buddy…
Very good example of difference. nice..
Uttam on February 2nd, 2011 6:37 amthis 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 amVery nice article, Could you please include spring and hibernate in this site.
parvathy on February 2nd, 2011 11:02 amVery good explanation. As somebody suggests plz. update springs and hibernate in this blog.
Thanx again
Siva on February 3rd, 2011 12:51 pmThanx a lot.. This info really helped!!
Shaye on February 5th, 2011 2:49 amHI Joseph,
anbu on February 21st, 2011 10:23 amExcellent work!
thank you!
Could you please include spring,struts and hibernate in this site with real world examples!
this is very usefull for me.
venkat on March 3rd, 2011 3:35 pmexcellent example…
Anonymous on March 21st, 2011 11:11 amthis is it!!!!!!!!!!!!!!!!!!!! what many looking for as well as i.
Mukesh on March 21st, 2011 6:17 pmthanx… a lot
Magesh on March 24th, 2011 11:16 amThanks a lot for explaining the difference between forward and redirect in simple way..
Ashok on March 29th, 2011 10:01 amhi…
Rangappa on March 31st, 2011 6:41 amIts very nice to uderstand with examples…
its very nice if u explation other topic (j2ee and advance topics)….
very very nice….
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 pmThanks Joseph
Amutha on April 7th, 2011 9:43 amNice Example with good explanation
Anonymous on April 15th, 2011 5:06 pmVery good explanation…………..
Anonymous on April 18th, 2011 11:42 amThanks for good explaination with good examle
C.P.Singh on April 20th, 2011 6:20 amgd explanation….besides i demond code examples….
bsr on April 21st, 2011 8:48 pmSimply awesome !!
Anonymous on April 26th, 2011 8:37 amHi boss.
Sekahr on April 27th, 2011 4:13 pmI got a good explanation after a long time.
Thanks.
Very nice explanation…
Raju on April 28th, 2011 5:22 amthx.
very nice example,it helps me a lot
nani on April 28th, 2011 6:46 amHi,
Thank you for your excellent work.
shashi on April 30th, 2011 10:49 amSuper job!! the examples are very clear to understand the concepts
Sunil on May 3rd, 2011 3:00 amreally nice …explanation and example
Manoj on May 5th, 2011 11:37 amrocks !!!
Aarya on May 6th, 2011 12:38 pmbrilliant presense of mind
krishan on May 9th, 2011 7:32 amnice explanation, thank u very much..
abhishek on May 14th, 2011 2:44 pmits really awsm….
Jyotshna on May 16th, 2011 11:03 amthnks..
i never seen such a simple example like this for forward and redirect… thanks a lot
Udhay on May 20th, 2011 12:38 pmGood Explaination.
Kishore on May 24th, 2011 8:05 amnice work!
kuzma on May 25th, 2011 7:25 amExcellent you have given such nice explanation.The way your writing and explaining every thing perfect
sureshsp on May 27th, 2011 5:12 pmnice explanation but if you can give example of forward then it is good …thanks
Kinjan Ajudiya on May 28th, 2011 11:46 amI 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 pmGreat example !!! Nice work…
Reji on June 7th, 2011 7:01 amnice work…n a very gud example to explain the difference.
nikita on June 9th, 2011 6:48 amNice way of explaining the difference b/w forward and redirect…
Dhanunjay on June 14th, 2011 6:08 amawesome example, which makes crystal clear between forward and Redirect.
sridhar on June 17th, 2011 12:58 pmcan 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 pmnice example
balu on July 1st, 2011 11:53 amthe examples are very clear
Priya on July 5th, 2011 11:04 amgood solution
malli on July 10th, 2011 4:46 amThanks to share the good explanation with real example
C.P.Singh on July 10th, 2011 5:10 am@C.P.Singh
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)
swathi on July 12th, 2011 9:00 amhow can i get from this.
pls help me……..
kalakara kolandha
eshwar on July 14th, 2011 4:18 amIts really helpful for me to study thank you dude
eshwar on July 14th, 2011 4:21 amtheory was good thank u for that and could u please post some example programs :)
Deepak on July 14th, 2011 4:25 amvery very nice example.wonderful xplanation.
swathi chintala on July 26th, 2011 4:56 pmNicely explained… Good Job
Rama on July 28th, 2011 5:37 amOnce if we have this type of real time explanations, at that time we can’t forget the concept. . .
krishna on July 29th, 2011 5:09 amAnyhow superb explanation. . .
Very good example. Thanks
Om on July 30th, 2011 2:34 amThank you a lot. This is really very useful. keep blogging.
radhakrishna on August 10th, 2011 12:46 pmThanks again…:)
Excellent Example
Badari on August 25th, 2011 12:55 pmvery 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 pmgud example
Rashmi on September 18th, 2011 4:42 amGood example……..
Nandhini on September 23rd, 2011 10:25 amexcellent example gr8!!
Anonymous on September 25th, 2011 2:05 pmSanjay
Great….
Example is super, is it helps lot to remember the concept……
Great again
GK Than on September 26th, 2011 4:54 amIt a Good Example for ever
Anonymous on September 27th, 2011 10:19 amHi
It is Very Good Example and we can Remember easily.
Anonymous on September 27th, 2011 10:23 amWhat a funny example!
Anonymous on September 30th, 2011 9:32 amThank u very much for giving such an exellent example
Saoni De on October 8th, 2011 5:43 amasdf
adsf on October 14th, 2011 3:32 pmnice realworld example………
rajesh on October 18th, 2011 10:33 amHi 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 amThis is really a good solution. Thanks for this solution
shakti prasad rath on October 24th, 2011 3:53 amHi thanks a lot. This is really good
Anonymous on November 1st, 2011 3:52 amexample.
aaj antar samjh aa gaya forward or redirect mein …………. wonderfull example
satyendra on November 2nd, 2011 5:32 amgud work:-)))
vivek on November 4th, 2011 4:07 amsuperb explanation. . .
kondaveeti somasekhar chowdary on November 4th, 2011 5:59 amnice Example :)
Sandhya on November 9th, 2011 7:59 amGreat man….nice and very very helpful example.
Partha on November 10th, 2011 2:14 pmnice explanation(example is very good easy understandable)
Anonymous on November 10th, 2011 6:28 pmperfect example.. too good
Anonymous on November 11th, 2011 9:18 amnice work!!!!!!!!
pramiya on November 16th, 2011 3:00 amNice example.
Rajkumar.G on November 20th, 2011 4:48 amawsummmmmm
shilpa on November 22nd, 2011 9:41 amvery well example!
samina on November 22nd, 2011 7:37 pmgood explanation ,
chaitu on November 23rd, 2011 10:17 amHi,
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 on November 26th, 2011 6:10 pmAnup Shrivastava.
THIS IS AWESOME REAL TIME SCIENARIO EXAMPLE .
MANJUNATHA on November 27th, 2011 9:23 amHi Joe,
Example explained the whole concept clearly . Thanks a lot .
Praveen on November 28th, 2011 10:31 amExcellent Example
Anonymous on November 29th, 2011 6:43 amreally great work sir…
shivanand on November 30th, 2011 2:00 pmExcellent Example
Sudhir Rana on November 30th, 2011 4:43 pmgood one
Dushyant on December 1st, 2011 6:25 amreally gr8 work
Darshan on December 2nd, 2011 4:06 amExcellent differences….
Apurva on December 6th, 2011 9:14 amIts really great difference…
Yogendra on December 8th, 2011 4:25 amGreat example..thank u
Anonymous on December 9th, 2011 10:04 amPlease provide Spring concepts also..
Anonymous on December 9th, 2011 11:18 ami like ur webSite.it id really helpfull
Ganesh on December 10th, 2011 4:23 pmIt seems to be like a pictorial example. good work keep going
Muruga on December 12th, 2011 10:11 amvery nice explanation
Anonymous on December 12th, 2011 6:44 pmNice explanation with example
harjinder on December 12th, 2011 6:45 pmVery nice explanation with exaple
Deepak on December 13th, 2011 1:51 pmHello,
I am a beginner in JAVA and the concepts explained here are very good and real life examples make it even more clear.
thanks…
shilpa on December 13th, 2011 3:37 pmi am lucky to have come across this website..
its indeed really helpful…
Hi.. Nice example to understand better…:):):)
reshma begum on December 14th, 2011 6:39 amHi Jo, Very good! Thanks a lot. keep it up…
Tekuri on December 22nd, 2011 3:28 pmNice explanation
Anonymous on December 24th, 2011 9:24 amthank u …..
bhaskar on December 27th, 2011 4:37 pmWhat a brilliant example.
Thanks
Rajeev on January 2nd, 2012 7:09 amWhat a wonderful example. I like this example. Thank you so much
Dalayya on January 2nd, 2012 8:05 amHi Boss.
very thanks boss. Super example.
Kulanthaivelu on January 2nd, 2012 1:11 pmThanks….
Pramod Rajane on January 5th, 2012 1:39 pmVery Good explanation
vishwash on January 5th, 2012 2:48 pmGood Explanation
Thanks
Kavitha on January 5th, 2012 8:29 pmREALLY SUPERBBBB… No one can give example more than this…
prathip on January 9th, 2012 2:20 pmSimple & worthy explanation. Good Work!!
indu on January 10th, 2012 6:33 amtnx .
its every easy to understand forward and redirect example
pradeep on January 10th, 2012 10:47 amgood explanation with great examples… thank u..
Jeen on January 10th, 2012 12:02 pmExplained the concept clearly and in a very simple way. Good Work
Anonymous on January 12th, 2012 12:10 pmgood work..thanq
naveen on January 13th, 2012 9:01 amVery nice explanation in very simple language.
Anonymous on January 16th, 2012 11:25 amHi Joe,
That was awesome explanation with the real time senario in simple language.
Ravali on January 18th, 2012 12:59 pmhello sir..
itz really a very good explaination.. thank u so much sir..
rekha on January 19th, 2012 10:41 pmS………….. of course it’s good
Anonymous on January 21st, 2012 1:26 pmawesome example of class level and real time level.
Salim on January 23rd, 2012 7:11 pmI am really impressed by this example
Very nice explanation. Please keep posting these kind of explanations.
Senthil on January 24th, 2012 8:24 pmvery good explanation with great examples… thank u..
Ramesh on January 25th, 2012 10:41 pmgood explanation ………Thank q….
Anonymous on February 2nd, 2012 6:25 pmvery nice Explanation ,,,,,,,,,,,
Mujeeb on February 2nd, 2012 6:26 pmGood one
Muthu on February 4th, 2012 12:15 am