Consider the case where 2 webapps are running within the same servlet container. If the first webapp creates a singleton object, is the same object accessible from the other? My findings so far suggest not, and I don't understand why that should be.
Other posts in this forum suggest setting 'crossContext' to 'true', which is fair enough. However, I'm curious why two webapps running within the same servlet conatainer and within the same JVM do not appear to be able to share the same singleton object. Or am I wrong?
First of all, the scope of a static variable is actually the class loader and not the JVM. Webservers use isolated class loaders for each web application that it hosts. Therefore, the singletons are per web application even if all of them are running on the same JVM instance.
This isolation is essential to avoid any conflicts in the static space especially considering the fact that the same libraries/frameworks could be used in two different applications. Another advantage is that you get to host different versions of the same app on the same server ( service upgradation ) without any conflicts.
Many thanks for your explanation.
Well you can/can't. There are advanced configs to share classes among web modules. If not a production problem and you are just inquiring for academic interests then play around with JBoss.