Top Description Fields Methods
jakarta.servlet

public Interface ServletContext

Imports
jakarta.servlet.descriptor.JspConfigDescriptor, java.io.InputStream, java.net.MalformedURLException, .URL, java.util.Enumeration, .EventListener, .Map, .Set

Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.

There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)

In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.

The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.

Author
Various
See Also
Servlet#getServletConfig, ServletConfig#getServletContext

Field Summary

Modifier and TypeField and Description
public static final String
ORDERED_LIBS

The name of the ServletContext attribute whose value (of type java.util.List<java.lang.String>) contains the list of names of JAR files in WEB-INF/lib ordered by their web fragment names (with possible exclusions if <absolute-ordering> without any <others/> is being used), or null if no absolute or relative ordering has been specified

public static final String
TEMPDIR

The name of the ServletContext attribute which stores the private temporary directory (of type java.io.File) provided by the servlet container for the ServletContext

Method Summary

Modifier and TypeMethod and Description
public FilterRegistration.Dynamic

Returns:

a FilterRegistration object that may be used to further configure the registered filter, or null if this ServletContext already contains a complete FilterRegistration for a filter with the given filterName
addFilter
(String
the name of the filter
filterName
,
String
the fully qualified class name of the filter
className
)

Adds the filter with the given name and class name to this servlet context.

public FilterRegistration.Dynamic

Returns:

a FilterRegistration object that may be used to further configure the given filter, or null if this ServletContext already contains a complete FilterRegistration for a filter with the given filterName or if the same filter instance has already been registered with this or another ServletContext in the same container
addFilter
(String
the name of the filter
filterName
,
Filter
the filter instance to register
filter
)

Registers the given filter instance with this ServletContext under the given filterName.

public FilterRegistration.Dynamic

Returns:

a FilterRegistration object that may be used to further configure the registered filter, or null if this ServletContext already contains a complete FilterRegistration for a filter with the given filterName
addFilter
(String
the name of the filter
filterName
,
Class<? extends Filter>
the class object from which the filter will be instantiated
filterClass
)

Adds the filter with the given name and class type to this servlet context.

public ServletRegistration.Dynamic

Returns:

a ServletRegistration object that may be used to further configure the registered servlet, or null if this ServletContext already contains a complete ServletRegistration for a servlet with the given servletName
addJspFile
(String
the name of the servlet
servletName
,
String
the full path to a JSP file within the web application beginning with a `/'.
jspFile
)

Adds the servlet with the given jsp file to this servlet context.

public void
addListener(String
the fully qualified class name of the listener
className
)

Adds the listener with the given class name to this ServletContext.

public <
the class of the EventListener to add
T extends EventListener
>
void
addListener(T
the listener to be added
t
)

Adds the given listener to this ServletContext.

public void
addListener(Class<? extends EventListener>
the listener class to be instantiated
listenerClass
)

Adds a listener of the given class type to this ServletContext.

public ServletRegistration.Dynamic

Returns:

a ServletRegistration object that may be used to further configure the registered servlet, or null if this ServletContext already contains a complete ServletRegistration for a servlet with the given servletName
addServlet
(String
the name of the servlet
servletName
,
String
the fully qualified class name of the servlet
className
)

Adds the servlet with the given name and class name to this servlet context.

public ServletRegistration.Dynamic

Returns:

a ServletRegistration object that may be used to further configure the given servlet, or null if this ServletContext already contains a complete ServletRegistration for a servlet with the given servletName or if the same servlet instance has already been registered with this or another ServletContext in the same container
addServlet
(String
the name of the servlet
servletName
,
Servlet
the servlet instance to register
servlet
)

Registers the given servlet instance with this ServletContext under the given servletName.

public ServletRegistration.Dynamic

Returns:

a ServletRegistration object that may be used to further configure the registered servlet, or null if this ServletContext already contains a complete ServletRegistration for the given servletName
addServlet
(String
the name of the servlet
servletName
,
Class<? extends Servlet>
the class object from which the servlet will be instantiated
servletClass
)

Adds the servlet with the given name and class type to this servlet context.

public <
the class of the Filter to create
T extends Filter
>
T

Returns:

the new Filter instance
createFilter
(Class<T>
the Filter class to instantiate
clazz
)

Instantiates the given Filter class.

public <
the class of the EventListener to create
T extends EventListener
>
T

Returns:

the new EventListener instance
createListener
(Class<T>
the EventListener class to instantiate
clazz
)

Instantiates the given EventListener class.

public <
the class of the Servlet to create
T extends Servlet
>
T

Returns:

the new Servlet instance
createServlet
(Class<T>
the Servlet class to instantiate
clazz
)

Instantiates the given Servlet class.

public void
declareRoles(String...
the role names being declared
roleNames
)

Declares role names that are tested using isUserInRole.

public Object

Returns:

an Object containing the value of the attribute, or null if no attribute exists matching the given name.
getAttribute
(String
a String specifying the name of the attribute
name
)

Returns the servlet container attribute with the given name, or null if there is no attribute by that name.

public Enumeration<String>

Returns:

an Enumeration of attribute names
getAttributeNames
()

Returns an Enumeration containing the attribute names available within this ServletContext.

public ClassLoader

Returns:

the class loader of the web application represented by this ServletContext
getClassLoader
()

Gets the class loader of the web application represented by this ServletContext.

public ServletContext

Returns:

the ServletContext object that corresponds to the named URL, or null if either none exists or the container wishes to restrict this access.
getContext
(String
a String specifying the context path of another web application in the container.
uripath
)

Returns a ServletContext object that corresponds to a specified URL on the server.

public String

Returns:

The context path of the web application, or "" for the root context
getContextPath
()

Returns the context path of the web application.

public Set<SessionTrackingMode>

Returns:

set of the session tracking modes supported by default for this ServletContext
getDefaultSessionTrackingModes
()

Gets the session tracking modes that are supported by default for this ServletContext.

public int

Returns:

the major version of the Servlet specification that the application represented by this ServletContext is based on
getEffectiveMajorVersion
()

Gets the major version of the Servlet specification that the application represented by this ServletContext is based on.

public int

Returns:

the minor version of the Servlet specification that the application represented by this ServletContext is based on
getEffectiveMinorVersion
()

Gets the minor version of the Servlet specification that the application represented by this ServletContext is based on.

public Set<SessionTrackingMode>

Returns:

set of the session tracking modes in effect for this ServletContext
getEffectiveSessionTrackingModes
()

Gets the session tracking modes that are in effect for this ServletContext.

public FilterRegistration

Returns:

the (complete or preliminary) FilterRegistration for the filter with the given filterName, or null if no FilterRegistration exists under that name
getFilterRegistration
(String
the name of a filter
filterName
)

Gets the FilterRegistration corresponding to the filter with the given filterName.

public Map<String, ? extends FilterRegistration>

Returns:

Map of the (complete and preliminary) FilterRegistration objects corresponding to all filters currently registered with this ServletContext
getFilterRegistrations
()

Gets a (possibly empty) Map of the FilterRegistration objects (keyed by filter name) corresponding to all filters registered with this ServletContext.

public String

Returns:

a String containing the value of the context's initialization parameter, or null if the context's initialization parameter does not exist.
getInitParameter
(String
a String containing the name of the parameter whose value is requested
name
)

Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

public Enumeration<String>

Returns:

an Enumeration of String objects containing the names of the context's initialization parameters
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.

public JspConfigDescriptor

Returns:

the <jsp-config> related configuration that was aggregated from the web.xml and web-fragment.xml descriptor files of the web application represented by this ServletContext, or null if no such configuration exists
getJspConfigDescriptor
()

Gets the <jsp-config> related configuration that was aggregated from the web.xml and web-fragment.xml descriptor files of the web application represented by this ServletContext.

public int

Returns:

The major version of Jakarta Servlet specification that this container supports
getMajorVersion
()

Returns the major version of Jakarta Servlet specification that this container supports.

public String

Returns:

a String specifying the file's MIME type
getMimeType
(String
a String specifying the name of a file
file
)

Returns the MIME type of the specified file, or null if the MIME type is not known.

public int

Returns:

The minor version of Jakarta Servlet specification that this container supports
getMinorVersion
()

Returns the minor version of Jakarta Servlet specification that this container supports.

public RequestDispatcher

Returns:

a RequestDispatcher object that acts as a wrapper for the named servlet, or null if the ServletContext cannot return a RequestDispatcher
getNamedDispatcher
(String
a String specifying the name of a servlet to wrap
name
)

Returns a RequestDispatcher object that acts as a wrapper for the named servlet.

public String

Returns:

the real path, or null if the translation cannot be performed
getRealPath
(String
the virtual path to be translated to a real path
path
)

Gets the real path corresponding to the given virtual path.

public String

Returns:

the request character encoding that are supported by default for this ServletContext
getRequestCharacterEncoding
()

Gets the request character encoding that are supported by default for this ServletContext.

public RequestDispatcher

Returns:

a RequestDispatcher object that acts as a wrapper for the resource at the specified path, or null if the ServletContext cannot return a RequestDispatcher
getRequestDispatcher
(String
a String specifying the pathname to the resource
path
)

Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

public URL

Returns:

the resource located at the named path, or null if there is no resource at that path
getResource
(String
a String specifying the path to the resource
path
)

Returns a URL to the resource that is mapped to the given path.

public InputStream

Returns:

the InputStream returned to the servlet, or null if no resource exists at the specified path
getResourceAsStream
(String
a String specifying the path to the resource
path
)

Returns the resource located at the named path as an InputStream object.

public Set<String>

Returns:

a Set containing the directory listing, or null if there are no resources in the web application whose path begins with the supplied path.
getResourcePaths
(String
the partial path used to match the resources, which must start with a /
path
)

Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument.

public String

Returns:

the request character encoding that are supported by default for this ServletContext
getResponseCharacterEncoding
()

Gets the response character encoding that are supported by default for this ServletContext.

public String

Returns:

a String containing at least the servlet container name and version number
getServerInfo
()

Returns the name and version of the servlet container on which the servlet is running.

public String

Returns:

The name of the web application or null if no name has been declared in the deployment descriptor.
getServletContextName
()

Returns the name of this web application corresponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element.

public ServletRegistration

Returns:

the (complete or preliminary) ServletRegistration for the servlet with the given servletName, or null if no ServletRegistration exists under that name
getServletRegistration
(String
the name of a servlet
servletName
)

Gets the ServletRegistration corresponding to the servlet with the given servletName.

public Map<String, ? extends ServletRegistration>

Returns:

Map of the (complete and preliminary) ServletRegistration objects corresponding to all servlets currently registered with this ServletContext
getServletRegistrations
()

Gets a (possibly empty) Map of the ServletRegistration objects (keyed by servlet name) corresponding to all servlets registered with this ServletContext.

public SessionCookieConfig

Returns:

the SessionCookieConfig object through which various properties of the session tracking cookies created on behalf of this ServletContext may be configured
getSessionCookieConfig
()

Gets the SessionCookieConfig object through which various properties of the session tracking cookies created on behalf of this ServletContext may be configured.

public int

Returns:

the session timeout in minutes that are supported by default for this ServletContext
getSessionTimeout
()

Gets the session timeout in minutes that are supported by default for this ServletContext.

public String

Returns:

a String containing the configuration name of the logical host on which the servlet context is deployed.
getVirtualServerName
()

Returns the configuration name of the logical host on which the ServletContext is deployed.

public void
log(String
a String specifying the message to be written to the log file
msg
)

Writes the specified message to a servlet log file, usually an event log.

public void
log(String
a String that describes the error or exception
message
,
Throwable
the Throwable error or exception
throwable
)

Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file.

public void
removeAttribute(String
a String specifying the name of the attribute to be removed
name
)

Removes the attribute with the given name from this ServletContext.

public void
setAttribute(String
a String specifying the name of the attribute
name
,
Object
an Object representing the attribute to be bound
object
)

Binds an object to a given attribute name in this ServletContext.

public boolean

Returns:

true if the context initialization parameter with the given name and value was set successfully on this ServletContext, and false if it was not set because this ServletContext already contains a context initialization parameter with a matching name
setInitParameter
(String
the name of the context initialization parameter to set
name
,
String
the value of the context initialization parameter to set
value
)

Sets the context initialization parameter with the given name and value on this ServletContext.

public void
setRequestCharacterEncoding(String
request character encoding
encoding
)

Sets the request character encoding for this ServletContext.

public void
setResponseCharacterEncoding(String
response character encoding
encoding
)

Sets the response character encoding for this ServletContext.

public void
setSessionTimeout(int
session timeout in minutes
sessionTimeout
)

Sets the session timeout in minutes for this ServletContext.

public void
setSessionTrackingModes(Set<SessionTrackingMode>
the set of session tracking modes to become effective for this ServletContext
sessionTrackingModes
)

Sets the session tracking modes that are to become effective for this ServletContext.