why tomcat has its own classloader. what is the advantage of having user defined classloader
-
As another answer I might mention that I'm pretty sure that if you overwrite the classloader you can do some pretty impressive meta stuff with your code. It can let you implement your own encryption schemes and should be able to do some aspect-j style tricks (For instance, having every call to "Log" tagged with the name of the class/line number it's logging from. I didn't make this an "Answer" because I haven't done this stuff but I think it's possible. – Bill K Aug 23 '10 at 19:01
3 Answers
It isolates the various webapplications deployed into the container; that is, the behaviour of a webapplication will not be affected by (un)deploying another webapplication.
Each webapp only sees its own classes, not those provided by the other applications. This allows different webapps to use different versions of the same class. Deploying several webapplications would be a nightmare without that isolation.
Similarly, OSGI bundles get their own class loaders so different bundles can use different versions of the same plugin. Again, this isolation makes it less likely that adding a plugin (with its depedent libraries) will affect the other plugins in the system.

- 68,356
- 14
- 108
- 175
Tomcat (and other application containers) need to be able to handle loading classes from WAR files etc. How would you be able to do that without using user-defined classloaders?
EDIT: Basically you need user-defined classloaders if you need to load classes or resources in "unusual" ways... such as from an EAR or WAR file. As another example, you might load classes from a database, or from some secure storage.

- 1,421,763
- 867
- 9,128
- 9,194
-
2skeet why they cant use bootstrap classloader or system classloader – Dead Programmer Aug 23 '10 at 17:56
-
1@Suresh S: Do either of those know how to process a WAR or an EAR file? – Jon Skeet Aug 23 '10 at 18:13
I worked on a system once that had a classloader that loaded classes from a distributed DB. That way you could modify your code, compile it and have the compiler dump it to the DB, then anyone who restarted their system would have it load immediately (this was for clients on a distributed client/server system where admins could create their own screen modules on the fly and push them out to the clients).
This had issues by the way, great theory but I can't recommend it.

- 62,186
- 18
- 105
- 157