Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No of JVM instances per war file?

Tags:

java

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

like image 262
M Sach Avatar asked Oct 10 '11 16:10

M Sach


People also ask

What does a WAR file contains in Java?

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.

Does WAR file contain jar?

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.

How does WAR file work?

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 ...

How can WAR file deployment be supported?

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.


2 Answers

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.

like image 113
meriton Avatar answered Sep 22 '22 19:09

meriton


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.

like image 44
MarianP Avatar answered Sep 20 '22 19:09

MarianP