File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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 "", 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: 689


This will throws exception at run time...... when you write
that will commit response...

It means you have already send response....

Now after that you are writing
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

Joined: Sep 29, 2008
Posts: 12617

Yes. You can't redirect once output has been sent to the client.
I agree. Here's the link:
subject: sendRedirect curios behavior
It's not a secret anymore!