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
Each WAR file contains servlets, JSPs, a deployment descriptor, and related resource files. Static HTML files and JSP are stored at the top level of the WAR directory. The top-level directory contains the WEB-INF subdirectory which contains tag library descriptor files in addition to the following: Server-side classes.
The WAR file (Web Application Resource or Web Application ARchive) is a container for JAR files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static sites (HTML and associated files), and other resources that make up an online application. A file entitled web.
In software engineering, a WAR file (Web Application Resource or Web application ARchive) is a file used to distribute a collection of JAR-files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web ...
Perhaps the simplest way to deploy a WAR file to Tomcat is to copy the file to Tomcat's webapps directory. Copy and paste WAR files into Tomcat's webapps directory to deploy them. Tomcat monitors this webapps directory for changes, and if it finds a new file there, it will attempt to deploy it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With