This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes sendRedirect problem! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect problem!" Watch "sendRedirect problem!" New topic
Author

sendRedirect problem!

Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41

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.


Val SCJP2 1.2<br />====>SCJD
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

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.


Groovy
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
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

Joined: Aug 17, 2001
Posts: 399
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

Joined: Nov 27, 2001
Posts: 41
Thanks all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect problem!
 
Similar Threads
Servlet Redirection
sendRedirect problem!help me
Redirection of web pages
jstl c + import
JSP Web Site