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.)
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.
The ServletContext object is contained within
the ServletConfig object, which the Web server provides the
servlet when the servlet is initialized.
Servlet#getServletConfig, ServletConfig#getServletContext
| Modifier and Type | Field and Description |
|---|---|
| public static final String | ORDERED_LIBS
The name of the |
| public static final String | TEMPDIR
The name of the ServletContext attribute which stores the private temporary directory (of type java.io.File) provided by the servlet container for the ServletContext |
| Modifier and Type | Method and Description |
|---|---|
| public FilterRegistration. | Returns: a FilterRegistration object that may be used to further configure the registered filter, or null if this ServletContext already contains a complete FilterRegistration for a filter with the given filterNamethe name of the filter filterName, String the fully qualified class name of the filter className)Adds the filter with the given name and class name to this servlet context. |
| public FilterRegistration. | Returns: a FilterRegistration object that may be used to further configure the given filter, or null if this ServletContext already contains a complete FilterRegistration for a filter with the given filterName or if the same filter instance has already been registered with this or another ServletContext in the same containerthe name of the filter filterName, Filter the filter instance to register filter)Registers the given filter instance with this ServletContext under the given filterName. |
| public FilterRegistration. | Returns: a FilterRegistration object that may be used to further configure the registered filter, or null if this ServletContext already contains a complete FilterRegistration for a filter with the given filterNamethe name of the filter filterName, Class<? extends Filter> the class object from which the filter will be
instantiated filterClass)Adds the filter with the given name and class type to this servlet context. |
| public ServletRegistration. | Returns: a ServletRegistration object that may be used to further configure the registered servlet, or null if this ServletContext already contains a complete ServletRegistration for a servlet with the given servletNamethe name of the servlet servletName, String the full path to a JSP file within the web application
beginning with a `/'. jspFile)Adds the servlet with the given jsp file to this servlet context. |
| public void | addListener(String
the fully qualified class name of the listener className)Adds the listener with the given class name to this ServletContext. |
| public < the class of the EventListener to add T extends EventListener> void | |
| public void | addListener(Class<? extends EventListener>
the listener class to be instantiated listenerClass)Adds a listener of the given class type to this ServletContext. |
| public ServletRegistration. | Returns: a ServletRegistration object that may be used to further configure the registered servlet, or null if this ServletContext already contains a complete ServletRegistration for a servlet with the given servletNamethe name of the servlet servletName, String the fully qualified class name of the servlet className)Adds the servlet with the given name and class name to this servlet context. |
| public ServletRegistration. | Returns: a ServletRegistration object that may be used to further configure the given servlet, or null if this ServletContext already contains a complete ServletRegistration for a servlet with the given servletName or if the same servlet instance has already been registered with this or another ServletContext in the same containerthe name of the servlet servletName, Servlet the servlet instance to register servlet)Registers the given servlet instance with this ServletContext under the given servletName. |
| public ServletRegistration. | Returns: a ServletRegistration object that may be used to further configure the registered servlet, or null if this ServletContext already contains a complete ServletRegistration for the given servletNamethe name of the servlet servletName, Class<? extends Servlet> the class object from which the servlet will be
instantiated servletClass)Adds the servlet with the given name and class type to this servlet context. |
| public < the class of the Filter to create T extends Filter> T | Returns: the new Filter instancethe Filter class to instantiate clazz)Instantiates the given Filter class. |
| public < the class of the EventListener to create T extends EventListener> T | Returns: the new EventListener instancethe EventListener class to instantiate clazz)Instantiates the given EventListener class. |
| public < the class of the Servlet to create T extends Servlet> T | Returns: the new Servlet instancethe Servlet class to instantiate clazz)Instantiates the given Servlet class. |
| public void | declareRoles(String...
the role names being declared roleNames)Declares role names that are tested using |
| public Object | Returns: anObject containing the value of the
attribute, or null if no attribute
exists matching the given name.a name)String specifying the name
of the attributeReturns the servlet container attribute with the given name, or
|
| public Enumeration | Returns: anEnumeration of attribute
namesReturns an |
| public ClassLoader | Returns: the class loader of the web application represented by this ServletContextGets the class loader of the web application represented by this ServletContext. |
| public ServletContext | Returns: theServletContext object that
corresponds to the named URL, or null if either
none exists or the container wishes to restrict
this access.a uripath)String specifying the context path of
another web application in the container.Returns a |
| public String | Returns: The context path of the web application, or "" for the root contextReturns the context path of the web application. |
| public Set | Returns: set of the session tracking modes supported by default for this ServletContextGets the session tracking modes that are supported by default for this ServletContext. |
| public int | Returns: the major version of the Servlet specification that the application represented by this ServletContext is based onGets the major version of the Servlet specification that the application represented by this ServletContext is based on. |
| public int | Returns: the minor version of the Servlet specification that the application represented by this ServletContext is based onGets the minor version of the Servlet specification that the application represented by this ServletContext is based on. |
| public Set | Returns: set of the session tracking modes in effect for this ServletContextGets the session tracking modes that are in effect for this ServletContext. |
| public FilterRegistration | Returns: the (complete or preliminary) FilterRegistration for the filter with the given filterName, or null if no FilterRegistration exists under that namethe name of a filter filterName)Gets the FilterRegistration corresponding to the filter with the given filterName. |
| public Map | Returns: Map of the (complete and preliminary) FilterRegistration objects corresponding to all filters currently registered with this ServletContextGets a (possibly empty) Map of the FilterRegistration objects (keyed by filter name) corresponding to all filters registered with this ServletContext. |
| public String | Returns: aString containing the value of the
context's initialization parameter, or null if the
context's initialization parameter does not exist.a name)String containing the name of the
parameter whose value is requestedReturns a |
| public Enumeration | Returns: anEnumeration of String
objects containing the names of the context's
initialization parametersReturns the names of the context's initialization parameters as an
|
| public JspConfigDescriptor | Returns: the<jsp-config> related configuration
that was aggregated from the web.xml and
web-fragment.xml descriptor files of the web application
represented by this ServletContext, or null if no such configuration
existsGets the |
| public int | Returns: 4Returns the major version of the Servlet API that this servlet container supports. |
| public String | Returns: aString specifying the file's MIME typea file)String specifying the name of a fileReturns the MIME type of the specified file, or |
| public int | Returns: 0Returns the minor version of the Servlet API that this servlet container supports. |
| public RequestDispatcher | Returns: aRequestDispatcher object
that acts as a wrapper for the named servlet,
or null if the ServletContext
cannot return a RequestDispatchera name)String specifying the name
of a servlet to wrapReturns a |
| public String | Returns: the real path, or null if the translation cannot be performedthe virtual path to be translated to a
real path path)Gets the real path corresponding to the given virtual path. |
| public String | Returns: the request character encoding that are supported by default for this ServletContextGets the request character encoding that are supported by default for this ServletContext. |
| public RequestDispatcher | Returns: aRequestDispatcher object
that acts as a wrapper for the resource
at the specified path, or null if
the ServletContext cannot return
a RequestDispatchera path)String specifying the pathname
to the resourceReturns a |
| public URL | Returns: the resource located at the named path, ornull if there is no resource at that patha path)String specifying
the path to the resourceReturns a URL to the resource that is mapped to the given path. |
| public InputStream | Returns: theInputStream returned to the
servlet, or null if no resource
exists at the specified patha path)String specifying the path
to the resourceReturns the resource located at the named path as
an |
| public Set | Returns: a Set containing the directory listing, or null if there are no resources in the web application whose path begins with the supplied path.the partial path used to match the resources,
which must start with a / path)Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument. |
| public String | Returns: the request character encoding that are supported by default for this ServletContextGets the response character encoding that are supported by default for this ServletContext. |
| public String | Returns: aString containing at least the
servlet container name and version numberReturns the name and version of the servlet container on which the servlet is running. |
| public Servlet | Returns: thejavax.servlet.Servlet Servlet with the given namethe servlet name name)
Deprecated
As of Java Servlet API 2.1, with no direct replacement.
|
| public String | Returns: The name of the web application or null if no name has been declared in the deployment descriptor.Returns the name of this web application corresponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element. |
| public Enumeration | Returns: anEnumeration of javax.servlet.Servlet Servlet names
Deprecated
As of Java Servlet API 2.1, with no replacement.
|
| public ServletRegistration | Returns: the (complete or preliminary) ServletRegistration for the servlet with the given servletName, or null if no ServletRegistration exists under that namethe name of a servlet servletName)Gets the ServletRegistration corresponding to the servlet with the given servletName. |
| public Map | Returns: Map of the (complete and preliminary) ServletRegistration objects corresponding to all servlets currently registered with this ServletContextGets a (possibly empty) Map of the ServletRegistration objects (keyed by servlet name) corresponding to all servlets registered with this ServletContext. |
| public Enumeration | Returns: anEnumeration of javax.servlet.Servlet Servlet
Deprecated
As of Java Servlet API 2.0, with no replacement.
|
| public SessionCookieConfig | Returns: the SessionCookieConfig object through which various properties of the session tracking cookies created on behalf of this ServletContext may be configuredGets the |
| public int | Returns: the session timeout in minutes that are supported by default for this ServletContextGets the session timeout in minutes that are supported by default for this ServletContext. |
| public String | Returns: aString containing the configuration name of the
logical host on which the servlet context is deployed.Returns the configuration name of the logical host on which the ServletContext is deployed. |
| public void | |
| public void | log(Exception
the exception, String Exception errora msg)String that describes the exception
Deprecated
As of Java Servlet API 2.1, use
log(String message, Throwable throwable)
instead.
|
| public void | |
| public void | removeAttribute(String
a name)String specifying the name
of the attribute to be removedRemoves the attribute with the given name from this ServletContext. |
| public void | setAttribute(String
a name, Object String specifying the name
of the attributean object)Object representing the
attribute to be boundBinds an object to a given attribute name in this ServletContext. |
| public boolean | Returns: true if the context initialization parameter with the given name and value was set successfully on this ServletContext, and false if it was not set because this ServletContext already contains a context initialization parameter with a matching namethe name of the context initialization parameter to set name, String the value of the context initialization parameter to set value)Sets the context initialization parameter with the given name and value on this ServletContext. |
| public void | setRequestCharacterEncoding(String
request character encoding encoding)Sets the request character encoding for this ServletContext. |
| public void | setResponseCharacterEncoding(String
response character encoding encoding)Sets the response character encoding for this ServletContext. |
| public void | setSessionTimeout(int
session timeout in minutes sessionTimeout)Sets the session timeout in minutes for this ServletContext. |
| public void | setSessionTrackingModes(Set<SessionTrackingMode>
the set of session tracking modes to
become effective for this ServletContext sessionTrackingModes)Sets the session tracking modes that are to become effective for this ServletContext. |