Questions › Is ServletContext one per web-app or one per JVM?
giri Asked

Here is the doubt I have come up with ServletContext is one per web-app and one per JVM. But if I am running more than one web-app on the same JVM. Then it has 2 ServletContext per JVM. Is it Possible? Can anybody elaborate on this?


3 Answers :
Nathan Hughes answered

Each webapp will have its own ServletContext. The way the api docs put it is:

There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)

The "per web application" part means that if you set up the application in a cluster then each node in the cluster has a separate JVM, and that JVM will be running separate copies of all the applications, including a ServletContext for each webapp.

Nils Schmidt answered

One per web application and one per JVM can be explained with the following tables.

Several Web Apps on one Server (one JVM):

Web App A - Server/JVM 1 - Context 1 
Web App B - Server/JVM 1 - Context 2 
Web App C - Server/JVM 1 - Context 3

Same Web App on several Servers (several JVMs):

Web App A - Server/JVM 1 - Context 1 
Web App B - Server/JVM 1 - Context 2 
Web App C - Server/JVM 1 - Context 3 
Web App C - Server/JVM 2 - Context 4
Bozho answered

ServletContext:

There is one context per "web application" per Java Virtual Machine.

That means each web application has exactly one ServletContext on each VM. (The multiple-VM case is when you have a distributed application)