I have legacy javaservlet web application which has a global user defined class object in the main servlet class. while two requests hit on the same time, the first request values being shared to the second request as well. Please guide me How to handle this scenario and how to avoid this.
As you have discovered servlets should never have read/write instance variables.
Rather, create "scoped variables" on one of the request, session, or application contexts depending upon the needed lifetime of the data. Most often, data only needs to "live" for the duration of a request and so you'd create it on the request. For example: request.setAttribute("someName",someValue)
Many MVC frameworks allow you to declare a scope on your injected dependencies. For instance, a data source can be valid for the entire lifetime of an application and it's thread-safe, so in Spring you can annotate it like so:
However, if you want to inject a domain model, it should be request scoped because having only one that's shared between requests will cause concurrency issues, and making your domain model thread-safe will introduce latency issues. Therefore:
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.