aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes response.sendRedirect vs. request.getRequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "response.sendRedirect vs. request.getRequestDispatcher" Watch "response.sendRedirect vs. request.getRequestDispatcher" New topic
Author

response.sendRedirect vs. request.getRequestDispatcher

Jason Stortz
Ranch Hand

Joined: Jan 11, 2001
Posts: 68
Okay, I'm using Tomcat 4.0.4. I'm having a hard time deciding when to use response.sendRedirect() and when to use request.getRequestDispatcher(someURL).forward(request, response).
We used to use response.sendRedirect all over the place, but that started failing once we moved from iPlanet 4.1 to Tomcat. I need to redirect the user OFF our site to another site though, so the forward method of RequestDispatcher doesn't work since it has to be a relative path.
Anyone got any hard and fast rules here? response.sendRedirect only seems to work some of the time, and I cannot tell why.
Any advice would be awesome.
Thanks,
Jason
raj sekar
Ranch Hand

Joined: Mar 11, 2002
Posts: 79
Hi Jason,
We should keep in mind a couple of important points about the sendRedirect()
method.
* We cannot call this method if the response is committed,
that is, if the response header has already been sent to the browser.
If we do, the method will throw a java.lang.IllegalStateException.

For ex:
public void doGet(HttpServletReqeust req, HttpServletResponse res)
{
PrintWriter pw = res.getPrintWriter();
pw.println("<html><body> Hello World </body></html>");
pw.flush(); // send the response
res.sendRedirect("http://www.javaranch.com"); // Tries to Redirect
}

Thanks,
Raj


Thanks,
RajN - (SCJP,SCWCD,SCBCD,SCSA,IBM XML 142 )
http://www.geocities.com/n_rajasekar/index.html
You do what you love, love what you do and always deliver more than you promise
Paul McKenna
Ugly Redneck
Ranch Hand

Joined: Jul 08, 2000
Posts: 1006
Another point worthy of keeping in mind is that if you use the response.sendRedirect method your request parameters will be lost. However, the requestDispatcher.forward retains these parameters and makes them available to the called servlet/jsp.
SR


Commentary From the Sidelines of history
ticco scott
Greenhorn

Joined: Dec 10, 2004
Posts: 1
This is in reference to your comments on sendRedirect()---
* We cannot call this method if the response is committed,
that is, if the response header has already been sent to the browser.
If we do, the method will throw a java.lang.IllegalStateException.

For ex:
public void doGet(HttpServletReqeust req, HttpServletResponse res)
{
PrintWriter pw = res.getPrintWriter();
pw.println("<html><body> Hello World </body></html>");
pw.flush(); // send the response
res.sendRedirect("http://www.javaranch.com"); // Tries to Redirect
}


We are running into similar issue with requestDispathcer.forward() too (i.e. the output was partially written out to the buffer before making the call to forward )
I was under the impression that with server side forward this should not happen ???

What are your comments on this??

Plz respond.


-Thanx,
ticco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: response.sendRedirect vs. request.getRequestDispatcher