wood burning stoves 2.0*
The moose likes Servlets and the fly likes two web applications referring to a common jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "two web applications referring to a common jar" Watch "two web applications referring to a common jar" New topic
Author

two web applications referring to a common jar

satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
Hello ,
Not sure if this is the right forum for this question

I have two web applications ( Ap A & B ) deployed on a server as war files.
These wars have a "Manifest" file in which is specified a common library jar file containing utility classes. ( Util.jar )

So essentially both applications make use of this jar file .

This jar file is kept at a common location on server.

Questions ::
1 >Will different classloaders load these applications separately ?
2 >Will there be any issues related to "thread" safety ?
I dont know what the right word is - by thread I mean if both applications make a call to a Utility class in which there are static instance level variables .

So will a change in the value of these variables made by one app be reflected in the request simultaneously coming in from another app ?

Or does separate classloaders also mean an additional compartment within the jvm for each app ? i.e the classloader demarcates the boundary for each app ?

Thanks ,
~satish
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
I have two web applications ( Ap A & B ) deployed on a server as war files. These wars have a "Manifest" file in which is specified a common library jar file containing utility classes. ( Util.jar )

I don't think that .war files use manifests.

Will different classloaders load these applications separately ?

Yes, at least three classloaders will be involved: one for the common libraries, and one for each of the web apps.

Will there be any issues related to "thread" safety ? I dont know what the right word is - by thread I mean if both applications make a call to a Utility class in which there are static instance level variables.

If there's a chance that the code will be called simultaneously by more than one thread, then, yes, you'll need to make it thread-safe.

So will a change in the value of these variables made by one app be reflected in the request simultaneously coming in from another app ?

Yes. If you want to avoid this then you need to keep a copy of the library in each web app's WEB-INF/lib directory. You still need to worry about thread safety, of course, since the servlet that uses the library could be hit simultaneously by several requests.
[ August 26, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
Thanks Ulf for the response .

In a couple of examples I am seing war files with manifest that contains jar references .

Regarding " thread safety " - I totally understand that the servlet container uses a muti thread approach

What I was worried was if two apps simultaneously use the utility jar - will there be any issues - and from your answer - there will be issues .

Thanks for these details .

My only query is - so what really does the class loader do ?
what would have happened if only one classloader did the loading of all three ( two apps and one jar )

I am not knowledgeable regarding class loaders so pardon if these questions are basic .

Thanks ,
~satish
Mario Minutolo
Greenhorn

Joined: Aug 06, 2008
Posts: 23
Originally posted by satish bodas:
Hello ,
Not sure if this is the right forum for this question

I have two web applications ( Ap A & B ) deployed on a server as war files.
These wars have a "Manifest" file in which is specified a common library jar file containing utility classes. ( Util.jar )

So essentially both applications make use of this jar file .

~satish



Don't do this at home!

If you do not NEED to share objects between web-apps (which you SHOULD NOT anyway ), do NOT USE a common library, you will get into a hell of class loaders.

Then, if you do not NEED static variable, do not use them, and if you think you need them then use singletons instead.

Somethig like this:

You can use synchronized getters and setters to access/modify private fields of a singleton, which is more secure then accessing static fields.


Classloaders errors, and errors realted to static variables are quite nasty to debug, always try to find a solution which will not possibly trigger this kind of errors.
Mario Minutolo
Greenhorn

Joined: Aug 06, 2008
Posts: 23
Originally posted by satish bodas:

My only query is - so what really does the class loader do ?
what would have happened if only one classloader did the loading of all three ( two apps and one jar )


Classloaders in a webapp are arranged into a tree structure.
There is a root classloader which belongs to the 'server' and then there is the common classloader, which is shared between apps, and then there is a classloader for each webapp.

Since webapp are distinct entities, must keep their resource 'private', using a classloader per webapp enforce this policiy.

You should not access resouces of a webapp from another, using differnt instance of a class loader you cannot do this, so you can't violate this fundamental policy of a web-application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: two web applications referring to a common jar
 
Similar Threads
Strange NoClassDefFoundError with obfuscated build
Deserialization problem - InvalidClass w/ negative serialVersionUid for local class
weblogic applications classloader question and good practice?
not able to run two site simultaneously in a tomcat websrever
Class Loader