application |
Variable name:
application
Interface name:
javax.servlet.ServletContext
Extends:
None
Implemented by:
Internal container-dependent class
JSP Page type:
Available in both regular JSP pages and error pages
Description
The ServletContext provides resources shared within a web application. It holds attribute values representing the JSP application scope. An attribute value can be an instance of any valid Java class. The ServletContext also defines a set of methods that a JSP page or a servlet uses to communicate with its container; for example, to get the MIME type of a file, dispatch requests, or write to a log file. The web container is responsible for providing an implementation of the ServletContext interface.

Each ServletContext is assigned a specific URI path prefix within a web server. For example, a context could be responsible for all resources under http://www.mycorp.com/catalog. All requests that start with the /catalog request path, which is known as the context path, are routed to this servlet context.

Only one instance of a ServletContext may be available to the servlets and JSP pages in a web application. If the web application indicates that it is distributable, there must be only one instance of the ServletContext object in use per application in each Java Virtual Machine.

getAttribute() |
public Object getAttribute(String name)
Returns the servlet context attribute with the specified name, or null if there is no attribute by that name. Context attributes can be set by a servlet or a JSP page, representing the JSP application scope. A container can also use attributes to provide information that is not already available through methods in this interface.

getAttributeNames() |
public java.util.Enumeration getAttributeNames()
Returns an Enumeration of String objects containing the attribute names available within this servlet context.

getContext() |
public ServletContext getContext(String uripath)
Returns a ServletContext object that corresponds to a specified URI in the web container. This method allows servlets and JSP pages to gain access to contexts other than their own. The URI path must be absolute (beginning with "/") and is interpreted based on the containers' document root. In a security-conscious environment, the container may return null for a given URI.

getInitParameter() |
public String getInitParameter(String name)
Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist. Context initialization parameters can be defined in the web application deployment descriptor.

getInitParameterNames() |
public java.util.Enumeration getInitParameterNames()
Returns the names of the context's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.

getMajorVersion() |
public int getMajorVersion()
Returns the major version of the Java Servlet API the web container supports. A container that complies with the Servlet 2.3 API returns 2.

getMimeType() |
public String getMimeType(String filename)
Returns the MIME type of the specified file, or null if the MIME type is not known. The MIME type is determined by the configuration of the web container and may be specified in a web application deployment descriptor.

getMinorVersion() |
public int getMinorVersion()
Returns the minor version of the Java Servlet API the web container supports. A container that complies with the Servlet 2.3 API returns 3.

getNamedDispatcher() |
public RequestDispatcher getNamedDispatcher(String name)
Returns a RequestDispatcher object that acts as a wrapper for the named servlet or JSP page. Names can be defined for servlets and JSP pages in the web application deployment descriptor.

getRealPath() |
public String getRealPath(String path)
Returns a String containing the filesystem path for specified context-relative path. This method returns null if the web container cannot translate the path to a filesystem path for any reason (such as when the content is being made available from a WAR archive).

getRequestDispatcher() |
public RequestDispatcher getRequestDispatcher(String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the specified context-relative path. The resource can be dynamic (servlet or JSP) or static (e.g., a regular HTML file).

getResource() |
public java.net.URL getResource(String path) throws MalformedURLException
Returns a URL to the resource that is mapped to the specified context-relative path. This method allows the web container to make a resource available to servlets and JSP pages from sources other than a local filesystem, such as a database or a WAR file.

The URL provides access to the resource content direct, so be aware that requesting a JSP page returns a URL for the JSP source code as opposed to the processed result. Use a RequestDispatcher instead to include the results of an execution.

This method returns null if no resource is mapped to the pathname.

getResourceAsStream() |
public java.io.InputStream getResourceAsStream(String path)
Returns the resource mapped to the specified context-relative path as an InputStream object. See the getResource() method for details.

getServerInfo() |
public String getServerInfo()
Returns the name and version of the servlet container on which the servlet or JSP page is running as a String with the format servername/versionnumber (for example, Tomcat/3.2). A container may include other optional information, such as the Java version and operating system information, within parentheses.

log() |
public void log(String message) public void log(String message, Throwable cause)
Writes the specified message to a web container log file, or the specified message and a stack trace for the specified Throwable to the servlet log file. The name and type of the log file are container-dependent.

setAttribute() |
public void setAttribute(String name, Object attribute)
Binds an object to the specified attribute name in this servlet context. If the specified name is already used for an attribute, this method removes the old attribute and binds the name to the new attribute.

getServlet() | Deprecated
public Servlet getServlet(String name) throws ServletException
This method was originally defined to retrieve a servlet from a ServletContext. As of the Servlet 2.1 API, this method always returns null and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.

getServlets() | Deprecated
public Enumeration getServlets()
This method was originally defined to return an Enumeration of all the servlets known to this servlet context. As of the Servlet 2.1 API, this method always returns an empty Enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.

getServletNames() | Deprecated
public Enumeration getServletNames()
This method was originally defined to return an Enumeration of all the servlet names known to this context. As of Servlet 2.1, this method always returns an empty Enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.

log() | Deprecated
public void log(Exception exception, String message)
This method was originally defined to write an exception's stack trace and an explanatory error message to the web container log file. As of the Servlet 2.1 API, the recommendation is to use log(String, Throwable) instead.