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 problem! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect problem!" Watch "sendRedirect problem!" New topic

sendRedirect problem!

Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41

First,I want to show "hello" on browser,
Next,redirect to
but,the browser shows only "hello",don't redirect to
If del out.flush(),browser can redirect to don't show "hello".
who can tell me, why?

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.

So much trouble in the world -- Bob Marley
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

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

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
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
one more thing u can do is set a "REFRESH" header in servlet like this..
hope this helps
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
Thanks all!
I agree. Here's the link:
subject: sendRedirect problem!
It's not a secret anymore!