If I have a Servlet class, and this class is used in two applications – are static members of shared across both applications?
Is this behaviour specified by Java EE or container specific?
If I have a Servlet class, and this class is used in two applications
Share
No, the static members will not be shared across applications. Typically, each application would be associated with its own classloader, and hence, the Servlet class would be loaded twice in the container. By inference, static members would not be shared across the applications.
If you need to share data across applications, it is recommended to use files, JMS queues or a database, depending on your needs.
The Java EE 6 Platform specification does not define class loading behavior. The specification states the following in this regard:
Classes and resources that are visible to components, do not include classes from other web modules in other applications. They might include classes and resources in other web modules of the same application:
By inference, the Servlet class, if deployed in two different applications, will not be able to access the other class in the other application.