File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes sendRedirect(Url) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "sendRedirect(Url) " Watch "sendRedirect(Url) " New topic
Author

sendRedirect(Url)

Salima Lalani
Ranch Hand

Joined: Aug 15, 2002
Posts: 59
Hi All ,
I came across following example from Hanumant's book
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{

PrintWriter pw = res.getWriter();
pw.println("<html><body>HelloWorld</body></html>");
pw.flush();
res.sendRedirect("http://www.cnn.com");

}
The explanation of example says that since the response header are always sent to the browser, so this method will throw IllegalStateException.
When i run this code, it worked fine, except it didn't redirect to the url. If i comment the flush() , then it redirects me to cnn.com.
I don't understand what is meant by committed response. When the above example will throw exception.
Will appreciate your kind help.
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Hi Salima
The response is being committed when pw.flush() executes. ie A res.isCommitted() immediately afterwards will return true.
Also, the res.sendRedirect("...") does throw an IllegalStateException. You can test this in a try...catch clause.
So.... the Servlet side of things is working as expected. I'm guessing that the browser doesn't care about the exception and accepts the state of the response at the time it was committed. You would probably need to catch the exception and use sendError() to display a meaningful error on the browser.
Hope this helps
Mark.
Salima Lalani
Ranch Hand

Joined: Aug 15, 2002
Posts: 59
Hello Mark,
I tried working as you said. But it is not throwing an exception. Pls post your code so that i can understand.
Thanks a lot!!!
Salima
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Salima
After pw.flush(), code the following try...catch:

Your output should include: "An IllegalStateException occurred"
Mark.
Salima Lalani
Ranch Hand

Joined: Aug 15, 2002
Posts: 59
Hi Mark,
I got it.
Thanks
Salima
vn us
Ranch Hand

Joined: Dec 27, 2002
Posts: 33
To continue on this question
Can i set a Header say "name" as the header name and value is "MYNAME" in the response object and call the response.sendRedirect( url ) and Can i access the same header ( "name" ) in the redirected url?
Thanks,
AVNUS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect(Url)
 
Similar Threads
What is the difference between Submit and Foward in jsp
RequestDispatcher
Why the exception in my error page is null when error comes from a servlet?
sendRedirect()
How servlet response is commited?