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 | Method and Description |
|---|---|
| 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 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: aString containing at least the
servlet container name and version numbera 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 int | Returns: 2Returns 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: 4Returns 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 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: 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 | |
| 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 correponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element. |
| public Enumeration | |
| public Enumeration | |
| public void | |
| public void | log(Exception exception, String msg)
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 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. |
| getAttribute | back to summary |
|---|---|
| public Object getAttribute(String name) Returns the servlet container attribute with the given name,
or The attribute is returned as a
| |
| getAttributeNames | back to summary |
|---|---|
| public Enumeration Returns an
| |
| getContext | back to summary |
|---|---|
| public ServletContext getContext(String uripath) Returns a This method allows servlets to gain
access to the context for various parts of the server, and as
needed obtain In a security conscious environment, the servlet container may
return
| |
| getInitParameter | back to summary |
|---|---|
| public String getInitParameter(String name) Returns a This method can make available configuration information useful to an entire "web application". For example, it can provide a webmaster's email address or the name of a system that holds critical data.
| |
| getInitParameterNames | back to summary |
|---|---|
| public Enumeration Returns the names of the context's initialization parameters as an
| |
| getMajorVersion | back to summary |
|---|---|
| public int getMajorVersion() Returns the major version of the Java Servlet API that this servlet container supports. All implementations that comply with Version 2.4 must have this method return the integer 2.
| |
| getMimeType | back to summary |
|---|---|
| public String getMimeType(String file) Returns the MIME type of the specified file, or | |
| getMinorVersion | back to summary |
|---|---|
| public int getMinorVersion() Returns the minor version of the Servlet API that this servlet container supports. All implementations that comply with Version 2.4 must have this method return the integer 4.
| |
| getNamedDispatcher | back to summary |
|---|---|
| public RequestDispatcher getNamedDispatcher(String name) Returns a Servlets (and JSP pages also) may be given names via server
administration or via a web application deployment descriptor.
A servlet instance can determine its name using
This method returns
| |
| getRealPath | back to summary |
|---|---|
| public String getRealPath(String path) Returns a The real path returned will be in a form
appropriate to the computer and operating system on
which the servlet container is running, including the
proper path separators. This method returns | |
| getRequestDispatcher | back to summary |
|---|---|
| public RequestDispatcher getRequestDispatcher(String path) Returns a The pathname must begin with a "/" and is interpreted as relative
to the current context root. Use
| |
| getResource | back to summary |
|---|---|
| public URL getResource(String path) throws MalformedURLException Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root. This method allows the servlet container to make a resource
available to servlets from any source. Resources
can be located on a local or remote
file system, in a database, or in a The servlet container must implement the URL handlers
and This method returns Some containers may allow writing to the URL returned by this method using the methods of the URL class. The resource content is returned directly, so be aware that
requesting a This method has a different purpose than
| |
| getResourceAsStream | back to summary |
|---|---|
| public InputStream getResourceAsStream(String path) Returns the resource located at the named path as
an The data in the Meta-information such as content length and content type
that is available via The servlet container must implement the URL handlers
and This method is different from
| |
| getResourcePaths | back to summary |
|---|---|
| public Set Returns a directory-like listing of all the paths to resources within
the web application whose longest sub-path matches the supplied path
argument. Paths indicating subdirectory paths end with a '/'. The
returned paths are all relative to the root of the web application and
have a leading '/'. For example, for a web application containing | |
| getServerInfo | back to summary |
|---|---|
| public String getServerInfo() Returns the name and version of the servlet container on which the servlet is running. The form of the returned string is
servername/versionnumber.
For example, the JavaServer Web Development Kit may return the string
The servlet container may return other optional information
after the primary string in parentheses, for example,
| |
| getServlet | back to summary |
|---|---|
| public Servlet getServlet(String name) throws ServletException
Deprecated As of Java Servlet API 2.1, with no direct replacement. This method was originally defined to retrieve a servlet
from a In lieu of this method, servlets can share information using the
| |
| getServletContextName | back to summary |
|---|---|
| public String getServletContextName() Returns the name of this web application correponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element.
| |
| getServletNames | back to summary |
|---|---|
| public Enumeration Deprecated As of Java Servlet API 2.1, with no replacement. This method was originally defined to return an
| |
| getServlets | back to summary |
|---|---|
| public Enumeration Deprecated As of Java Servlet API 2.0, with no replacement. This method was originally defined to return an | |
| log | back to summary |
|---|---|
| public void log(String msg) Writes the specified message to a servlet log file, usually an event log. The name and type of the servlet log file is specific to the servlet container.
| |
| log | back to summary |
|---|---|
| public void log(Exception exception, String msg)
Deprecated As of Java Servlet API 2.1, use
This method was originally defined to write an exception's stack trace and an explanatory error message to the servlet log file. | |
| log | back to summary |
|---|---|
| public void log(String message, Throwable throwable) Writes an explanatory message and a stack trace
for a given | |
| removeAttribute | back to summary |
|---|---|
| public void removeAttribute(String name) Removes the attribute with the given name from
the servlet context. After removal, subsequent calls to
If listeners are configured on the
| |
| setAttribute | back to summary |
|---|---|
| public void setAttribute(String name, Object object) Binds an object to a given attribute name in this servlet context. If the name specified is already used for an attribute, this method will replace the attribute with the new to the new attribute. If listeners are configured on the
If a null value is passed, the effect is the same as calling
Attribute names should follow the same convention as package
names. The Java Servlet API specification reserves names
matching | |