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
-
respose.sendRedirect(URL) Vs RequestDispatcher (11 messages)
- Posted by: Sabyasachi Chowdhury
- Posted on: June 07 2004 09:40 EDT
Threaded Messages (11)
- respose.sendRedirect(URL) Vs RequestDispatcher by Paul Strack on June 07 2004 15:37 EDT
- request for an example by yatish katira on May 15 2006 01:33 EDT
- Thats great by Vijaya Sravanthi on April 17 2008 16:27 EDT
- respose.sendRedirect(URL) Vs RequestDispatcher by Sabyasachi Chowdhury on June 07 2004 23:40 EDT
- respose.sendRedirect(URL) Vs RequestDispatcher by Paul Strack on June 08 2004 11:33 EDT
- And that's why... by Yoav Shapira on June 10 2004 07:52 EDT
-
respose.sendRedirect(URL) Vs RequestDispatcher by ram kol on June 11 2004 07:01 EDT
-
respose.sendRedirect(URL) Vs RequestDispatcher by Paul Strack on June 12 2004 10:19 EDT
- Re: respose.sendRedirect(URL) Vs RequestDispatcher by Kiran Gudipati on November 07 2006 02:00 EST
-
respose.sendRedirect(URL) Vs RequestDispatcher by Paul Strack on June 12 2004 10:19 EDT
- respose.sendRedirect(URL) Vs RequestDispatcher by Paul Strack on June 08 2004 11:33 EDT
- Why respose.sendRedirect(URL) Vs RequestDispatcher by Ion Dumneanu on October 21 2006 06:38 EDT
- Performance difference by Smita Lohia on June 24 2008 19:46 EDT
-
respose.sendRedirect(URL) Vs RequestDispatcher[ Go to top ]
- Posted by: Paul Strack
- Posted on: June 07 2004 15:37 EDT
- in response to Sabyasachi Chowdhury
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. -
request for an example[ Go to top ]
- Posted by: yatish katira
- Posted on: May 15 2006 01:33 EDT
- in response to Paul Strack
hi, The concept was explained well.i understood it. i request u to show an example which uses redirect technique. -
Thats great[ Go to top ]
- Posted by: Vijaya Sravanthi
- Posted on: April 17 2008 16:27 EDT
- in response to Paul Strack
Really i never read such a realtime differnces. Ur answer is great Thanks Sravi -
respose.sendRedirect(URL) Vs RequestDispatcher[ Go to top ]
- Posted by: Sabyasachi Chowdhury
- Posted on: June 07 2004 23:40 EDT
- in response to Sabyasachi Chowdhury
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 ? -
respose.sendRedirect(URL) Vs RequestDispatcher[ Go to top ]
- Posted by: Paul Strack
- Posted on: June 08 2004 11:33 EDT
- in response to Sabyasachi Chowdhury
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). -
And that's why...[ Go to top ]
- Posted by: Yoav Shapira
- Posted on: June 10 2004 07:52 EDT
- in response to Paul Strack
... 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. -
respose.sendRedirect(URL) Vs RequestDispatcher[ Go to top ]
- Posted by: ram kol
- Posted on: June 11 2004 19:01 EDT
- in response to Paul Strack
Thanks Paul.
Is it the same as --> jsp:inculde and %@ include directive. -
respose.sendRedirect(URL) Vs RequestDispatcher[ Go to top ]
- Posted by: Paul Strack
- Posted on: June 12 2004 10:19 EDT
- in response to ram kol
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. -
Re: respose.sendRedirect(URL) Vs RequestDispatcher[ Go to top ]
- Posted by: Kiran Gudipati
- Posted on: November 07 2006 14:00 EST
- in response to Paul Strack
Paul, Excellent explanation, I have been using "forward" but not sure of the exact difference between "Redirect" and "forward". Thanks a lot .. Kiran. -
Why respose.sendRedirect(URL) Vs RequestDispatcher[ Go to top ]
- Posted by: Ion Dumneanu
- Posted on: October 21 2006 06:38 EDT
- in response to Sabyasachi Chowdhury
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 -
Performance difference[ Go to top ]
- Posted by: Smita Lohia
- Posted on: June 24 2008 19:46 EDT
- in response to Sabyasachi Chowdhury
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?