File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Regarding Thread-safe in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Regarding Thread-safe in Servlets" Watch "Regarding Thread-safe in Servlets" New topic
Author

Regarding Thread-safe in Servlets

Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
Hi....
By default, servlets are multithreaded,so Are they Thread-safe? and How?
C Shinde
Ranch Hand

Joined: Oct 22, 2009
Posts: 60
Servlets are rightly not threadsafe, so you need to either implement SingleThreadModel which is deprecated in the latest servlet Spec. or you need to code your Servlet in such a way that it is Thread-safe.

You can refer to this article for the same :JavaWorld - ThreadSafe Servlets - A Simple example


Regards,
Chetan Shinde
Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
C Shinde wrote:Servlets are rightly not threadsafe,


Thanks for replay,

so,why Java soft people implemented like this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Because for most servlets, the issue of thread safety doesn't arise. You design systems to work naturally for the common uses, but to allow programmers to do uncommon things. And since Java already has features which allow programmers to achieve thread safety when necessary, it wasn't necessary to do anything specific in the implementation of servlets. It's the programmer's responsibility to deal with it in the few cases where it is an issue.
C Shinde
Ranch Hand

Joined: Oct 22, 2009
Posts: 60
Check this URL at CodeRanch - Code Ranch .

Well, as I said there was this SingleThreadModel that could be implemented to make threadsafe, but was deprecated as it did not providing complete synchronization, it provided code level sync but then the session context, applicationcontext was not synchronized.

So the other option is the only way to get thread-safety in servlets is to code the Servlet in a Thread-Safe manner. THe JavaWorld guys have told how to do this.

You can try reading on net of what Thread-Safety means. Which you can again read here. webpage.
Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
Paul Clapham wrote:Because for most servlets, the issue of thread safety doesn't arise.


Hi Paul,
Thanks for replay,

But still I have little bit of doubt regarding servlets thread safety, so why most servlets, the issue of thread safety doesn't arise.How can you say this? and what is reason behind this? please clarify my doubt.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
The issue of Thread safety does not arise because each request is handled by a single Thread and this thread is the only one that has access to the request and response objects. Objects and variables which are created inside the service or doGet or doPost, etc method are only visible to the Thread handing that request.

It is the programmer's job to ensure that objects and variables which are potentially reachable by more than one request Thread are handled properly.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Thread-safe in Servlets