Discussions

Web tier: servlets, JSP, Web frameworks: respose.sendRedirect(URL) Vs RequestDispatcher

  1. Hi

    In a servlet when I use response.sendRedirect() and then if I put some state ment like one System.out.println(); after that line of code it gets executed...it works same for RequestDipsatcher also.

    My question is : The control is passed to the next URL only after the complete execution of the whole service code ? The what is the difference between this two other than one is client side and the other is server side ?

    Thanks
    Sabyasachi Chowdhury
  2. What is the difference between this two other than one is client side and the other is server side?
    That is the key difference, but this has some important implications:

    1) If you use a RequestDispatcher, the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute(). With a sendRedirect(), it is a new request from the client, and the only way to pass data is through the session or with web parameters (url?name=value).

    2) A sendRedirect() also updates the browser history. Suppose you have JSP-1 which has a form that targets Servlet-2, which then redirects to JSP-3. With a redirect, the user's address bar will read "http://[host]/JSP-3". If the user clicks the Reload/Refresh button, only JSP-3 will be re-executed, not Servlet-2.

    If you use a RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read "http://[host]/Servlet-2". A reload/refresh will execute both Servlet-2 and JSP-3. This can be important if Servlet-2 performs some system update (such as credit-card processing).

    Both kinds of redirections are useful, depending on the precise effect you want.
  3. request for an example[ Go to top ]

    hi, The concept was explained well.i understood it. i request u to show an example which uses redirect technique.
  4. Thats great[ Go to top ]

    Really i never read such a realtime differnces. Ur answer is great Thanks Sravi
  5. That was awsome !!! And the way U explained it also is fantastic....thanks a lot.

    I understood what ever u said but in case of both redirect() and RequestDispatcher when is the control forwared to the next Servlet/JSP only when the execution of the whole service method compltes or when that line is hit ?
  6. I understood what ever u said but in case of both redirect() and RequestDispatcher when is the control forwared to the next Servlet/JSP only when the execution of the whole service method compltes or when that line is hit?
    It depends. For sendRedirect(), control is transferred when the whole service method completes. For RequestDispatcher, control is transferred immediately (when you call the forward() method).
  7. And that's why...[ Go to top ]

    ... we tell people to put a return statement right after the response.sendRedirect statement. That prevents some hard to debug errors that result from the programmer thinking sendRedirect is an immediate exit from the method.
  8. Thanks Paul.

    Is it the same as --> jsp:inculde and %@ include directive.
  9. Is it the same as --> jsp:include and %@ include directive.
    Actually, <jsp:include> vs. <%@ include %> is a different story.

    <%@ include %> is a "compile-time" include. The included file is inserted into the JSP when the JSP compiles.

    <jsp:include> is a "run-time" include. If JSP-1 includes JSP-2, JSP-2 is invoked when JSP-1 executes, and the output of JSP-2 is inserted into the output stream of JSP-1.

    The run-time include runs a bit slowly, but can save a lot of system memory, so in general, the run-time include is preferable. Only use the compile-time include if the include code must be in the same page.

    For example, if you want to use an include that defines a bunch of tag libraries with the taglib directive, it will have to be a compile-time include. If you just want to insert a standard header and footer in your page, a run-time include will be more effecient.
  10. Paul, Excellent explanation, I have been using "forward" but not sure of the exact difference between "Redirect" and "forward". Thanks a lot .. Kiran.
  11. Hi

    In a servlet when I use response.sendRedirect() and then if I put some state ment like one System.out.println(); after that line of code it gets executed...it works same for RequestDipsatcher also.

    My question is : The control is passed to the next URL only after the complete execution of the whole service code ? The what is the difference between this two other than one is client side and the other is server side ?

    Thanks
    Sabyasachi Chowdhury
  12. Performance difference[ Go to top ]

    Hi Paul, That was an excellend explanation. I wanted to know if there is any significant performance overhead in using sendRedirect over requestDispatcher. Usually we use sendRedirect if we have to change the context to other JVM or in case we want to use an external URL. But still if I want to use sendRedirect even if I have to retain the same context, is it a good practice?