Top Description Fields Methods
jakarta.servlet

public Interface ServletContext

Known Direct Implementers
org.apache.catalina.core.ApplicationContext, org.apache.catalina.core.ApplicationContextFacade, org.apache.catalina.core.StandardContext.NoPluggabilityServletContext
Imports
java.io.InputStream, java.net.MalformedURLException, .URL, java.util.Enumeration, .EventListener, .Map, .Set, jakarta.servlet.descriptor.JspConfigDescriptor

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.

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 that holds the ordered list of web fragments for this web application.

public static final String
TEMPDIR

The name of the ServletContext attribute that holds the temporary file location for the web application.

Method Summary

Modifier and TypeMethod and Description
public FilterRegistration.Dynamic

Returns:

null if the filter has already been fully defined, else a jakarta.servlet.FilterRegistration.Dynamic object that can be used to further configure the filter
addFilter
(String
Name of filter to add
filterName
,
String
Name of filter class
className
)

Add filter to context.

public FilterRegistration.Dynamic

Returns:

null if the filter has already been fully defined, else a jakarta.servlet.FilterRegistration.Dynamic object that can be used to further configure the filter
addFilter
(String
Name of filter to add
filterName
,
Filter
Filter to add
filter
)

Add filter to context.

public FilterRegistration.Dynamic

Returns:

null if the filter has already been fully defined, else a jakarta.servlet.FilterRegistration.Dynamic object that can be used to further configure the filter
addFilter
(String
Name of filter to add
filterName
,
Class<? extends Filter>
Class of filter to add
filterClass
)

Add filter to context.

public ServletRegistration.Dynamic

Returns:

a jakarta.servlet.ServletRegistration.Dynamic object that can be used to further configure the servlet
addJspFile
(String
The servlet name under which this JSP file should be registered
jspName
,
String
The path, relative to the web application root, for the JSP file to be used for this servlet
jspFile
)

Add a JSP to the context.

public void
addListener(String
TODO
className
)

TODO SERVLET3 - Add comments

public <
TODO
T extends EventListener
>
void
addListener(T
TODO
t
)

TODO SERVLET3 - Add comments

public void
addListener(Class<? extends EventListener>
TODO
listenerClass
)

TODO SERVLET3 - Add comments

public ServletRegistration.Dynamic

Returns:

The registration object that enables further configuration
addServlet
(String
The name of the servlet to register
servletName
,
String
The implementation class for the servlet
className
)

Register a servlet implementation for use in this ServletContext.

public ServletRegistration.Dynamic

Returns:

The registration object that enables further configuration
addServlet
(String
The name of the servlet to register
servletName
,
Servlet
The Servlet instance to register
servlet
)

Register a servlet instance for use in this ServletContext.

public ServletRegistration.Dynamic

Returns:

null if the servlet has already been fully defined, else a jakarta.servlet.ServletRegistration.Dynamic object that can be used to further configure the servlet
addServlet
(String
Name of servlet to add
servletName
,
Class<? extends Servlet>
Class of servlet to add
servletClass
)

Add servlet to the context.

public <
The type for the given class
T extends Filter
>
T

Returns:

The created Filter instance.
createFilter
(Class<T>
The the class for which an instance should be created
c
)

Create a Filter instance using the given class.

public <
TODO
T extends EventListener
>
T

Returns:

TODO
createListener
(Class<T>
TODO
c
)

TODO SERVLET3 - Add comments

public <
The type for the given class
T extends Servlet
>
T

Returns:

The created Servlet instance.
createServlet
(Class<T>
The the class for which an instance should be created
c
)

Create an Servlet instance using the given class.

public void
declareRoles(String...
The roles to add
roleNames
)

Add to the declared roles for this ServletContext.

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 servlet context.

public ClassLoader

Returns:

The associated web application class loader
getClassLoader
()

Get the web application class loader associated with 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 main context path
getContextPath
()

Return the main path associated with this context.

public Set<SessionTrackingMode>

Returns:

The set of default session tracking modes for this web application
getDefaultSessionTrackingModes
()

Obtains the default session tracking modes for this web application.

public int

Returns:

The major version declared in web.xml
getEffectiveMajorVersion
()

Obtain the major version of the servlet specification for which this web application is implemented.

public int

Returns:

The minor version declared in web.xml
getEffectiveMinorVersion
()

Obtain the minor version of the servlet specification for which this web application is implemented.

public Set<SessionTrackingMode>

Returns:

The value supplied via setSessionTrackingModes(Set) if one was previously set, else return the defaults
getEffectiveSessionTrackingModes
()

Obtains the currently enabled session tracking modes for this web application.

public FilterRegistration

Returns:

TODO
getFilterRegistration
(String
TODO
filterName
)

TODO SERVLET3 - Add comments

public Map<String, ? extends FilterRegistration>

Returns:

TODO
getFilterRegistrations
()

public String

Returns:

a String containing the value of the initialization parameter
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:

TODO
getJspConfigDescriptor
()

public int

Returns:

6
getMajorVersion
()

Returns the major version of the Java Servlet API that this servlet 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:

0
getMinorVersion
()

Returns the minor version of the Servlet API that this servlet 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:

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

Returns a String containing the real path for a given virtual path.

public String

Returns:

The character encoding name or null if no default has been specified
getRequestCharacterEncoding
()

Get the default character encoding for reading request bodies.

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 a specified 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 character encoding name or null if no default has been specified
getResponseCharacterEncoding
()

Get the default character encoding for writing response bodies.

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 registration details for the named Servlet or null if no Servlet has been registered with the given name
getServletRegistration
(String
The name of the Servlet of interest
servletName
)

Obtain the details of the named servlet.

public Map<String, ? extends ServletRegistration>

Returns:

A Map of servlet names to servlet registrations for all servlets registered with this context
getServletRegistrations
()

Obtain a Map of servlet names to servlet registrations for all servlets registered with this context.

public SessionCookieConfig

Returns:

TODO
getSessionCookieConfig
()

public int

Returns:

The current default session timeout in minutes
getSessionTimeout
()

Get the default session timeout.

public String

Returns:

The primary name of the virtual host on which this context is deployed
getVirtualServerName
()

Get the primary name of the virtual host on which this context 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 the servlet context.

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 servlet context.

public boolean

Returns:

true if the call succeeds or false if the call fails because an initialisation parameter with the same name has already been set
setInitParameter
(String
Name of initialisation parameter
name
,
String
Value for initialisation parameter
value
)

Set the given initialisation parameter to the given value.

public void
setRequestCharacterEncoding(String
The name of the character encoding to use
encoding
)

Set the default character encoding to use for reading request bodies.

public void
setResponseCharacterEncoding(String
The name of the character encoding to use
encoding
)

Set the default character encoding to use for writing response bodies.

public void
setSessionTimeout(int
The new default session timeout in minutes.
sessionTimeout
)

Set the default session timeout.

public void
setSessionTrackingModes(Set<SessionTrackingMode>
The session tracking modes to use for this web application
sessionTrackingModes
)

Configures the available session tracking modes for this web application.