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 ServletContext attribute that holds the ordered list of web fragments for this web application. |
| public static final String | TEMPDIR
The name of the ServletContext attribute that holds the temporary file location for the web application. |
| Modifier and Type | Method and Description |
|---|---|
| public FilterRegistration. | Returns: null if the filter has already been fully defined, else a
jakarta. object that can be used to further configure the
filterName of filter to add filterName, String Name of filter class className)Add filter to context. |
| public FilterRegistration. | Returns: null if the filter has already been fully defined, else a
jakarta. object that can be used to further configure the
filterName of filter to add filterName, Filter Filter to add filter)Add filter to context. |
| public FilterRegistration. | Returns: null if the filter has already been fully defined, else a
jakarta. object that can be used to further configure the
filterName of filter to add filterName, Class<? extends Filter> Class of filter to add filterClass)Add filter to context. |
| public ServletRegistration. | Returns: ajakarta. object that can be used to further configure the
servletThe servlet name under which this JSP file should be registered jspName, String The path, relative to the web application root, for the JSP file to be used for this servlet jspFile)Add a JSP to the context. |
| public void | |
| public < TODO T extends EventListener> void | |
| public void | |
| public ServletRegistration. | Returns: The registration object that enables further configurationThe name of the servlet to register servletName, String The implementation class for the servlet className)Register a servlet implementation for use in this ServletContext. |
| public ServletRegistration. | Returns: The registration object that enables further configurationThe name of the servlet to register servletName, Servlet The Servlet instance to register servlet)Register a servlet instance for use in this ServletContext. |
| public ServletRegistration. | Returns: null if the servlet has already been fully defined, else a
jakarta. object that can be used to further configure the
servletName of servlet to add servletName, Class<? extends Servlet> Class of servlet to add servletClass)Add servlet to the context. |
| public < The type for the given class T extends Filter> T | Returns: The created Filter instance.The the class for which an instance should be created c)Create a Filter instance using the given class. |
| public < TODO T extends EventListener> T | |
| public < The type for the given class T extends Servlet> T | Returns: The created Servlet instance.The the class for which an instance should be created c)Create an Servlet instance using the given class. |
| public void | |
| public Object | Returns: anObject containing the value of the attribute, or null if no attribute exists
matching the given namea 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 associated web application class loaderGet the web application class loader associated with 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 | |
| public Set | Returns: The set of default session tracking modes for this web applicationObtains the default session tracking modes for this web application. |
| public int | Returns: The major version declared in web.xmlObtain the major version of the servlet specification for which this web application is implemented. |
| public int | Returns: The minor version declared in web.xmlObtain the minor version of the servlet specification for which this web application is implemented. |
| public Set | Returns: The value supplied viasetSessionTrackingModes(Set) if one was previously set, else return the
defaultsObtains the currently enabled session tracking modes for this web application. |
| public FilterRegistration | |
| public Map | |
| public String | Returns: aString containing the value of the initialization parametera 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 | |
| public int | Returns: 6Returns the major version of the Java 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: aString specifying the real path, or null if the translation cannot be performeda path)String specifying a virtual pathReturns a |
| public String | Returns: The character encoding name ornull if no default has been specifiedGet the default character encoding for reading request bodies. |
| 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 a specified 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 character encoding name ornull if no default has been specifiedGet the default character encoding for writing response bodies. |
| 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 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 ServletRegistration | Returns: The registration details for the named Servlet ornull if no Servlet has been registered
with the given nameThe name of the Servlet of interest servletName)Obtain the details of the named servlet. |
| public Map | Returns: A Map of servlet names to servlet registrations for all servlets registered with this contextObtain a Map of servlet names to servlet registrations for all servlets registered with this context. |
| public SessionCookieConfig | |
| public int | Returns: The current default session timeout in minutesGet the default session timeout. |
| public String | Returns: The primary name of the virtual host on which this context is deployedGet the primary name of the virtual host on which this context is deployed. |
| public void | |
| 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 the servlet context. |
| 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 servlet context. |
| public boolean | Returns: true if the call succeeds or false if the call fails because an initialisation
parameter with the same name has already been setName of initialisation parameter name, String Value for initialisation parameter value)Set the given initialisation parameter to the given value. |
| public void | setRequestCharacterEncoding(String
The name of the character encoding to use encoding)Set the default character encoding to use for reading request bodies. |
| public void | setResponseCharacterEncoding(String
The name of the character encoding to use encoding)Set the default character encoding to use for writing response bodies. |
| public void | setSessionTimeout(int
The new default session timeout in minutes. sessionTimeout)Set the default session timeout. |
| public void | setSessionTrackingModes(Set<SessionTrackingMode>
The session tracking modes to use for this web application sessionTrackingModes)Configures the available session tracking modes for this web application. |