Synopsis
Interface Name: javax.servlet.ServletContext
Superinterface: None
Immediate Subinterfaces: None
Implemented By: None
Availability: Servlet API 1.0 and later
Description
The ServletContext interface defines a set of methods that can be used to communicate with the servlet container in a non-request-specific manner. This includes finding path information, accessing other servlets running on the server, and writing to the server log file. Each web application has a different servlet context.
Interface Declaration
public interface ServletContext { // Methods public abstract Object getAttribute(String name); public abstract Enumeration getAttributeNames(); // New in 2.1 public abstract ServletContext getContext(String uripath); // New in 2.1 public abstract String getInitParameter(String name); // New in 2.2 public abstract Enumeration getInitParameterNames(); // New in 2.2 public abstract int getMajorVersion(); // New in 2.1 public abstract String getMimeType(String file); public abstract int getMinorVersion(); // New in 2.1 public abstract RequestDispatcher getNamedDispatcher(String name); // New in 2.2 public abstract String getRealPath(String path); public abstract URL getResource(String path) // New in 2.1 throws MalformedURLException; public abstract InputStream getResourceAsStream(String path); // New in 2.1 public abstract String getServerInfo(); public abstract Servlet getServlet(String name) // Deprecated throws ServletException; public abstract Enumeration getServletNames(); // Deprecated public abstract Enumeration getServlets(); // Deprecated public abstract void log(Exception exception, String msg); // Deprecated public abstract void log(String msg); public abstract void log(String msg, Throwable t); // New in 2.1 public abstract void removeAttribute(String name); // New in 2.1 public abstract void setAttribute(String name, Object o); // New in 2.1 }
Methods
getAttribute()
public abstract Object getAttribute(String name)
- Description
-
Returns the value of the named context attribute as an Object or null if the attribute does not exist. Server-specific attributes may be autoset by the servlet container to provide servlets with information above and beyond that provided for by the base Servlet API. Attributes can also be set programmatically by servlets as a way of sharing information throughout the web application represented by this context. Attribute names should follow the same convention as package names. The package names java.* and javax.* are reserved for use by the Java Software division of Sun Microsystems (formerly known as JavaSoft), and com.sun.* is reserved for use by Sun Microsystems. See your server's documentation for a list of its built-in attributes. Remember that servlets relying on server-specific attributes are not portable.
getAttributeNames()
public abstract Enumeration getAttributeNames()
- Description
-
Returns the names of all the current context attributes as an Enumeration of String objects. It returns an empty Enumeration if the context has no attributes. This method was introduced in Servlet API 2.1.
getContext()
public abstract ServletContext getContext(String uripath)
- Description
-
Returns the ServletContext instance assigned to the specified URI path. The given path must be absolute (beginning with / ) and is interpreted based on the server's root. This method allows a servlet to gain access to a context outside its own, letting it view data inside that context or obtain a RequestDispatcher to resources within that context. In a security-conscious or distributed environment, the servlet container may return null for any and all paths. This method was introduced in Servlet API 2.1.
getInitParameter()
public abstract String getInitParameter(String name)
- Description
-
Returns the value of the named context initialization parameter or null if no matching parameter is found. 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. Context init parameters are assigned in the web application deployment descriptor. This method was introduced in Servlet API 2.2.
getInitParameterNames()
public abstract Enumeration getInitParameterNames()
- Description
-
Returns the names of all the init parameters for this context as an Enumeration of String objects. It returns an empty Enumeration if the context has no attributes. Context init parameters are assigned in the web application deployment descriptor. This method was introduced in Servlet API 2.2.
getMajorVersion()
public abstract int getMajorVersion()
- Description
-
Returns the major version of the Servlet API that this container supports. For example, a container implementing 2.1 returns 2. This method was introduced in Servlet API 2.1.
getMimeType()
public abstract String getMimeType(String file)
- Description
-
Returns the MIME type of the given file or null if it is not known. Some implementations return text/plain if the specified file does not exist. Common MIME types are text/html, text/plain, image/gif, and image/jpeg.
getMinorVersion()
public abstract int getMinorVersion()
- Description
-
Returns the minor version of the Servlet API that this container supports. For example, a container implementing 2.1 returns 1. This method was introduced in Servlet API 2.1.
getNamedDispatcher()
public abstract RequestDispatcher getNamedDispatcher(String name)
- Description
-
Returns a RequestDispatcher to dispatch to a resource given by name, instead of by path. This allows dispatching to resources that are not necessarily publicly available. Servlets (and JSP pages also) may be given names via server administration or via the web application deployment descriptor. The method returns null if the context cannot return a dispatcher for any reason. This method was introduced in Servlet API 2.2.
getRealPath()
public abstract String getRealPath(String path)
- Description
-
Returns the real filesystem path of any given "virtual path" or null if the translation cannot be performed (such as when the file is on a remote filesystem or is found only inside a .war archive). If the given path is /, the method returns the document root for the context. If the given path is the same as the one returned by getPathInfo( ), the method returns the same real path as would be returned by getPathTranslated( ). There is no CGI counterpart.
getResource()
public abstract URL getResource(String path)
- Description
-
Returns a URL to the resource mapped to the specified path. The path must begin with a / , and is interpreted as relative to the context root. The method may return null if no resource could be associated with the path. 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 filesystem, in a database, or in a .war file. Some containers may also allow writing to the URL object using the methods of the URL class. This method should be used when not all resources are local files such as in a distributed environment (where a servlet container may be on a different host than the resource) or when content is coming from a .war file (where the files cannot be accessed directly). The resource content is returned raw, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of a JSP execution. This method has a different purpose than Class.getResource( ), which looks up resources based on a class loader. This method does not use class loaders. This method was introduced in Servlet API 2.1.
getResourceAsStream()
public abstract InputStream getResourceAsStream(String path)
- Description
-
Returns an InputStream to read the content of the resource mapped to the specified path. The path must begin with a / , and is interpreted as relative to the context root. The method may return null if no resource could be associated with the path. Using this method is often more convenient than using getResource( ); however, metainformation about the resource such as content length and content type that is available via getResource( ) is lost when using this method. This method was introduced in Servlet API 2.1.
getServerInfo()
public abstract String getServerInfo()
- Description
-
Returns the name and version of the server software, separated by a forward slash (/). The value is the same as the CGI variable SERVER_SOFTWARE.
getServlet()
public abstract Servlet getServlet(String name) throws ServletException
- Description
-
Returns null in Servlet API 2.1 and later. Previously returned the loaded servlet matching the given name or null if the servlet was not found. This method has been deprecated and defined to return null as of Servlet API 2.1 because direct access to another servlet instance opens up too many opportunities for error. The reasoning goes that servlets may be destroyed by the servlet container at any time, so no object but the container should hold a direct reference to a servlet. Also, on a server that supports load balancing where servlets are distributed across a number of servers, it may be impossible even to return a local servlet reference. Servlets instead should collaborate by using shared ServletContext attributes. Technically, defining this method to return null does not break backward compatibility because the servlet container always had the option of returning null on this method for any reason.
getServletNames()
public abstract Enumeration getServletNames()
- Description
-
Returns an empty Enumeration in Servlet API 2.1 and later. Previously returned an Enumeration of the names of the servlet objects loaded in this context. This method has been deprecated and defined to return an empty Enumeration as of Servlet API 2.1 for the same reasons getServlet( ) was deprecated and defined to return null. This method was introduced in Servlet API 2.0.
getServlets()
public abstract Enumeration getServlets() throws ServletException
- Description
-
Returns an empty Enumeration in Servlet API 2.1 and later. Previously returned an Enumeration of the Servlet objects loaded in this context. This method was deprecated in Servlet API 2.0 in favor of getServletNames( ). It was defined to return an empty Enumeration in Servlet API 2.1 following the behavior of getServletNames( ).
log()
public abstract void log(String msg)
- Description
-
Writes the given message to a servlet log. The output location is server-specific, usually an event log file.
public abstract void log(String msg, Throwable t)
- Description
-
Writes the given message and the stack trace of the Throwable to a servlet log. The output location is server-specific, usually an event log file. This method was introduced in Servlet API 2.1.
public abstract void log(Exception exception, String msg)
- Description
-
Writes the given message and the stack trace of the Exception to a servlet log. The output location is server-specific. Notice the nonstandard placement of the optional Exception parameter as the first parameter instead of the last. This method was deprecated in Servlet API 2.1 in favor of log(String msg, Throwable t), a method which follows the standard parameter ordering and also allows any Throwable to be logged, not just an Exception. This method was introduced in Servlet API 2.0.
removeAttribute()
public abstract void removeAttribute(String name)
- Description
-
Removes the attribute with the given name from the context. Attributes should be removed when no longer needed to eliminate memory bloat. This method was introduced in Servlet API 2.1.
setAttribute()
public abstract void setAttribute(String name, Object o)
- Description
-
Binds an object under a given name in this servlet context. Any existing binding with the same name is replaced. This method was introduced in Servlet API 2.1.