Servlet

R

rickbeardk

Hi!

When you create a servlet with the method doGet, the servlet runs at
every request.

Is it possible to create a servlet wich runs all the time and is doing
things on the server on wich it is running (e.g. writing a logfile,
listening to RS232 etc.) and still also react and response at requests
from a client?

- rick -
 
A

Alex Hunsley

Hi!

When you create a servlet with the method doGet, the servlet runs at
every request.

No, a servlet is not created with doGet. doGet is called per request (or
more accurately, service() in the superclass is called per request,
which then calls doGet or doPost etc.). Your servlet instance persists
across all requests between server startup and server shutdown/restart.
This is why servlets must be careful about storing any state between
requests: they can't just use member variables in a normal naive way,
since those member variables are shared across all requests they service.



Anyway, for a servlet to do some background processing, perhaps try a
scheme like this:

On a request for starting work, start a new thread to do the calculation
work. Associate this thread with the session of the requesting party. On
subsequent requests, check if the thread has finished its work:
Yes -> return result to user
No -> return a message page saying 'processing, please wait...',
complete with an http-refresh header in the page, to cause the page to
automatically refresh every 1 second (or whatever interval you choose).
This way to user sees a continually refreshing page telling them the
processing is in progress. Your "please wait" page could also return
some sort of progress bar or percentage, if you could work out such a thing.

This is only one way, but a fairly common strategy methinks.
 

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

No members online now.

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top