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.
Various
- $
- Version$Method from javax.servlet.ServletContext Summary: |
---|
getAttribute, getAttributeNames, getContext, getContextPath, getInitParameter, getInitParameterNames, getMajorVersion, getMimeType, getMinorVersion, getNamedDispatcher, getRealPath, getRequestDispatcher, getResource, getResourceAsStream, getResourcePaths, getServerInfo, getServlet, getServletContextName, getServletNames, getServlets, log, log, log, removeAttribute, setAttribute |
Method from javax.servlet.ServletContext Detail: |
---|
null if there is no attribute by that name.
An attribute allows a servlet container to give the
servlet additional information not
already provided by this interface. See your
server documentation for information about its attributes.
A list of supported attributes can be retrieved using
getAttributeNames .
The attribute is returned as a |
Enumeration containing the
attribute names available
within this servlet context. Use the
#getAttribute method with an attribute name
to get the value of an attribute. |
ServletContext object that
corresponds to a specified URL on the server.
This method allows servlets to gain access to the context for various parts of the server, and as needed obtain RequestDispatcher objects from the context. The given path must be begin with "/", is interpreted relative to the server's document root and is matched against the context roots of other web applications hosted on this container. In a security conscious environment, the servlet container may
return |
|
String containing the value of the named
context-wide initialization parameter, or null if the
parameter does not exist.
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. |
Enumeration of String objects, or an
empty Enumeration if the context has no initialization
parameters. |
|
null if
the MIME type is not known. The MIME type is determined
by the configuration of the servlet container, and may be specified
in a web application deployment descriptor. Common MIME
types are "text/html" and "image/gif" . |
|
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 ServletConfig#getServletName . This method returns |
String containing the real path
for a given virtual path. For example, the path "/index.html"
returns the absolute file path on the server's filesystem would be
served by a request for "http://host/contextPath/index.html",
where contextPath is the context path of this ServletContext..
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 |
RequestDispatcher object can be used to forward
a request to the resource or to include the resource in a response.
The resource can be dynamic or static.
The pathname must begin with a "/" and is interpreted as relative
to the current context root. Use |
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
|
InputStream object.
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
|
/welcome.html /catalog/index.html /catalog/products.html /catalog/offers/books.html /catalog/offers/music.html /customer/login.jsp /WEB-INF/web.xml /WEB-INF/classes/com.acme.OrderServlet.class, getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"} getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}. |
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,
|
|
|
|
|
|
|
Throwable exception
to the servlet log file. The name and type of the servlet log
file is specific to the servlet container, usually an event log. |
null .
If listeners are configured on the |
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 |