Java Servlets, Display First, Run Code Later?

N

Nino Skilj

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
 
A

Anton Spaans

Nino Skilj said:
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.
 
A

Andrew Thompson

..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()
 
R

Ralf Fischer

Nino said:
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.
 
T

Troy Kinsella

Nino said:
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.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top