public interface ServletContext
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. |
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> |
createFilter(java.lang.Class<T> t)
Instantiates the specified filter class.
|
<T extends java.util.EventListener> |
createListener(java.lang.Class<T> listenerClass)
Instantiates the specified listener class.
|
<T extends Servlet> |
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
RequestDispatcher s
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.
|
static final java.lang.String TEMPDIR
File
temporary directory for the application's private use.static final java.lang.String ORDERED_LIBS
WEB-INF/lib
ordered by their
web fragment names, or null if no relative ordering is specified.java.lang.String getContextPath()
ServletContext getContext(java.lang.String UriPath)
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.UriPath
- The path to the servlet,
such as /servlet/ShowBook
UriPath
or when it is unavailable
due to security restrictionssetAttribute(java.lang.String, java.lang.Object)
int getMajorVersion()
int getMinorVersion()
int getEffectiveMajorVersion()
int getEffectiveMinorVersion()
java.lang.String getMimeType(java.lang.String filename)
filename
- the filejava.util.Set getResourcePaths(java.lang.String pathPrefix)
getResourcePaths ("/images/jpegs/")
might yield the set {"/images/jpegs/a.jpg","/images/jpegs/b.jpg,..."}pathPrefix
- The path starting with "/" used to match against the resourcesjava.net.URL getResource(java.lang.String virtualPath) throws java.net.MalformedURLException
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.
virtualPath
- the path to the requested resource
(e.g. /philosophy/words-to-avoid.html
)java.net.MalformedURLException
URL.openConnection()
,
URLConnection.setDoOutput(boolean)
,
URLConnection.getOutputStream()
java.io.InputStream getResourceAsStream(java.lang.String virtualPath)
getResource(virtualPath).openStream()
.
But the servlet engine is allowed to implement is in a more efficient
way.virtualPath
- the path to the requested resource
(e.g. /philosophy/words-to-avoid.html
)getResource(java.lang.String)
,
URL.openStream()
RequestDispatcher getRequestDispatcher(java.lang.String UriPath)
RequestDispatcher
to forward requests or
include responses from another (active) resource. Some resources can also
be accessed by the getResource
method.UriPath
- the path to another (active) resource
(e.g. /servlet/OtherServlet
)RequestDispatcher
for the (active) resource
found at UriPath
getResource(java.lang.String)
RequestDispatcher getNamedDispatcher(java.lang.String name)
name
- the name of the servlet to wrapServlet getServlet(java.lang.String name) throws ServletException
name
- the name of the wanted servletServletException
- if a servlet related error occuredjava.util.Enumeration getServlets()
Enumeration
.java.util.Enumeration getServletNames()
RequestDispatcher
instead; now this always returns an empty Enumeration
.void log(java.lang.String message)
message
- the message to writevoid log(java.lang.Exception exception, java.lang.String message)
log(String, Throwable)
which is more
general.exception
- the exceptionmessage
- the messagelog(java.lang.String, java.lang.Throwable)
void log(java.lang.String message, java.lang.Throwable t)
t
- the exceptionmessage
- the messagejava.lang.String getRealPath(java.lang.String virtualPath)
getResource
and getResourceAsStream
methods to access the original object in a more abstract way not tied to
the filesystem.virtualPath
- the path to be translated
(e.g. /graphics/baby-gnu.png
)getResource(java.lang.String)
,
getResourceAsStream(java.lang.String)
java.lang.String getServerInfo()
java.lang.String getInitParameter(java.lang.String name)
name
- the name of the init parameterjava.util.Enumeration getInitParameterNames()
boolean setInitParameter(java.lang.String name, java.lang.String value)
java.lang.IllegalStateException
- if the context is already
initializedServletContainerInitializer
java.lang.Object getAttribute(java.lang.String name)
name
- the name of the attributejava.util.Enumeration getAttributeNames()
void setAttribute(java.lang.String name, java.lang.Object object)
ServletContext
.
Can be used to communicate with other servlets in this
ServletContext
. The names used must follow the conventions
used for naming java packages.name
- - which is used to refer to this objectobject
- - which should be returned when somebody calls
getAttribute(name)
getAttribute(java.lang.String)
void removeAttribute(java.lang.String name)
ServletContext
name
- The name which was used to set the object with
setObject
java.lang.String getServletContextName()
ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.String className)
servletName
- the servlet nameclassName
- class name of the servletjava.lang.IllegalStateException
- if the context is already
initializedServletContainerInitializer
ServletRegistration.Dynamic addServlet(java.lang.String servletName, Servlet servlet)
servletName
- the servlet nameservlet
- the servletjava.lang.IllegalStateException
- if the context is already
initializedServletContainerInitializer
ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.Class<? extends Servlet> t)
servletName
- the servlet namet
- class of the servletjava.lang.IllegalStateException
- if the context is already
initializedServletContainerInitializer
<T extends Servlet> T createServlet(java.lang.Class<T> t) throws ServletException
t
- class of the servletServletException
ServletRegistration getServletRegistration(java.lang.String servletName)
servletName
- the servlet namejava.util.Map<java.lang.String,? extends Registration> getServletRegistrations()
FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.String className)
filterName
- the filter nameclassName
- class name of the filterjava.lang.IllegalStateException
- if the context is already
initializedServletContainerInitializer
FilterRegistration.Dynamic addFilter(java.lang.String filterName, Filter filter)
filterName
- the filter namefilter
- the filterjava.lang.IllegalStateException
- if the context is already
initializedServletContainerInitializer
FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.Class<? extends Filter> t)
filterName
- the filter namet
- class of the filterjava.lang.IllegalStateException
- if the context is already
initializedServletContainerInitializer
<T extends Filter> T createFilter(java.lang.Class<T> t) throws ServletException
t
- class of the servletServletException
FilterRegistration getFilterRegistration(java.lang.String filterName)
servletName
- the servlet namejava.util.Map<java.lang.String,? extends FilterRegistration> getFilterRegistrations()
SessionCookieConfig getSessionCookieConfig()
void setSessionTrackingModes(java.util.Set<SessionTrackingMode> set)
java.lang.IllegalStateException
- if the context is already
initializedjava.util.Set<SessionTrackingMode> getDefaultSessionTrackingModes()
java.util.Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
void addListener(java.lang.String className)
java.lang.IllegalStateException
- if the context is already
initializedvoid addListener(java.util.EventListener listener)
java.lang.IllegalStateException
- if the context is already
initializedvoid addListener(java.lang.Class<? extends java.util.EventListener> t)
java.lang.IllegalStateException
- if the context is already
initialized<T extends java.util.EventListener> T createListener(java.lang.Class<T> listenerClass) throws ServletException
t
- class of the servletServletException
JspConfigDescriptor getJspConfigDescriptor()
jsp-config
elements in the web.xml
and/or
web-fragment.xml
descriptors for this context.java.lang.ClassLoader getClassLoader()
void declareRoles(java.lang.String... roleNames)
#isUserInRole
.