File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes sendRedirect curios behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect curios behavior" Watch "sendRedirect curios behavior" New topic
Author

sendRedirect curios behavior

narain ashwin
Ranch Hand

Joined: Dec 27, 2007
Posts: 48
Pl see the below code



I expected this code to flash "Helloworld" for a second before redirecting to "http://www.sun.com", but it is not at all redirecting.Please explain this behaviour


I drink two cappuccinos a day.Is that of any help to solve java problems??
JBoss 6 - Admin console Username/pwd: admin/admin
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 685


Hi,

This will throws exception at run time...... when you write
out.flush();
that will commit response...

It means you have already send response....

Now after that you are writing
response.sendRedirect(“http://www.sun.com”);
so you are try to access response object....

You you are to late for perform that action.... so it gives exception....


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
narain ashwin
Ranch Hand

Joined: Dec 27, 2007
Posts: 48
Thanks for the reply...It is indeed causing an exception, IllegalStateException . I haven't checked the server logs before this..again thanks a lot!
narain ashwin
Ranch Hand

Joined: Dec 27, 2007
Posts: 48
Reopening this thread for a clarification.
In the above code if I remove the out.flush() as

redirection works fine without causing any exceptions...would it cause an exception when the server automatically flushes the stream?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes. You can't redirect once output has been sent to the client.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: sendRedirect curios behavior