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?
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.
There is only one ServletContext object per web application.
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.
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.
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.
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
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