Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ServletContext one per web-app or one per JVM?

Tags:

java

servlets

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?

like image 677
giri Avatar asked Feb 03 '10 11:02

giri


People also ask

How many ServletContext is available for an entire web application?

There's only one ServletContext for an entire web app, and all the parts of the web app share it. But each servlet in the app has its own ServletConfig. The Container makes a ServletContext when a web app is deployed, and makes the context available to each Servlet and JSP (which becomes a servlet) in the web app.

How many ServletContext objects are available for?

There is only one ServletContext object per web application.

What is the difference between ServletContext and ApplicationContext?

ApplicationContext is Spring's container. It's used to wire the configurations from Spring beans together and use them for the application. Use ApplicationContext if you want to retrieve the information of Spring beans. Use ServletContext if you want to get/set attribute those shared to all Servlet.

What is a ServletContext?

public interface ServletContext. Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine.


2 Answers

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.

like image 66
Nathan Hughes Avatar answered Sep 25 '22 01:09

Nathan Hughes


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
like image 39
Nils Schmidt Avatar answered Sep 22 '22 01:09

Nils Schmidt