I am looking for some general suggestions on how to do a task:
I have a Java servlet running in Tomcat that is a form. Users come to
the page and fill out the form, and my servlet puts the information
into a database. My issue has to deal with a class I need to call
before entering the data in the database, which takes a little bit of
time to execute, thus the submission of the form takes a long time.
This can be called at ANY time, however.
Therefore, I want to be able to spawn a new thread to execute that
class/process right when the form is loaded, so my process can execute
while the user is filling out the form so that it is done (or almost
done) by the time they submit the form. How would you go about doing
that?
I think I know how to call a new thread to start the process, but how
can I make sure that after they hit "submit" on my form, that the other
thread is done before the data is entered into the database?
I hope I am clear enough.
Thanks for the help.
Rohit Kumbhar - 15 Aug 2006 23:09 GMT
> I am looking for some general suggestions on how to do a task:
>
[quoted text clipped - 9 lines]
> done) by the time they submit the form. How would you go about doing
> that?
I am assuming that you have a particular task which needs to be done for
a particular form [load-submit/request-response] combination, per
request. I would do it this way [if at all this is the only way to do it]:
Have two different URL strings [may use the same servlet though] for
form-load and form-submit actions.
URL is for form-load: Start the threaded task and cache the thread-id or
the runnable object or thread complete flag with some unique key [say,
session id].
URL is for form-submit: Get the id/object/flag for the key, do the
required checks and update the database.
> I think I know how to call a new thread to start the process, but how
> can I make sure that after they hit "submit" on my form, that the other
> thread is done before the data is entered into the database?
To achieve this, a simple flag should do the trick. Note, you will need
to cache the flags per request to the form.
> I hope I am clear enough.
>
> Thanks for the help.
Regards
Rohit