No of JVM instances per war file?
As per my understanding there will be one jvm instance and one class loader hierarchy per war file. Right? Two questions:-
Question1:- if i package my servlet class and business class(packaed in jar file) in war file.So war file here contains jar file and servlet class. If i try access static global variable declared in servlet in business class, i can do it Correct? because here will be only one jvm instances and class loader hierarchy
Question2开发者_如何学运维:-In same scenario as above if i package my servlet class and business class in two different war files both packaged under same ear file then If i try access static global variable declared in servlet in business class, i can not do it .Is it Correct? because here will be two jvm instances and class loader hierarchy per war file
Of course the entire application server runs in a single JVM (at least that's true of all application servers I know of). There is no need to launch a separate JVM to give each web application a dedicated class loader that sees different (versions of) classes.
So war file here contains jar file and servlet class. If i try access static global variable declared in servlet in business class, i can do it Correct?
You probably can, but you should not, as it violates the layering of your application if the business layer relies on the presence of a particular class from the presentation layer.
if i package my servlet class and business class in two different war files both packaged under same ear file then If i try access static global variable declared in servlet in business class, i can not do it .Is it Correct?
Again, this is bad design. Moreover (as far as I know) the specification does not mandate a particular behaviour that is adhered to by all application servers, so this is likely to depend on your choice of application server and its configuration.
There is no reason for a web container to start a new JVM instance for each web application (either deployed using a war file or by simple copying of what would be inside the war into the './webapps/' directory in e.g. Apache Tomcat). Different web apps are usually started using different class loaders to securely separate them from each other. This is no standard, just the way things are usually done by web containers.
There are no 'global static' variables in Java (not by this name), what you mean are 'public static' class fields/variables. These are only accessible by classes loaded by the same classloader (that are contained in the same web app). (Also presuming they have access to the containing class as a class may have default access which disallows some classes, even loaded by the same classloader, from accessing its members).
The way you try to access stuff from different wars is bad design as explained in meriton's answer.
1) Use ServletContex for sharing data within same web app as described in gertas' answer.
2) If you really need to, you may share data between different web apps using JNDI.
3) Also consider if what you really need is not sharing data, but messaging or full fledged persistence mechanism.
You are right, one class loader per WAR, separate static variable scopes between WARs, but not exactly two JVM instances. Usually servlet container runs under one JVM.
But using static variables to exchange data (constants are ok) is bad design I would suggest using ServletContext. See also Servlet context scope vs global variable .
精彩评论