Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendRedirect problem!

 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First,I want to show "hello" on browser,
Next,redirect to www.yahoo.com
but,the browser shows only "hello",don't redirect to www.yahoo.com.
If del out.flush(),browser can redirect to www.yahoo.com.But don't show "hello".
who can tell me, why?
thanks.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be using version 2.1 of the servlet spec. The printwriter output is not buffered, so when you called flush(), the redirect header can no longer be set. Why don't you just reverse it by having your redirect header before sending any output to the client.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the flush method is invoked the response will be committed i.e. the contents written will be sent to the client .In such a case it is illgal to invoke sendRedirect() method. So IllelagalStateException will occur.
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In terms of what you can do purely from the server side, it's really *either* write a page that gets shown *or* send a redirect header; not both.
You could write javascript code to the page that will execute the 'redirect' (set the browser's location) on the client side, perhaps after a delay ( using a timer ).
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing u can do is set a "REFRESH" header in servlet like this..
response.setHeader("Refresh","2;URL=http://www.yahoo.com");
out.println("Hello");
hope this helps
malhar
 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic