Difference between forward and sendRedirect

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.

Ads by Google

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.

This JSP tutorial was added on 13/05/2008.

«

»

Comments on "Difference between forward and sendRedirect"

  1. Sarma says:

    Hi,

    Thank you for your excellent work. I liked the example you have given here for the difference between forward and redirect.

  2. Ramkrishna says:

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

  3. dipak sonawane says:

    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

  4. Priyanka says:

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

  5. SAILESH says:

    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

  6. Prasanth says:

    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

    • Gopal Rao says:

      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.

    • Swaraj says:

      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.

  7. Jagdish Adhikari says:

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

  8. Biswabrata Banerjee says:

    This site is the best among many !!
    Great !

  9. srinivas says:

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

  10. Vamshidhar says:

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

  11. Supri says:

    Hi..

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

  12. srinivas says:

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

  13. Praveen Kumar Jayaram says:

    You rock Joe! That was really helpful :)

  14. aryan says:

    Awesome example…. keep up the good work!

  15. nagarjuna says:

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

  16. Siva says:

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

    Thanx again

  17. Shaye says:

    Thanx a lot.. This info really helped!!

  18. anbu says:

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

  19. venkat says:

    this is very usefull for me.

  20. Anonymous says:

    excellent example…

  21. Mukesh says:

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

  22. Magesh says:

    thanx… a lot

  23. Ashok says:

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

  24. Priya J says:

    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.

    • Niraj says:

      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.

  25. elangovan says:

    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.

  26. Joe says:

    @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.

  27. Nitin says:

    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….

  28. swathi says:

    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……..

  29. eshwar says:

    Its really helpful for me to study thank you dude

  30. Deepak says:

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

  31. swathi chintala says:

    very very nice example.wonderful xplanation.

  32. Rama says:

    Nicely explained… Good Job

  33. krishna says:

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

  34. Om says:

    Very good example. Thanks

  35. radhakrishna says:

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

  36. Naveen Sharma says:

    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.

  37. shakti prasad rath says:

    This is really a good solution. Thanks for this solution

  38. satyendra says:

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

  39. vivek says:

    gud work:-)))

  40. kondaveeti somasekhar chowdary says:

    superb explanation. . .

  41. Partha says:

    Great man….nice and very very helpful example.

  42. Anonymous says:

    nice explanation(example is very good easy understandable)

  43. Anonymous says:

    perfect example.. too good

  44. pramiya says:

    nice work!!!!!!!!

  45. Rajkumar.G says:

    Nice example.

  46. shilpa says:

    awsummmmmm

  47. Jeen says:

    good explanation with great examples… thank u..

  48. naveen says:

    good work..thanq

  49. rekha says:

    hello sir..

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

  50. Salim says:

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

  51. Ramesh says:

    very good explanation with great examples… thank u..

  52. Anonymous says:

    good explanation ………Thank q….

  53. Mujeeb says:

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

  54. Muthu says:

    Good one

  55. jagan says:

    Nice Explanation.:-)

  56. Anonymous says:

    servlet is threadsafe or not… I want to know this concept exactly.. can anyone explain this…

    • Niraj says:

      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.

  57. Santhosh Kumar R says:

    Hi Joe you site is super and nice explanations to be understand by anyone. Admiring your interest in Java,

  58. gaurav says:

    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.

  59. hiren says:

    I loved the real world example.. good one!!

  60. Sangeetha says:

    Very catchy and interesting real world example. Thanks Joe

  61. FIroj says:

    Joe, u rocks …love dis blog ….keep up ur enthusiasm and good work …

  62. neel says:

    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?

  63. Hari says:

    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.

  64. Minh says:

    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 .

  65. dhanya says:

    The real time example cited here makes to understand the whole concepts.

  66. Surya says:

    Excellent Example…!!!! now only i have got clarity between forward and sendRedirect techniques…!!! thanks a lot…!!!

  67. Nilesh says:

    Excellent Example

  68. chittaranjan padhi says:

    gr8 example….

  69. prasadreddy says:

    good explanation

  70. khasim says:

    you awesome man….brilliant explanation.

    khasim on july 30th,2012 2:28 am.

  71. khasim says:

    you awesome man….brilliant explanation.

  72. Gopal Gowda says:

    Nice explanation and good example too.

  73. Manikandan says:

    very nice example.great one too

  74. Aman says:

    Milk man example are very good..
    thank u very much..

  75. ram says:

    it is very clear about it.

  76. akhilesh says:

    thanks a lot joe , really you have given great example which can help anyone….

  77. Anonymous says:

    Very nice explanation!!!!
    Thank you Joe :-)

  78. Bhushan says:

    Example of Dudhwala is awesome:) Please add some tutorial on jQuery, if you are expertise in it.

  79. Anand says:

    you are simply superb :)

  80. Anonymous says:

    Very good Explanation…

  81. Anonymous says:

    good example

  82. suma says:

    explanation was very clear

  83. fsffd says:

    Nice Explanation. Thanks a lot.

  84. Swati says:

    Really nice explanation .

  85. Tarun S. Pandey says:

    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!! :)

  86. Jayesh Kalkani says:

    this is the best tutorial for all the beginners who have doubts in forward and sendRedirect…
    thank u very much….

  87. Vish says:

    Cool stuff! Really liked the milk man example :-)

  88. Snehit says:

    Very good explaination with real world examples.Good job.

  89. Manoj Patil says:

    nice example

  90. Venkat says:

    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?

  91. Jaganath says:

    Good

  92. Priyam says:

    Hi Amigo,

    really good example and simple to understand.

  93. vignesh says:

    A good one with an apt example..!!

  94. sudarshan says:

    Nice example

  95. chethan says:

    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..

  96. pradeep says:

    nice example!! thanksss :-)

  97. abhishek says:

    mast………but i m expecting one example program for forward and request….

  98. Sathish Kumar Mani says:

    Your examples are awesome. I liked it no no I admired it

  99. Yash says:

    Joe.. You have explained it very well..

  100. Dharmendra says:

    Really a very good explanation..with real world and practical scenarios.

    Thanks!

  101. Manoj says:

    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.

  102. Venky says:

    Awesome Example. Got clear information about sendRedirect() and forward() methods. Thanks alot :)

  103. Manikantan says:

    Good Real world example :)

  104. keval says:

    very good and impressive example.

  105. shakti says:

    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.

  106. Nithin J Pathalil says:

    Just wish to tell u that the real world example was great.. Got the idea from the example.

  107. Anonymous says:

    examples gives brief idea.
    Thanks

  108. Rajasekhar says:

    Nice explanation and awesome example ha ha ha..

  109. Aditya says:

    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

  110. Ashwini Kumar Patro says:

    Sir,
    i want to know which one is faster in execution forward() or redirect ()?

Comments are closed.