This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendRedirect curios behavior

 
narain ashwin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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....

 
narain ashwin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You can't redirect once output has been sent to the client.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic