Jason Coward wrote:
>
> Is it appropriate to declare an HttpServlet's service() [or doGet()/doPost()]
> methods synchronized so that I can block additional requests as long as the
> servlet's life cycle has not been terminated (i.e. it has been destroy()'ed)?

No, absolutely not. the only exception might be if you are writing this servlet
for your own use, and it will never be called by more than one person (you). If
you intend to use this servlet where it could ever be called by more than one
client, then you should not synchronize service(), doGet() or doPost().

The proper solution is to make your servlet thread safe. That generally means
- Don't use instance variables inside methods, use only locals
- If you use instance or class variables inside methods, make them read-only
(don't allow a thread to change the variable)
- If you must change instance or class variables, protect them with a
synchronized block.
- If you must change instance or class variables, protect them with a
synchronized method.
- If you use system resources (files, etc.), you must protect them so that
concurrent threads do not affect each other.

> I am trying to write a file copy routine that runs as a background thread in a
> servlet, initiated by the first request to the servlet, and destroyed once the
> thread is completed upon receipt of a progress check from the client which
> initiates the servlet.  The servlet calls itself repetitively (using
> setHeader("refresh", ...)), reporting progress until the thread is completed.
>
> Also, can a servlet destroy() itself after the the response has been sent?  If
> so, how would it be accomplished?

A servlet could call its destroy() method at the end of processing a service
request. However that's generally not done. There's no reason to destroy the
servlet. The destroy method is provided for the servlet engine to call, not for
the servlet itself to call.

K Mukhar

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to