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

Discussion in 'Java' started by hiwa, May 13, 2004.

  1. hiwa

    hiwa Guest

    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?
     
    hiwa, May 13, 2004
    #1
    1. Advertising

  2. Take a look at Web Progressor at http://www.SunwestTek.com/wp. It is
    designed for scenario like this.

    (hiwa) wrote in message news:<>...
    > 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?
     
    Sunwest Technologies, May 13, 2004
    #2
    1. Advertising

  3. On 13 May 2004 03:11:07 -0700, hiwa <> wrote:

    > 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
     
    Michiel Konstapel, May 13, 2004
    #3
  4. hiwa

    hiwa Guest

    Michiel Konstapel <> wrote in message news:<>...
    > On 13 May 2004 03:11:07 -0700, hiwa <> wrote:
    >
    > > 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


    Thanks.
    Thread with page refresh did the work!
     
    hiwa, May 14, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. phil cunningham
    Replies:
    2
    Views:
    403
    Rob R. Ainscough
    Jan 25, 2006
  2. Rod
    Replies:
    0
    Views:
    424
  3. R
    Replies:
    0
    Views:
    724
  4. Christian Graf
    Replies:
    2
    Views:
    400
  5. qazmlp
    Replies:
    1
    Views:
    603
    Jonathan Turkanis
    Feb 15, 2004
Loading...

Share This Page