wood burning stoves*
The moose likes Servlets and the fly likes Thread-safety of servlets. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread-safety of servlets." Watch "Thread-safety of servlets." New topic
Author

Thread-safety of servlets.

raj sekhar
Ranch Hand

Joined: Oct 16, 2001
Posts: 117
I have few questions about servlets and the surrounding issue of thread-safety.
1.Are servlets thread safe?
2. How servlets are affected when the user opens a new browser for the same page and does simultaneously two different things.Again...how thread-safe are these operations and what precautions should the developer take?
3.what if two or more requests access the same servlet(though they do not have any instace variables) and how does the servlet engine handle in normal case(a single servlet object serving all requests?..or a pool?)
4. How does a servlet pool work? Will the developer have any control over this pool that the engine might have?
4.I am planning to have one single servlet to guide all other operations of the application ....somewhat like a MVC architecture. Does this kind of architecture fit servlets and what measures/precautions should a develper take?
Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
1.&2. Are servlets thread safe?
Not automatically, you have to use the proper programming conventions. Basically, never use a servlet instance variable for any request-specific data.
3. & 4. multiple requests.
There is typically only one instance of a servlet object. The servlet engine provides a unique request and response object for each separate request. The servlet engine might provide a Thread pool (NOT a servlet object pool) - you don't have to worry about it.
5. You are describing a classic servlet architecture.
Bill
------------------
author of:
raj sekhar
Ranch Hand

Joined: Oct 16, 2001
Posts: 117
Thank you very much Bill
If servlets are not thread-safe, under what conditions should the developer really need to worry(apart from NOT using instance variables).I did go thru the specs.Maybe I am wrong but I couldnt find a concise listing or something like that.Can you point me to some urls on that direction?
Can you also point to a right source to know more about the classic architecture that was said above?...questions in my mind are about the precautions and better way of handling various components in the application.
Thanks for all your help.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Aside from the obvious - don't use instance variables - you would need to worry about support classes where a single instance has to be used by multiple requests. For example, I have an object that holds course schedule information that may be modified on the fly. Every access has to be in a synchronized block.
A less than obvious case - a single HttpSession object may be accessed by more than one request Thread - IF the user has two browser windows open on the same application OR a frame is being used. In this case synchronize on the session when accessing session attributes.
There have been several discussions here about the MVC architecture - also called "type 2 architecture" in discussion of servlets and JSP.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread-safety of servlets.
 
Similar Threads
Servlet synchronization question.....
When will be single threaded servlet mandatory to implement?
Single Thread Model
Is Sessions thread safe if servlet implement SingleThreadModel interface ?
Single Thread Model