Difference request.forward() and response.sendRedirect .

Discussions

Web tier: servlets, JSP, Web frameworks: Difference request.forward() and response.sendRedirect .

  1. What exactly is the difference between request.forward() and response.sendRedirect ?
    Is there a problem in managing sessions while using response.sendRedirect ?

    Threaded Messages (16)

  2. i suppose u r talking bout requestDispatcher.forward() here.

    well basically both method calls redirect u to new resource/page/servlet.

    the difference between the two is that sendRedirect always sends a header back to the client/browser. this header then contains the resource(page/servlet) which u wanted to be redirected. the browser uses this header to make another fresh request. thus sendRedirect has a overhead as to the extra remort trip being incurred. its like any other Http request being generated by ur browser. the advantage is that u can point to any resource(whether on the same domain or some other domain). for eg if sendRedirect was called at www.mydomain.com then it can also be used to redirect a call to a resource on www.theserverside.com.

    where as in case of forward() call, the above is not true. resources from the server, where the fwd. call was made, can only be requested for. but the major diff between the two is that forward just routes the request to the new resources which u specify in ur forward call. that means this route is made by the servlet engine at the server level only. no headers r sent to the browser which makes this very eficient. also the request and response objects remain the same both from where the forward call was made and the resource which was called.

    i hope i have hit ur question right.
  3. Thanks Kapil,
    Well, u have answered part of my question actually.
    The second part of my question is that whether I'd face a problem in maintaining the session for a user if I use response.sendRedirect. Is there a possibility for the session to be lost ? If so, why ?

    Kiran.
  4. This question has been answered already,check the archives.

     regards
     mani
  5. HI, i tried searching the archives, for the answer on session loss.
    Even i want to know what happens to the session when a response.sendredirect() is done.
    In my case i have a Task.jsp which is a pop up (window.open()) , on click of a "OK" button, the same Task.jsp is called but then the page shows an error saying that" Your Session is not valid or event is not passed retry after login in again ".
    While debbuging i find that the session.getValue(XXX) are null, even the request.getParameter(XXX) are null ( fine request is understandable, but session also )

    but this is working fine when i use https protocol.

    Please help.

    thanks
    Praful
  6. forgot to mention, i'm using response.sendredirect(Task.jsp?.......)
    while redirecting it.

    thx.
    C.Praful
  7. nope, theres no chance of session being lost. no way
  8. nope, theres no chance of session being lost. no way
    This is not correct, you will have to use encodeRedirectURL() when using response.serndRedirect(). otherwise URL rewriting cannot be used with browsers which do not support cookies. ans session will be lost if session is maintained using url rewriting. Response.sendRedirect example
  9. No, not all. Session object live as the browser not colsed, session timed expire,session invalidte
  10. Hi,

      I'm also facing the same problem.In my case,the request from the webserver present in India is redirected to teh EMS server present in USA.For that I'm using response.sendRediect().But the session gets expired.I tried using session.getId() to get the session of both the pages,they different.

    Any one have solution for this?
    Thanks in advance

    cheers,
    Priya
  11. this appears to only happen on windows 2000, when my code runs a response.sendRedirect(url) it crashes because immediatly after I log the the session id has changed and the session variables are gone. has anyone figured this out yet?
  12. I think the main difference is in response.sendRedirect() you need to give absolute path of the url and also request and response objects will be passed automatically to the new page. But in the case of forward method, you need to explicitly send the request and response objects to the new page explicitly. Hope I answered your question.

    Murthy
  13. when you invoke a forward request, the request is sent to the another resource on the server, without the client being informed that the another resource is going to process the request. this process occurs completely within the web-container. when a sendRedirect method is invoked, it cause the web-container to return to the web browser indicating that thenew URL should be requested. because the browser issues a new request the object that is stored as a request attributes will be lost before the redirect occurs. this extra process make redirect slower than forward.
  14. Forwarding Request Vs Re-Directing URL[ Go to top ]

    Request Forwarding: Both request parameter and session is saved URL redirect: It's a brand new request and browser cookie will be saved...you should be able to get the cookie and can retrive the session related data at the server side.( You can store session data in memory or in database at server side....but what you need is the COOKIE..and COOKIE is not lost by URL redirect!) Think like this: Forwarding the Request and Re-Directing URL are two totally different animal. Your server gets the request from the browser..job of server is to send back the response. How how that response is generated is totally up to your server..you can process that request in one servlet or can process the same request using multiple sevlet...To process request using multiple servlet, you need to pass the request to another servlet...and ofcouse we do not want to loose the parameters/data set into the request otherwise that request does not make sense anymore.....This is all request forwarding!...Request forwarding is done during one HTTP request-response cycle..Once you return the response, you are done! Now, In the response, you can tell your browser to do URL redirect to any other URL..this is HTTP header thing and browser will interprete the HTTP header and will send brand new request...
  15. I am using redirect option to send the request from my application A to a totally different application B. Once I am getting response back from application B, I need to update my database. In this situation, I need to know how to get the application A's session object to does the database action. Is this something possible, OR do I need to re-initiate application A's session object again.

  16. See this servlet redirect example It explains what is the difference between redirect and forward
  17. Thanks[ Go to top ]

    Thanks for info

    _________________

    Coffee Reviews