Avoiding Class Loader Pitfalls

A couple of common problems occur when dealing with Tomcat’s class loaders. The solutions to these problems come from the information covered previously.

Packages Split Among Different Class Loaders

Each class loader has its own unique namespace, which has a practical application in Tomcat. If you have multiple classes in the same package, the same class loader must load them all for them to have access to other class loaders’ private, protected, or package-private members.

Singletons

A singleton is a class designed that can be instantiated only one time in any given JVM. Say a developer wants to share a singleton among multiple Web applications and wants to maintain the contract that only one instance be created in a single JVM.

Placing this class in the Web application class loader path means that each Web application will create a new instance of this class, which isn’t what the developer intends. This is because each Web application has its own class loader and class loaders maintain distinct namespaces.

The solution is to place this class in the shared class loader path, where the singleton will be shared among all Web applications, as they all share the same class loader.



Pro Jakarta Tomcat 5
Pro Apache Tomcat 5/5.5 (Experts Voice in Java)
ISBN: 1590593316
EAN: 2147483647
Year: 2004
Pages: 94

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net