How to get flush() and forward() working together?

H

hiwa

In the doGet() method of a servlet, we do:

(1)out.println(simpleMessage);
(2)out.flush();
(3)Do a time consuming task
(4)When the task is finished, we call reqDispatcher.forward()
to display other resource.

The problem is as follows:

If we don't call flush(), simpleMessage doesn't get displayed
before going to other resource.

However, if we call flush(), reqDispatcher.forward() doesn't
work. The error message says "forward() not allowed after buffer
has committed."

If we use sendRedirect() instead of forward(), resulting error
message is the same.

Can't ther be a workaround for the problem?
 
M

Michiel Konstapel

In the doGet() method of a servlet, we do:

(1)out.println(simpleMessage);
(2)out.flush();
(3)Do a time consuming task
(4)When the task is finished, we call reqDispatcher.forward()
to display other resource.

The problem is as follows:

If we don't call flush(), simpleMessage doesn't get displayed
before going to other resource.

However, if we call flush(), reqDispatcher.forward() doesn't
work. The error message says "forward() not allowed after buffer
has committed."

If we use sendRedirect() instead of forward(), resulting error
message is the same.

Can't ther be a workaround for the problem?

Nope. You have to pick one way of replying to the client's request.
Either you generate the reply yourself (out.println()) or you have someone
else do it (indirectly, by sending the client a redirect to another page,
or directly, by forwarding the request on the server side). You can't send
two replies to the same request.

What you can do is
(1) spawn a thread to do the time consuming task
(2) send the client a page that contains a refresh header, telling them
the "simple message"
(3) when the client comes back because of the refresh, check wether the
task has completed
(4a) if it hasn't, send them a "still busy" page, with another refresh
header
(4b) if it has, show the "other resource"

HTH,
Michiel
 
H

hiwa

Michiel Konstapel said:
Nope. You have to pick one way of replying to the client's request.
Either you generate the reply yourself (out.println()) or you have someone
else do it (indirectly, by sending the client a redirect to another page,
or directly, by forwarding the request on the server side). You can't send
two replies to the same request.

What you can do is
(1) spawn a thread to do the time consuming task
(2) send the client a page that contains a refresh header, telling them
the "simple message"
(3) when the client comes back because of the refresh, check wether the
task has completed
(4a) if it hasn't, send them a "still busy" page, with another refresh
header
(4b) if it has, show the "other resource"

HTH,
Michiel

Thanks.
Thread with page refresh did the work!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top