简体   繁体   中英

Are static members shared across applications in a Java EE compliant servlet container?

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 - are static members of shared across both applications?

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.

Is this behaviour specified by Java EE or container specific?

The Java EE 6 Platform specification does not define class loading behavior. The specification states the following in this regard:

EE.8.3 Class Loading Requirements

The Java EE specification purposely does not define the exact types and arrangements of class loaders that must be used by a Java EE product. Instead, the specification defines requirements in terms of what classes must or must not be visible to components.

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:

EE.8.3.1 Web Container Class Loading Requirements

...

Components in the web container may have access to the following classes and resources. Portable applications must not depend on having or not having access to these classes or resources.

•The classes and resources accessible to any other web modules included in the same ear file, as described above.

...

By inference, the Servlet class, if deployed in two different applications, will not be able to access the other class in the other application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM