JavaScript is disabled on your browser.
javax.servlet

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:
    1.0
    • Field Summary

      Fields
      Modifier and Type Field and Description
      static java.lang.String ORDERED_LIBS
      Servlet context attribute that stores a List of Strings containing the names of JAR files in WEB-INF/lib ordered by their web fragment names, or null if no relative ordering is specified.
      static java.lang.String TEMPDIR
      Servlet context attribute that stores a File temporary directory for the application's private use.
    • Method Summary

      Methods
      Modifier and Type Method and Description
      FilterRegistration.Dynamic addFilter (java.lang.String filterName, java.lang.Class<? extends Filter> t)
      Adds the filter with the specified name and class to this context.
      FilterRegistration.Dynamic addFilter (java.lang.String filterName, Filter filter)
      Adds the filter with the specified name to this context.
      FilterRegistration.Dynamic addFilter (java.lang.String filterName, java.lang.String className)
      Adds the filter with the specified name and class to this context.
      void addListener (java.lang.Class<? extends java.util.EventListener> t)
      Adds the listener with the specified class to this context.
      void addListener (java.util.EventListener listener)
      Adds the specified listener to this context.
      void addListener (java.lang.String className)
      Adds the listener with the specified class name to this context.
      ServletRegistration.Dynamic addServlet (java.lang.String servletName, java.lang.Class<? extends Servlet> t)
      Adds the servlet with the specified name and class to this context.
      ServletRegistration.Dynamic addServlet (java.lang.String servletName, Servlet servlet)
      Adds the servlet with the specified name to this context.
      ServletRegistration.Dynamic addServlet (java.lang.String servletName, java.lang.String className)
      Adds the servlet with the specified name and class to this context.
      <T extends Filter>
      T
      createFilter (java.lang.Class<T> t)
      Instantiates the specified filter class.
      <T extends java.util.EventListener>
      T
      createListener (java.lang.Class<T> listenerClass)
      Instantiates the specified listener class.
      <T extends Servlet>
      T
      createServlet (java.lang.Class<T> t)
      Instantiates the specified servlet class.
      void declareRoles (java.lang.String... roleNames)
      Declare role names for #isUserInRole.
      java.lang.Object getAttribute (java.lang.String name)
      Gets the value of a named attribute
      java.util.Enumeration getAttributeNames ()
      Gets an enumeration containing all the attribute names
      java.lang.ClassLoader getClassLoader ()
      Returns the classloader used by this context to load application classes.
      ServletContext getContext (java.lang.String UriPath)
      Gives the ServletContext of another servlet indicated by the UriPath on the same server.
      java.lang.String getContextPath ()
      Returns the context path of the web application.
      java.util.Set<SessionTrackingMode> getDefaultSessionTrackingModes ()
      Returns the default session tracking modes for this context.
      int getEffectiveMajorVersion ()
      Returns the major version number of the servlet specification that the web application is based on.
      int getEffectiveMinorVersion ()
      Returns the minor version number of the servlet specification that the web application is based on.
      java.util.Set<SessionTrackingMode> getEffectiveSessionTrackingModes ()
      Returns the current session tracking modes for this context.
      FilterRegistration getFilterRegistration (java.lang.String filterName)
      Returns the servlet registration corresponding to the servlet with the specified name.
      java.util.Map<java.lang.String,? extends FilterRegistration> getFilterRegistrations ()
      Returns a map of filter names to registrations for all filters in this context.
      java.lang.String getInitParameter (java.lang.String name)
      get the application initialization parameter associated with the name.
      java.util.Enumeration getInitParameterNames ()
      get a list of the init parameter names.
      JspConfigDescriptor getJspConfigDescriptor ()
      Returns the JSP configuration from the jsp-config elements in the web.xml and/or web-fragment.xml descriptors for this context.
      int getMajorVersion ()
      Major version number of the Servlet API the servlet engine supports.
      java.lang.String getMimeType (java.lang.String filename)
      Gives the mimetype of the requested file
      int getMinorVersion ()
      Minor version number of the Servlet API the servlet engine supports.
      RequestDispatcher getNamedDispatcher (java.lang.String name)
      get a dispatcher which wraps the servlet specified by name.
      java.lang.String getRealPath (java.lang.String virtualPath)
      Translates the requested virtual path to the real filesystem path using the servers knowledge of the document root.
      RequestDispatcher getRequestDispatcher (java.lang.String UriPath)
      Returns a RequestDispatcher to forward requests or include responses from another (active) resource.
      java.net.URL getResource (java.lang.String virtualPath)
      Translates the requested virtual path to an URL object that can be accesed by the servlet.
      java.io.InputStream getResourceAsStream (java.lang.String virtualPath)
      A convenience method for getResource(virtualPath).openStream().
      java.util.Set getResourcePaths (java.lang.String pathPrefix)
      Returns a listing of all the resources that are accessible inside this web application, whose paths start with the path provided specified.
      java.lang.String getServerInfo ()
      A server supplied string containing the server name, version number, etc
      Servlet getServlet (java.lang.String name)
      Deprecated.
      as of Servlet API 2.1, use RequestDispatchers instead; this now always returns null
      java.lang.String getServletContextName ()
      Get the Servlet Context Name
      java.util.Enumeration getServletNames ()
      Deprecated.
      since 2.1, use a RequestDispatcher instead; now this always returns an empty Enumeration.
      ServletRegistration getServletRegistration (java.lang.String servletName)
      Returns the servlet registration corresponding to the servlet with the specified name.
      java.util.Map<java.lang.String,? extends Registration> getServletRegistrations ()
      Returns a map of servlet names to registrations for all servlets in this context.
      java.util.Enumeration getServlets ()
      Deprecated.
      as of Servlet API 2.1, there is no equivalent now. this Always returns an empty Enumeration.
      SessionCookieConfig getSessionCookieConfig ()
      Returns the configuration object for configuring properties of session tracking cookies in this context.
      void log (java.lang.Exception exception, java.lang.String message)
      Deprecated.
      Use log(String, Throwable) which is more general.
      void log (java.lang.String message)
      Writes a message to the log
      void log (java.lang.String message, java.lang.Throwable t)
      Writes an exception + message to the log
      void removeAttribute (java.lang.String name)
      Removes the named object from the ServletContext
      void setAttribute (java.lang.String name, java.lang.Object object)
      Puts a named object into the ServletContext.
      boolean setInitParameter (java.lang.String name, java.lang.String value)
      Sets the specified initialization parameter on this context.
      void setSessionTrackingModes (java.util.Set<SessionTrackingMode> set)
      Sets the session tracking modes for this context.
    • Field Detail

      • TEMPDIR

        static final java.lang.String TEMPDIR
        Servlet context attribute that stores a File temporary directory for the application's private use.
        See Also:
        Constant Field Values
      • ORDERED_LIBS

        static final java.lang.String ORDERED_LIBS
        Servlet context attribute that stores a List of Strings containing the names of JAR files in WEB-INF/lib ordered by their web fragment names, or null if no relative ordering is specified.
        See Also:
        Constant Field Values
    • Method Detail

      • getContextPath

        java.lang.String getContextPath()
        Returns the context path of the web application.
        Since:
        2.5
      • getContext

        ServletContext getContext(java.lang.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:
        2.1
        See Also:
        setAttribute(java.lang.String, java.lang.Object)
      • getMajorVersion

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

        int getMinorVersion()
        Minor version number of the Servlet API the servlet engine supports.
        Since:
        2.1
      • getEffectiveMajorVersion

        int getEffectiveMajorVersion()
        Returns the major version number of the servlet specification that the web application is based on.
        Since:
        3.0
      • getEffectiveMinorVersion

        int getEffectiveMinorVersion()
        Returns the minor version number of the servlet specification that the web application is based on.
        Since:
        3.0
      • getMimeType

        java.lang.String getMimeType(java.lang.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:
        1.0
      • getResourcePaths

        java.util.Set getResourcePaths(java.lang.String pathPrefix)
        Returns a listing of all the resources that are accessible inside this web application, whose paths start with the path provided specified. This path must start with a "/", and all paths in the resulting set will be relative to the root of the web application So a call of getResourcePaths ("/images/jpegs/") might yield the set {"/images/jpegs/a.jpg","/images/jpegs/b.jpg,..."}
        Parameters:
        pathPrefix - The path starting with "/" used to match against the resources
        Returns:
        a Set containing the paths of the matching resources or null if no resources with the specified prefix can be found
        Since:
        2.3
      • getResource

        java.net.URL getResource(java.lang.String virtualPath)
         throws java.net.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:
        java.net.MalformedURLException
        Since:
        2.1
        See Also:
        URL.openConnection(), URLConnection.setDoOutput(boolean), URLConnection.getOutputStream()
      • getResourceAsStream

        java.io.InputStream getResourceAsStream(java.lang.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:
        2.1
        See Also:
        getResource(java.lang.String), URL.openStream()
      • getRequestDispatcher

        RequestDispatcher getRequestDispatcher(java.lang.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:
        2.1
        See Also:
        getResource(java.lang.String)
      • getNamedDispatcher

        RequestDispatcher getNamedDispatcher(java.lang.String name)
        get a dispatcher which wraps the servlet specified by name. Servlet names can be set by the class or within the webapp deployment descriptor or by some other implementation dependant system.
        Parameters:
        name - the name of the servlet to wrap
        Returns:
        the means of dispatching to the servlet
        Since:
        2.2
      • getServlet

        Servlet getServlet(java.lang.String name)
         throws ServletException 
        Deprecated. as of Servlet API 2.1, use RequestDispatchers instead; this now always returns null
        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:
        1.0
      • getServlets

        java.util.Enumeration getServlets()
        Deprecated. as of Servlet API 2.1, there is no equivalent now. this Always returns an empty Enumeration.
        Gets all servlets
        Returns:
        Empty Enumeration, used to return an enumeration containing all loaded servlets including the calling servlet.
        Since:
        1.0
      • getServletNames

        java.util.Enumeration getServletNames()
        Deprecated. since 2.1, use a RequestDispatcher instead; now this always returns an empty Enumeration.
        Gets all servlet names
        Returns:
        Empty Enumeration, used to return an enumeration containing all loaded servlet names including the calling servlet name
        Since:
        2.0
      • log

        void log(java.lang.String message)
        Writes a message to the log
        Parameters:
        message - the message to write
        Since:
        1.0
      • log

        void log(java.lang.Exception exception,
         java.lang.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:
        2.0
        See Also:
        log(java.lang.String, java.lang.Throwable)
      • log

        void log(java.lang.String message,
         java.lang.Throwable t)
        Writes an exception + message to the log
        Parameters:
        t - the exception
        message - the message
        Since:
        2.1
      • getRealPath

        java.lang.String getRealPath(java.lang.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:
        1.0
        See Also:
        getResource(java.lang.String), getResourceAsStream(java.lang.String)
      • getServerInfo

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

        java.lang.String getInitParameter(java.lang.String name)
        get the application initialization parameter associated with the name. The application init parameters are set in the webapp deployment descriptor or in some other implementation defined way.
        Parameters:
        name - the name of the init parameter
        Returns:
        value the value of the init parameter
        Since:
        2.2
      • getInitParameterNames

        java.util.Enumeration getInitParameterNames()
        get a list of the init parameter names.
        Returns:
        the list of the init parameter names as defined in the webapp DD.
        Since:
        2.2
      • setInitParameter

        boolean setInitParameter(java.lang.String name,
         java.lang.String value)
        Sets the specified initialization parameter on this context. This can only be done during web application startup by the container or a container initializer.
        Returns:
        true on success
        Throws:
        java.lang.IllegalStateException - if the context is already initialized
        Since:
        3.0
        See Also:
        ServletContainerInitializer
      • getAttribute

        java.lang.Object getAttribute(java.lang.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:
        1.0
      • getAttributeNames

        java.util.Enumeration getAttributeNames()
        Gets an enumeration containing all the attribute names
        Returns:
        The enumeration containing all the attribute names
        Since:
        2.1
      • setAttribute

        void setAttribute(java.lang.String name,
         java.lang.Object object)
        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:
        2.1
        See Also:
        getAttribute(java.lang.String)
      • removeAttribute

        void removeAttribute(java.lang.String name)
        Removes the named object from the ServletContext
        Parameters:
        name - The name which was used to set the object with setObject
        Since:
        2.1
      • getServletContextName

        java.lang.String getServletContextName()
        Get the Servlet Context Name
        Returns:
        Name of the web-app or null if the context has no name.
        Since:
        2.3
      • addServlet

        ServletRegistration.Dynamic addServlet(java.lang.String servletName,
         java.lang.String className)
        Adds the servlet with the specified name and class to this context. This can only be done during web application startup by the container or a container initializer.
        Parameters:
        servletName - the servlet name
        className - class name of the servlet
        Returns:
        a registration object that can be used for configuration
        Throws:
        java.lang.IllegalStateException - if the context is already initialized
        Since:
        3.0
        See Also:
        ServletContainerInitializer
      • addServlet

        ServletRegistration.Dynamic addServlet(java.lang.String servletName,
         Servlet servlet)
        Adds the servlet with the specified name to this context. This can only be done during web application startup by the container or a container initializer.
        Parameters:
        servletName - the servlet name
        servlet - the servlet
        Returns:
        a registration object that can be used for configuration
        Throws:
        java.lang.IllegalStateException - if the context is already initialized
        Since:
        3.0
        See Also:
        ServletContainerInitializer
      • addServlet

        ServletRegistration.Dynamic addServlet(java.lang.String servletName,
         java.lang.Class<? extends Servlet> t)
        Adds the servlet with the specified name and class to this context. This can only be done during web application startup by the container or a container initializer.
        Parameters:
        servletName - the servlet name
        t - class of the servlet
        Returns:
        a registration object that can be used for configuration
        Throws:
        java.lang.IllegalStateException - if the context is already initialized
        Since:
        3.0
        See Also:
        ServletContainerInitializer
      • createServlet

        <T extends Servlet> T createServlet(java.lang.Class<T> t)
         throws ServletException 
        Instantiates the specified servlet class. The given class must have a zero-arg constructor.
        Parameters:
        t - class of the servlet
        Throws:
        ServletException
        Since:
        3.0
      • getServletRegistration

        ServletRegistration getServletRegistration(java.lang.String servletName)
        Returns the servlet registration corresponding to the servlet with the specified name.
        Parameters:
        servletName - the servlet name
        Since:
        3.0
      • getServletRegistrations

        java.util.Map<java.lang.String,? extends Registration> getServletRegistrations()
        Returns a map of servlet names to registrations for all servlets in this context.
        Since:
        3.0
      • addFilter

        FilterRegistration.Dynamic addFilter(java.lang.String filterName,
         java.lang.String className)
        Adds the filter with the specified name and class to this context. This can only be done during web application startup by the container or a container initializer.
        Parameters:
        filterName - the filter name
        className - class name of the filter
        Returns:
        a registration object that can be used for configuration
        Throws:
        java.lang.IllegalStateException - if the context is already initialized
        Since:
        3.0
        See Also:
        ServletContainerInitializer
      • addFilter

        FilterRegistration.Dynamic addFilter(java.lang.String filterName,
         Filter filter)
        Adds the filter with the specified name to this context. This can only be done during web application startup by the container or a container initializer.
        Parameters:
        filterName - the filter name
        filter - the filter
        Returns:
        a registration object that can be used for configuration
        Throws:
        java.lang.IllegalStateException - if the context is already initialized
        Since:
        3.0
        See Also:
        ServletContainerInitializer
      • addFilter

        FilterRegistration.Dynamic addFilter(java.lang.String filterName,
         java.lang.Class<? extends Filter> t)
        Adds the filter with the specified name and class to this context. This can only be done during web application startup by the container or a container initializer.
        Parameters:
        filterName - the filter name
        t - class of the filter
        Returns:
        a registration object that can be used for configuration
        Throws:
        java.lang.IllegalStateException - if the context is already initialized
        Since:
        3.0
        See Also:
        ServletContainerInitializer
      • createFilter

        <T extends Filter> T createFilter(java.lang.Class<T> t)
         throws ServletException 
        Instantiates the specified filter class. The given class must have a zero-arg constructor.
        Parameters:
        t - class of the servlet
        Throws:
        ServletException
        Since:
        3.0
      • getFilterRegistration

        FilterRegistration getFilterRegistration(java.lang.String filterName)
        Returns the servlet registration corresponding to the servlet with the specified name.
        Parameters:
        servletName - the servlet name
        Since:
        3.0
      • getFilterRegistrations

        java.util.Map<java.lang.String,? extends FilterRegistration> getFilterRegistrations()
        Returns a map of filter names to registrations for all filters in this context.
        Since:
        3.0
      • getSessionCookieConfig

        SessionCookieConfig getSessionCookieConfig()
        Returns the configuration object for configuring properties of session tracking cookies in this context.
        Since:
        3.0
      • setSessionTrackingModes

        void setSessionTrackingModes(java.util.Set<SessionTrackingMode> set)
        Sets the session tracking modes for this context.
        Throws:
        java.lang.IllegalStateException - if the context is already initialized
        Since:
        3.0
      • getDefaultSessionTrackingModes

        java.util.Set<SessionTrackingMode> getDefaultSessionTrackingModes()
        Returns the default session tracking modes for this context.
        Since:
        3.0
      • getEffectiveSessionTrackingModes

        java.util.Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
        Returns the current session tracking modes for this context.
        Since:
        3.0
      • getJspConfigDescriptor

        JspConfigDescriptor getJspConfigDescriptor()
        Returns the JSP configuration from the jsp-config elements in the web.xml and/or web-fragment.xml descriptors for this context.
        Since:
        3.0
      • getClassLoader

        java.lang.ClassLoader getClassLoader()
        Returns the classloader used by this context to load application classes.
        Since:
        3.0
      • declareRoles

        void declareRoles(java.lang.String... roleNames)
        Declare role names for #isUserInRole.
        Since:
        3.0

AltStyle によって変換されたページ (->オリジナル) /