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.
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 https://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.
Technical scenario: redirect should be used
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.
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.
Comments are closed for "Difference between forward and sendRedirect".
Hi,
Thank you for your excellent work. I liked the example you have given here for the difference between forward and redirect.
really good explanation for difference between forward and redirect and practical example.
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.
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?
regards,
Prasanth
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.
very good explanation with general example.
You rock Joe! That was really helpful :)
Awesome example…. keep up the good work!
Finally, I know the difference between forward and sendRedirect :P . Thanks a lot.
Very good explanation. As somebody suggests plz. update springs and hibernate in this blog.
Thanx again
Thanx a lot.. This info really helped!!
HI Joseph,
Excellent work!
thank you!
Could you please include spring,struts and hibernate in this site with real world examples!
this is very usefull for me.
excellent example…
this is it!!!!!!!!!!!!!!!!!!!! what many looking for as well as i.
thanx… a lot
Thanks a lot for explaining the difference between forward and redirect in simple way..
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.
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
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.
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….
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.
Its really helpful for me to study thank you dude
theory was good thank u for that and could u please post some example programs :)
very very nice example.wonderful xplanation.
Nicely explained… Good Job
Once if we have this type of real time explanations, at that time we can’t forget the concept. . .
Anyhow superb explanation. . .
Very good example. Thanks
Thank you a lot. This is really very useful. keep blogging.
Thanks again…:)
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.
This is really a good solution. Thanks for this solution
aaj antar samjh aa gaya forward or redirect mein …………. wonderfull example
gud work:-)))
superb explanation. . .
Great man….nice and very very helpful example.
nice explanation(example is very good easy understandable)
perfect example.. too good
nice work!!!!!!!!
Nice example.
awsummmmmm
good explanation with great examples… thank u..
good work..thanq
hello sir..
itz really a very good explaination.. thank u so much sir..
awesome example of class level and real time level.
I am really impressed by this example
very good explanation with great examples… thank u..
good explanation ………Thank q….
very nice Explanation ,,,,,,,,,,,
Good one
Nice Explanation.:-)
servlet is threadsafe or not… I want to know this concept exactly.. can anyone explain this…
Hi Joe you site is super and nice explanations to be understand by anyone. Admiring your interest in Java,
excellent example but only one statement is wrong dear that is “Control can be redirect to resources to different servers ” in case of sendredirect we can forward the request to another web-app but they should run under the same server only.
I loved the real world example.. good one!!
Very catchy and interesting real world example. Thanks Joe
Joe, u rocks …love dis blog ….keep up ur enthusiasm and good work …
Thanks guys for all your appreciations.
does browser retains Cookies when redirecting to another domain?
e.g. if Domain A sets a cookie in response before doing redirect to Domain B, will browser retain that coookie while creating new request for Domain B?
hi,i have a doubt to use send Redirect method.
when i implimented redirect method in my action class , how i hide parameters from users.
I have wonder your statement:
‘session’ is not lost in both forward and redirect.
Because I know session will be lost in redirect.
Please correct me if i am wrong .
The real time example cited here makes to understand the whole concepts.
Excellent Example…!!!! now only i have got clarity between forward and sendRedirect techniques…!!! thanks a lot…!!!
Excellent Example
gr8 example….
good explanation
you awesome man….brilliant explanation.
khasim on july 30th,2012 2:28 am.
you awesome man….brilliant explanation.
Nice explanation and good example too.
very nice example.great one too
Milk man example are very good..
thank u very much..
it is very clear about it.
thanks a lot joe , really you have given great example which can help anyone….
Very nice explanation!!!!
Thank you Joe :-)
Example of Dudhwala is awesome:) Please add some tutorial on jQuery, if you are expertise in it.
you are simply superb :)
Very good Explanation…
good example
explanation was very clear
Nice Explanation. Thanks a lot.
Really nice explanation .
Hi Joe, Nice examples you have given as well as explained. Your IRCTC example is also perfect. This also happens when we use Paypal Payment Gateway or similar. Good job!! Keep it up!! :)
this is the best tutorial for all the beginners who have doubts in forward and sendRedirect…
thank u very much….
Cool stuff! Really liked the milk man example :-)
Very good explaination with real world examples.Good job.
nice example
I have a query.
I have a jsp with all costants.I want to use those value in a servlet(which calls by the web.xml at the loading the server).
How can I achive this?
Good
Really good explanation
Hi Amigo,
really good example and simple to understand.
A good one with an apt example..!!
Nice example
Nice description about the difference between forward and redirect. Can u plz help me to get information and working about collections in java from basics. If u have any materials for that, Plz send me to my mail ID mentioned here. Please do helpfull. Thanks in advance..
nice example!! thanksss :-)
mast………but i m expecting one example program for forward and request….
Your examples are awesome. I liked it no no I admired it
Joe.. You have explained it very well..
Really a very good explanation..with real world and practical scenarios.
Thanks!
There is an attribute of HTTP Header called referer (actual spelling referrer) which will always keep track of the last URL from where the request is being made.
Servlet is not thread-safe by itself because of performance overhead. Prior to Servlet 2.0 to make it thread-safe you need to implement
SingleThreadModel interface but its deprecated and can be removed from future version.
You can make your Servlet thread-safe by making the service method synchronized.
Hello Joe,
The example given to this topic is good. Many of them who do programming does not know which one to access because they have not been given the scenario examples. Even in my days of going to java course they didn`t think of giving the examples simply they said this is the syntax, you need to use like this. How can one understand when? where? how? why? to use the concepts of prgmming language. It is best desirable to give examples rather than teaching long hour periods. I think you have done a great job and if possible give examples like the said above to all the concepts blogged in this website.
Awesome Example. Got clear information about sendRedirect() and forward() methods. Thanks alot :)
Good Real world example :)
very good and impressive example.
In forward() the servlet-1 forward the request to another servlet-2.the second servlet-2 to whom u forward the request ll send the response to the request not the servlet-1.
Just wish to tell u that the real world example was great.. Got the idea from the example.
examples gives brief idea.
Thanks
Nice explanation and awesome example ha ha ha..
Hi Joe,
Really, you had posted a Excellent topic
The touchy part of the article was the Milk Man example.
Thanks a Lot for your Effort.
Adi
Prashant,In the scenario of the online payment when the sites give a warning saying “Please do not press the back or the refresh button” they are using redirect not the forward.Because once the payment is successful then again its getting redirected to a different page which is showing the payment successful information.
Hi Prasanth,
In the payment and Displayinfo once fill informaion on merchent page(info)related once we presss the submit button then internaly it will goto payment site if u want conformation see URL(exp: flipcart==> to anybanksite) then in this time it will show dont press refresh button or back bcz if u prsss payment is not completed so transaction is not completed so transaction is rollback bcz of that it will show error page. once payment is sucesses then it redirect to merchentsite.now if u press refress button it wil show only displayinfo will refreshed but it wil not called payment.
Sir,
i want to know which one is faster in execution forward() or redirect ()?