File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlets/ThreadSafe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets/ThreadSafe" Watch "Servlets/ThreadSafe" New topic


Arul Raj

Joined: Dec 16, 2000
Posts: 5
Weather servlets are ThreadSafe? Please Explain
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Normally servlets are not Thread safe because the same servlet object can be executing many request Threads. You can not rely on instance variables keeping information for a particular request. When designing a servlet this must be kept in mind at all times.
It is not that hard to design your servlet so that only variables that are unique to a given request / Thread are used. The API provides help with the HttpSession object that you can use to store variables for a particular user.
If you absolutely can't figure out a way to do your servlet without instance variables, then you can use the SingleThreadModel interface which forces the servlet engine to only allow one Thread to execute in a servlet object.

author of:
I agree. Here's the link:
subject: Servlets/ThreadSafe
It's not a secret anymore!