File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Using default ServletContext? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using default ServletContext?" Watch "Using default ServletContext?" New topic

Using default ServletContext?

Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
A quick question for all you Servlet API gurus :-)
I have some information that I need to store past the end of an HttpSession lifecyle.
This information needs to be available to all servlets in the web application.
Regarding the ServletContext, the Servlet Specification states:

SRV.3.2 Scope of a ServletContext Interface
There is one instance object of the ServletContext interface associated with each
web application deployed into a container. In cases where the container is
distributed over many virtual machines, a web application will have an instance of
the ServletContext for each VM.
Servlets in a container that were not deployed as part of a web application are
implicitly part of a �default� web application and have a default ServletContext.
In a distributed container, the default ServletContext is non-distributable and
must only exist in one VM.

Therefore, this information cannot be stored in the web application's ServletContext. However, we are using tomcat, which can be configured to allow access to the default context with the following entry in the server.xml configuration file:

This default context is guaranteed to be in a single JVM only, so perhaps we can store stuff in there. Glorious hack, or dangerous approach?
For example, the following JSPs can share information between web applications, so I am guessing this approach should work for distributed web applications also:
File 1:

File 2:

Comments please :-)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

Therefore, this information cannot be stored in the web application's ServletContext.

Why do you think this? You haven't described anything about your setup that precludes using the web app's servlet context.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Using default ServletContext?
It's not a secret anymore!