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 is servlet doGet and doPost method are threadsafe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "is servlet doGet and doPost method are threadsafe?" Watch "is servlet doGet and doPost method are threadsafe?" New topic
Author

is servlet doGet and doPost method are threadsafe?

Kaleeswaran Karuppasamy
Ranch Hand

Joined: Jul 19, 2007
Posts: 154
is servlet doGet and doPost method are threadsafe or not?


Judge a man by his questions rather than his answers --Voltaire
SCJP 1.5 97%
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Servlets are not thread safe. Either do not use any member variables in them (recommended) or use member variables (not recommended) and synchronize to give them thread safety


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Kaleeswaran Karuppasamy
Ranch Hand

Joined: Jul 19, 2007
Posts: 154
i know servlet not threadsafe i am asking doget and dopost method is thread safe or not
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
You are asking for the impossible.

One could easily write a servlet in which doGet and doPost are NOT threadsafe by simply having the methods modify instance variables so the answer is "it depends".

On the other hand, the local variables refering to the request and the response ARE isolated from all other Threads.

Bill
Prashant Hurria
Ranch Hand

Joined: Mar 23, 2009
Posts: 40
Kaleeswaran Karuppasamy wrote:i know servlet not threadsafe i am asking doget and dopost method is thread safe or not


Hun ....... What do you mean by thread-safety??? It is said that servlets are not thread safe that means if there are 10 Simultaneous requests for a particular servlet the container will simply invoke ten different threads who will invoke the service method(in turn do get and doPost) of the Servlet (Unless your servlet implements the Threadsafe interface).
Please be aware of the fact that Variables local to methods are always threadsafe that means each Thread gets its own copy of the Method-Local variables. Now the so called Threadsafety problem occurs when you are using a Member variable i.e. the variable you declare in the servlet class all methods and all Threads share these variables.Change done by one method or one thread will be seen to everyone. So if you are using this for inter-method or inter-thread communication its ok but if you accidental use it for
(Cliched Example -->) something like The Balance in a Bank Account where one thread is Depositing while the other is Withdrawing all hell break loose.

Hope that helps.
Kaleeswaran Karuppasamy
Ranch Hand

Joined: Jul 19, 2007
Posts: 154
this what i expected local variable are always thread safe...thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is servlet doGet and doPost method are threadsafe?