The servlet specification strongly recommends that doGet, doPost, and all other service methods should not be declared as synchronized. Declaring such methods as synchronized will often decrease performance significantly. It is not necessary to declare these methods as synchronized at all, since this is taken care of by the servlet container itself.
My question is, do you guys really write the code keeping in mind of this multi threading?.. As the above article says, I really never bothered to use synchronize etc.. just write doget or doPOst().. thats it.
You do not synchronize doGet() / doPost() methods. It is a bad practice. It has more harmful effects compared to the uses and is discouraged. Even the SingleThreadModel interface has been deprecated from the API.
In most practical situations, you do not have to worry about multi-threading effects, though in certain situations you have to keep in mind that servlets are multi-threaded applications. For example, you should know that servlet instance variables are shared by multiple threads.
MORAL: Build in thread-safety in your code by synchronizing on context or session and do not synchronize the doXXX methods.
Consider a ServerContext object, which u have used to set an attribute. It is a known fact that this ServerContext object works at application level and so many servlets can hit this ServerContext object. So if we set the attribute to a particular value, it is possible that it may undergo a change via a different servlet, which is undesirable.
So we should sychronize this ServerContext object. As a thumb rule, We do not require to synchronize request object but we should take care of ServerContext and session object. But it should be noted that synchronizing a method allows only one thread to access the given method, thus sacrifysing concurrency. So we should be very careful while using 'synchronizing' keyword and should only use it where it is utmost needed.