Interface ServletContext


public interface ServletContext
A class created by the server to give servlets access to certain environment related objects and methods.
It contains standard information like the names of all the servlets, two kinds of log methods, the server name, etc.
The server can also store extra information here in the form of {String, Object} pairs. Different servlets can live in different ServletContexts, but a servlet engine can group related Servlets in the same ServletContext.
Servlet specific information can be transferred to a servlet using a class implementing the ServletConfig interface.
Since:
Servlet API 1.0
Version:
Servlet API 2.2
  • Method Details

    • getAttribute

      Object getAttribute(String name)
      Gets the value of a named attribute
      Parameters:
      name - the name of the attribute
      Returns:
      the value of the attribute or null if there is no attribute with this name
      Since:
      Servlet API 1.0
    • getAttributeNames

      Enumeration getAttributeNames()
      Gets an enumeration containing all the attribute names
      Returns:
      The enumeration containing all the attribute names
      Since:
      Servlet API 2.1
    • getContext

      ServletContext getContext(String UriPath)
      Gives the ServletContext of another servlet indicated by the UriPath on the same server. For security reasons this can return null even if there is an active servlet at that location. This can then be used to set attributes in the context of another servlet.
      Parameters:
      UriPath - The path to the servlet, such as /servlet/ShowBook
      Returns:
      ServletContext of the requested servlet or null if there is no servlet at the requested UriPath or when it is unavailable due to security restrictions
      Since:
      Servlet API 2.1
      See Also:
    • getMajorVersion

      int getMajorVersion()
      Major version number of the Servlet API the servlet engine supports.
      Returns:
      2 if the 2.1 Servlet API is supported
      Since:
      Servlet API 2.1
    • getMinorVersion

      int getMinorVersion()
      Minor version number of the Servlet API the servlet engine supports.
      Returns:
      1 if the 2.1 Servlet API is supported
      Since:
      Servlet API 2.1
    • getMimeType

      String getMimeType(String filename)
      Gives the mimetype of the requested file
      Parameters:
      filename - the file
      Returns:
      a String containing the mime type or null if the mime type cannot be determined
      Since:
      Servlet API 1.0
    • getRealPath

      String getRealPath(String virtualPath)
      Translates the requested virtual path to the real filesystem path using the servers knowledge of the document root. Use the getResource and getResourceAsStream methods to access the original object in a more abstract way not tied to the filesystem.
      Parameters:
      virtualPath - the path to be translated (e.g. /graphics/baby-gnu.png)
      Returns:
      the translated real filesystem path or null if the real system path cannot be found
      Since:
      Servlet API 1.0
      See Also:
    • getResource

      URL getResource(String virtualPath) throws MalformedURLException
      Translates the requested virtual path to an URL object that can be accesed by the servlet. This is more generic than the getRealPath method since it is not tied to the local filesystem. This means that a servlet can access the resource even when loaded in a servlet engine on a different machine. The servlet engine should make sure that the appropriate URLStreamHandlers and URLConnection classes are implemented to acces to resource.

      This can also be used to write to a resource if the resource (URLConnection) supports it. The following example gives you an OutputStream:

      URLConnection con = getResource("/logs/mylog.txt").openConnection();
      con.setDoOutput(true);
      OutputStream out = con.getOutputStream();

      Note that a ServerContext does not have to have access to the complete servers document space and is allowed to return null even for valid virtual paths.

      Note that according to the 2.1 API documentation this method can throw a MalformedURLException. But according to the official spec it does not throw any exceptions.

      Parameters:
      virtualPath - the path to the requested resource (e.g. /philosophy/words-to-avoid.html)
      Returns:
      the URL that can be used to access the resource or null if the resource cannot be found
      Throws:
      MalformedURLException
      Since:
      Servlet API 2.1
      See Also:
    • getResourceAsStream

      InputStream getResourceAsStream(String virtualPath)
      A convenience method for getResource(virtualPath).openStream(). But the servlet engine is allowed to implement is in a more efficient way.
      Parameters:
      virtualPath - the path to the requested resource (e.g. /philosophy/words-to-avoid.html)
      Returns:
      the InputStream that can be used to read the resource or null if the resource cannot be found
      Since:
      Servlet API 2.1
      See Also:
    • getRequestDispatcher

      RequestDispatcher getRequestDispatcher(String UriPath)
      Returns a RequestDispatcher to forward requests or include responses from another (active) resource. Some resources can also be accessed by the getResource method.
      Parameters:
      UriPath - the path to another (active) resource (e.g. /servlet/OtherServlet)
      Returns:
      an RequestDispatcher for the (active) resource found at UriPath
      Since:
      Servlet API 2.1
      See Also:
    • getNamedDispatcher

      RequestDispatcher getNamedDispatcher(String name)
      XXX
    • getServerInfo

      String getServerInfo()
      A server supplied string containing the server name, version number, etc
      Returns:
      the string
      Since:
      Servlet API 1.0
    • getInitParameter

      String getInitParameter(String name)
      XXX
    • getInitParameterNames

      Enumeration getInitParameterNames()
      XXX
    • log

      void log(String message)
      Writes a message to the log
      Parameters:
      message - the message to write
      Since:
      Servlet API 1.0
    • log

      void log(String message, Throwable t)
      Writes an exception + message to the log
      Parameters:
      message - the message
      t - the exception
      Since:
      Servlet API 2.1
    • log

      void log(Exception exception, String message)
      Deprecated.
      Use log(String, Throwable) which is more general.
      Writes an exception + message to the log
      Parameters:
      exception - the exception
      message - the message
      Since:
      Servlet API 2.0
      See Also:
    • setAttribute

      void setAttribute(String name, Object o)
      Puts a named object into the ServletContext. Can be used to communicate with other servlets in this ServletContext. The names used must follow the conventions used for naming java packages.
      Parameters:
      name - - which is used to refer to this object
      object - - which should be returned when somebody calls getAttribute(name)
      Since:
      Servlet API 2.1
      See Also:
    • removeAttribute

      void removeAttribute(String name)
      Removes the named object from the ServletContext
      Parameters:
      name - The name which was used to set the object with setObject
      Since:
      Servlet API 2.1
    • getServlet

      Servlet getServlet(String name) throws ServletException
      Deprecated.
      Always returns null. Since the servlet engine cannot know if a servlet ever gives up the reference to another servlet it could never destroy the servlet after this call. Only the servlet engine should have references to Servlets.
      Gets a specific servlet by name. The Servlet is guaranteed to accept service requests.
      Parameters:
      name - the name of the wanted servlet
      Returns:
      null, used to return the servlet or null if not loaded.
      Throws:
      ServletException - if a servlet related error occured
      Since:
      Servlet API 1.0
    • getServlets

      Enumeration getServlets()
      Deprecated.
      Always returns an empty Enumeration. Only the servlet engine should have references to Servlets.
      Gets all servlets
      Returns:
      Empty Enumeration, used to return an enumeration containing all loaded servlets including the calling servlet.
      Since:
      Servlet API 1.0
    • getServletNames

      Enumeration getServletNames()
      Deprecated.
      Always returns an empty Enumeration. Only the servlet engine should have references to Servlets.
      Gets all servlet names
      Returns:
      Empty Enumeration, used to return an enumeration containing all loaded servlet names including the calling servlet name
      Since:
      Servlet API 2.0