Java Servlets, Display First, Run Code Later?

Discussion in 'Java' started by Nino Skilj, Apr 7, 2004.

  1. Nino Skilj

    Nino Skilj Guest

    Hello,

    I am currently trying to build a servlet with some deep calculations.
    The servlet also handles some of the simple webpage displays as well
    (using out.println()). My understanding is that servlets (when run)
    will do all the calculations, prepare all the out.printlns, build the
    html page and then return it for display... My question is this then:

    Is there a way to "hurry" the output process while still doing
    calculations? I want to do something like this...

    //This should be displayed immediately
    out.println("Please wait, calculation in progress....");

    //This is where the logic would go
    doCalculations();

    Does this make sense? Is this possible?

    Nino Skilj
     
    Nino Skilj, Apr 7, 2004
    #1
    1. Advertising

  2. Nino Skilj

    Anton Spaans Guest

    "Nino Skilj" <> wrote in message
    news:...
    > Hello,
    >
    > I am currently trying to build a servlet with some deep calculations.
    > The servlet also handles some of the simple webpage displays as well
    > (using out.println()). My understanding is that servlets (when run)
    > will do all the calculations, prepare all the out.printlns, build the
    > html page and then return it for display... My question is this then:
    >
    > Is there a way to "hurry" the output process while still doing
    > calculations? I want to do something like this...
    >
    > //This should be displayed immediately
    > out.println("Please wait, calculation in progress....");
    >
    > //This is where the logic would go
    > doCalculations();
    >
    > Does this make sense? Is this possible?
    >
    > Nino Skilj


    There was a similar question on this newsgroup before. I gave this answer.
    It should help your question as well:

    Most browsers accept pages that contains multiple html documents. This is
    also called 'Server Push'
    (The other way is 'Client Pull', where the browser pings the web-server, by
    issuing http-request every so-many seconds, using the "refresh" meta tag).
    When the browser sends out a HTTP request, the server can respond with a
    so-called 'multipart' document. The first part of that document can be your
    'Please Wait...' page. The second part can be the real stuff.

    For more info do a google on 'Server Push' and 'html'. Here is an example
    link: http://wp.netscape.com/assist/net_sites/pushpull.html

    (Note that you may need some threading in your webserver to make this
    happen)

    1. Server gets http-request.
    2. Server finds out this is a long process to get the data and the final
    result.
    3. Therefore, the server prepares a multipart document and sends the 'Please
    Wait...' part of it right away.
    Be sure to flush the response-writer!
    The browser will receive and display the 'Please Wait..'. page.
    4. The server starts doing the long process (in the same thread or other
    thread)
    5. When the long process is finished, the server will write its results into
    the second part of the document.
    6. The server writes the end of the multipart document.
    The browser will receive the second part and display it.
    7. Done....

    Server Push allows you to have this kind of behavior and you can, for
    example, implement server-directed slide-shows...
    Note that Server Push is not recommended (at all) for heavily loaded
    servers, since the connections can stay open for a long time.

    -- Anton.
     
    Anton Spaans, Apr 7, 2004
    #2
    1. Advertising

  3. On 7 Apr 2004 10:11:36 -0700, Nino Skilj wrote:

    > ..My understanding is that servlets (when run)
    > will do all the calculations, prepare all the out.printlns, build the
    > html page and then return it for display...


    That is not my understanding, check..
    javax.servlet.ServletResponse.flushBuffer()

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Apr 7, 2004
    #3
  4. Nino Skilj

    Ralf Fischer Guest

    Nino Skilj wrote:
    > Is there a way to "hurry" the output process while still doing
    > calculations? I want to do something like this...
    >
    > //This should be displayed immediately
    > out.println("Please wait, calculation in progress....");


    // send already written content to the client
    out.flush();

    > //This is where the logic would go
    > doCalculations();
    >
    > Does this make sense? Is this possible?


    I cannot tell you if it makes sense ;) In fact you don't know when the
    servlet container flushes the output stream, so all the stuff you wrote
    on it goes over the net to the client (except at the end of your
    doService method, maybe), so just flush the stream to write ready
    content on the net. If you are using some kind of Filters (e.g.
    GZipFilter) there may be an impact on this behaviour.

    Sure the browser has to support such things, older netscape browser will
    not display partly transfered content, but the latest mozilla should
    work fine.
     
    Ralf Fischer, Apr 7, 2004
    #4
  5. Nino Skilj wrote:
    > Hello,
    >
    > I am currently trying to build a servlet with some deep calculations.
    > The servlet also handles some of the simple webpage displays as well
    > (using out.println()). My understanding is that servlets (when run)
    > will do all the calculations, prepare all the out.printlns, build the
    > html page and then return it for display... My question is this then:
    >
    > Is there a way to "hurry" the output process while still doing
    > calculations? I want to do something like this...
    >
    > //This should be displayed immediately
    > out.println("Please wait, calculation in progress....");
    >
    > //This is where the logic would go
    > doCalculations();
    >
    > Does this make sense? Is this possible?
    >
    > Nino Skilj


    I'm no servlet programmer but here's my suggestion.

    Couldn't you have an entry point to your servlet like this:

    -- start pseudo html/jsp page --

    <text>
    Please wait...
    </text>
    <call the real servlet that does the calculation />

    -- end --

    This way, the user would fully load the entry point page and see the
    "wait" text.. then when the servlet is done with it's calculation it
    replaces the "wait" page. Sort of like a redirection.

    I dunno.
     
    Troy Kinsella, Apr 7, 2004
    #5
    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. Guest
    Replies:
    7
    Views:
    703
    Walter Wang [MSFT]
    Jun 28, 2006
  2. przemo

    Head First JSP and Servlets

    przemo, Dec 8, 2006, in forum: Java
    Replies:
    0
    Views:
    340
    przemo
    Dec 8, 2006
  3. dotNET learner

    HTML first, Page_Load later

    dotNET learner, Mar 19, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    528
    dotNET learner
    Mar 20, 2007
  4. none

    How do I excute later codes first?

    none, Nov 5, 2004, in forum: Javascript
    Replies:
    1
    Views:
    129
    Evertjan.
    Nov 5, 2004
  5. user
    Replies:
    41
    Views:
    380
    Dr J R Stockton
    Dec 2, 2006
Loading...

Share This Page