您的位置:寻梦网首页编程乐园>Java 天地>Core Java
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Interface javax.servlet.ServletContext

public interface ServletContext
The ServletContext interface gives servlets access to information about their environment, and allows them to log significant events. Servlet writers decide what data to log. The interface is implemented by services, and used by servlets. Different virtual hosts should have different servlet contexts.

Servlets get the ServletContext object via the getServletContext method of ServletConfig. The ServletConfig object is provided to the servlet at initialization, and is accessible via the servlet's getServletConfig method.

See Also:
getServletConfig, getServletContext

Method Index

 o getAttribute(String)
Returns the value of the named attribute of the network service, or null if the attribute does not exist.
 o getMimeType(String)
Returns the mime type of the specified file, or null if not known.
 o getRealPath(String)
Applies alias rules to the specified virtual path and returns the corresponding real path.
 o getServerInfo()
Returns the name and version of the network service under which the servlet is running.
 o getServlet(String)
Returns the servlet of the specified name, or null if not found.
 o getServletNames()
Returns an enumeration of the Servlet object names in this server.
 o getServlets()
Returns an enumeration of the Servlet objects in this server. Deprecated.
 o log(Exception, String)
Write the stacktrace and the given message string to the servlet log file.
 o log(String)
Writes the given message string to the servlet log file.

Methods

 o getServlet
 public abstract Servlet getServlet(String name) throws ServletException
Returns the servlet of the specified name, or null if not found. When the servlet is returned it is initialized and ready to accept service requests.

Note: This is a dangerous method to call for the following reasons.

  • When this method is called the state of the servlet may not be known, and this could cause problems with the server's servlet state machine.
  • It is a security risk to allow any servlet to be able to access the methods of another servlet.

Parameters:
name - the name of the desired servlet
Throws: ServletException
if the servlet could not be initialized
 o getServlets
 public abstract Enumeration getServlets()
Note: getServlets() is deprecated. Please use getServletNames in conjunction with getServlet

Returns an enumeration of the Servlet objects in this server. Only servlets that are accessible (i.e., from the same namespace) will be returned. The enumeration always includes the servlet itself.

Note: This is a dangerous method to call for the following reasons.

  • When this method is called the state of the servlet may not be known, and this could cause problems with the server's servlet state machine.
  • It is a security risk to allow any servlet to be able to access the methods of another servlet.

See Also:
getServletNames, getServlet
 o getServletNames
 public abstract Enumeration getServletNames()
Returns an enumeration of the Servlet object names in this server. Only servlets that are accessible (i.e., from the same namespace) will be returned. The enumeration always includes the servlet itself.

Note: This is a dangerous method to call for the following reasons.

  • When this method is called the state of the servlet may not be known, and this could cause problems with the server's servlet state machine.
  • It is a security risk to allow any servlet to be able to access the methods of another servlet.

 o log
 public abstract void log(String msg)
Writes the given message string to the servlet log file. The name of the servlet log file is server specific; it is normally an event log.

Parameters:
msg - the message to be written
 o log
 public abstract void log(Exception exception,
                          String msg)
Write the stacktrace and the given message string to the servlet log file. The name of the servlet log file is server specific; it is normally an event log.

Parameters:
exception - the exception to be written
msg - the message to be written
 o getRealPath
 public abstract String getRealPath(String path)
Applies alias rules to the specified virtual path and returns the corresponding real path. For example, in an HTTP servlet, this method would resolve the path against the HTTP service's docroot. Returns null if virtual paths are not supported, or if the translation could not be performed for any reason.

Parameters:
path - the virtual path to be translated into a real path
 o getMimeType
 public abstract String getMimeType(String file)
Returns the mime type of the specified file, or null if not known.

Parameters:
file - name of the file whose mime type is required
 o getServerInfo
 public abstract String getServerInfo()
Returns the name and version of the network service under which the servlet is running. For example, if the network service was an HTTP service, then this would be the same as the CGI variable SERVER_SOFTWARE.

 o getAttribute
 public abstract Object getAttribute(String name)
Returns the value of the named attribute of the network service, or null if the attribute does not exist. This method allows access to additional information about the service, not already provided by the other methods in this interface. Attribute names should follow the same convention as package names. The package names java.* and javax.* are reserved for use by Javasoft, and com.sun.* is reserved for use by Sun Microsystems.

Parameters:
name - the name of the attribute whose value is required
Returns:
the value of the attribute, or null if the attribute does not exist.

All Packages  Class Hierarchy  This Package  Previous  Next  Index