public class WebappContext extends Object implements javax.servlet.ServletContext
This class acts as the foundation for registering listeners, servlets, and filters in an embedded environment.
Additionally, this class implements the the requirements set forth by the
Servlet 2.5 specification of ServletContext
, however, it also exposes
the dynamic registration API of Servlet 3.0.
TODO: Provide code examples once the api firms up a bit.
Modifier and Type | Field and Description |
---|---|
protected boolean |
deployed |
protected Map<String,FilterRegistration> |
filterRegistrations |
protected Map<String,ServletRegistration> |
servletRegistrations |
protected Map<String,FilterRegistration> |
unmodifiableFilterRegistrations |
Modifier | Constructor and Description |
---|---|
protected |
WebappContext() |
|
WebappContext(String displayName)
Creates a simple
WebappContext with the root being "/". |
|
WebappContext(String displayName,
String contextPath) |
|
WebappContext(String displayName,
String contextPath,
String basePath) |
Modifier and Type | Method and Description |
---|---|
void |
addContextInitParameter(String name,
String value) |
FilterRegistration |
addFilter(String filterName,
Class<? extends javax.servlet.Filter> filterClass)
Adds the filter with the given name and class type to this servlet
context.
|
FilterRegistration |
addFilter(String filterName,
javax.servlet.Filter filter)
Registers the given filter instance with this WebappContext
under the given filterName.
|
FilterRegistration |
addFilter(String filterName,
String className)
Adds the filter with the given name and class name to this servlet
context.
|
protected void |
addFilterMap(FilterMap filterMap,
boolean isMatchAfter)
Add a filter mapping to this Context.
|
void |
addListener(Class<? extends EventListener> listenerClass)
Adds the given listener class to this WebappContext.
|
void |
addListener(String className)
Adds the listener with the given class name to this WebappContext.
|
<T extends EventListener> |
addListener(T eventListener) |
ServletRegistration |
addServlet(String servletName,
Class<? extends javax.servlet.Servlet> servletClass)
Adds the servlet with the given name and class type to this servlet
context.
|
ServletRegistration |
addServlet(String servletName,
javax.servlet.Servlet servlet)
Registers the given servlet instance with this WebappContext
under the given servletName.
|
ServletRegistration |
addServlet(String servletName,
String className)
Adds the servlet with the given name and class name to this servlet
context.
|
void |
clearContextInitParameters() |
protected EventListener |
createEventListenerInstance(Class<? extends EventListener> eventListenerClass)
Instantiates the given EventListener class.
|
protected EventListener |
createEventListenerInstance(String eventListenerClassname)
Instantiates the given EventListener class.
|
<T extends javax.servlet.Filter> |
createFilter(Class<T> clazz) |
protected javax.servlet.Filter |
createFilterInstance(Class<? extends javax.servlet.Filter> filterClass)
Instantiates the given Filter class.
|
protected javax.servlet.Filter |
createFilterInstance(FilterRegistration registration)
Instantiates the given Filter class.
|
<T extends javax.servlet.http.HttpUpgradeHandler> |
createHttpUpgradeHandlerInstance(Class<T> clazz)
Instantiates the given HttpUpgradeHandler class.
|
<T extends EventListener> |
createListener(Class<T> clazz) |
<T extends javax.servlet.Servlet> |
createServlet(Class<T> clazz) |
protected javax.servlet.Servlet |
createServletInstance(Class<? extends javax.servlet.Servlet> servletClass)
Instantiates the given Servlet class.
|
protected javax.servlet.Servlet |
createServletInstance(ServletRegistration registration)
Instantiates the given Servlet class.
|
void |
declareRoles(String... roleNames) |
void |
deploy(HttpServer targetServer) |
protected void |
destroyFilters() |
Object |
getAttribute(String name) |
Enumeration<String> |
getAttributeNames() |
protected String |
getBasePath() |
ClassLoader |
getClassLoader() |
javax.servlet.ServletContext |
getContext(String uri) |
String |
getContextPath() |
Set<javax.servlet.SessionTrackingMode> |
getDefaultSessionTrackingModes() |
int |
getEffectiveMajorVersion() |
int |
getEffectiveMinorVersion() |
Set<javax.servlet.SessionTrackingMode> |
getEffectiveSessionTrackingModes() |
protected EventListener[] |
getEventListeners() |
protected FilterChainFactory |
getFilterChainFactory() |
protected List<FilterMap> |
getFilterMaps() |
FilterRegistration |
getFilterRegistration(String name)
Gets the FilterRegistration corresponding to the filter with the
given filterName.
|
Map<String,? extends FilterRegistration> |
getFilterRegistrations()
Gets a (possibly empty) Map of the FilterRegistration
objects (keyed by filter name) corresponding to all filters
registered with this WebappContext.
|
String |
getInitParameter(String name) |
Enumeration<String> |
getInitParameterNames() |
javax.servlet.descriptor.JspConfigDescriptor |
getJspConfigDescriptor() |
int |
getMajorVersion() |
String |
getMimeType(String file) |
int |
getMinorVersion() |
javax.servlet.RequestDispatcher |
getNamedDispatcher(String name) |
String |
getRealPath(String path) |
javax.servlet.RequestDispatcher |
getRequestDispatcher(String path) |
URL |
getResource(String path) |
InputStream |
getResourceAsStream(String path) |
Set<String> |
getResourcePaths(String path) |
String |
getServerInfo() |
javax.servlet.Servlet |
getServlet(String name)
Deprecated.
|
String |
getServletContextName() |
protected Collection<String> |
getServletNameFilterMappings(String filterName)
Gets the current servlet name mappings of the Filter with
the given name.
|
Enumeration<String> |
getServletNames()
Deprecated.
|
ServletRegistration |
getServletRegistration(String name)
Gets the ServletRegistration corresponding to the servlet with the
given servletName.
|
Map<String,? extends ServletRegistration> |
getServletRegistrations()
Gets a (possibly empty) Map of the ServletRegistration
objects (keyed by servlet name) corresponding to all servlets
registered with this WebappContext.
|
Enumeration<javax.servlet.Servlet> |
getServlets()
Deprecated.
|
javax.servlet.SessionCookieConfig |
getSessionCookieConfig() |
protected Collection<String> |
getUrlPatternFilterMappings(String filterName)
Gets the current URL pattern mappings of the Filter with the given
name.
|
String |
getVirtualServerName() |
void |
log(Exception e,
String message)
Deprecated.
|
void |
log(String message) |
void |
log(String message,
Throwable throwable) |
protected String |
normalize(String path)
Return a context-relative path, beginning with a "/", that represents
the canonical version of the specified path after ".." and "." elements
are resolved out.
|
void |
removeAttribute(String name) |
void |
removeContextInitParameter(String name) |
protected void |
removeFilterMaps()
Removes any filter mappings from this Context.
|
void |
setAttribute(String name,
Object value) |
protected void |
setDispatcherHelper(DispatcherHelper dispatcherHelper) |
boolean |
setInitParameter(String name,
String value) |
void |
setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes) |
void |
undeploy() |
protected void |
unregisterAllFilters() |
protected void |
unregisterFilter(javax.servlet.Filter f) |
protected boolean |
validateURLPattern(String urlPattern)
Validate the syntax of a proposed
<url-pattern>
for conformance with specification requirements. |
protected final Map<String,ServletRegistration> servletRegistrations
protected final Map<String,FilterRegistration> filterRegistrations
protected final Map<String,FilterRegistration> unmodifiableFilterRegistrations
protected boolean deployed
protected WebappContext()
public WebappContext(String displayName)
Creates a simple WebappContext
with the root being "/".
displayName
- public void deploy(HttpServer targetServer)
targetServer
- public void undeploy()
public void addContextInitParameter(String name, String value)
name
- value
- public void removeContextInitParameter(String name)
name
- public void clearContextInitParameters()
public FilterRegistration addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
The registered filter may be further configured via the returned
FilterRegistration
object.
If this WebappContext already contains a preliminary FilterRegistration for a filter with the given filterName, it will be completed (by assigning the name of the given filterClass to it) and returned.
addFilter
in interface javax.servlet.ServletContext
filterName
- the name of the filterfilterClass
- the class object from which the filter will be
instantiatedIllegalStateException
- if this WebappContext has already
been initializedpublic FilterRegistration addFilter(String filterName, javax.servlet.Filter filter)
The registered filter may be further configured via the returned
FilterRegistration
object.
If this WebappContext already contains a preliminary FilterRegistration for a filter with the given filterName, it will be completed (by assigning the class name of the given filter instance to it) and returned.
addFilter
in interface javax.servlet.ServletContext
filterName
- the name of the filterfilter
- the filter instance to registerIllegalStateException
- if this WebappContext has already
been initializedpublic FilterRegistration addFilter(String filterName, String className)
The registered filter may be further configured via the returned
FilterRegistration
object.
The specified className will be loaded using the classloader associated with the application represented by this WebappContext.
If this WebappContext already contains a preliminary FilterRegistration for a filter with the given filterName, it will be completed (by assigning the given className to it) and returned.
addFilter
in interface javax.servlet.ServletContext
filterName
- the name of the filterclassName
- the fully qualified class name of the filterIllegalStateException
- if this WebappContext has already
been initializedpublic ServletRegistration addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
The registered servlet may be further configured via the returned
ServletRegistration
object.
If this WebappContext already contains a preliminary ServletRegistration for a servlet with the given servletName, it will be completed (by assigning the name of the given servletClass to it) and returned.
addServlet
in interface javax.servlet.ServletContext
servletName
- the name of the servletservletClass
- the class object from which the servlet will be
instantiatedIllegalStateException
- if this WebappContext has already
been initializedpublic ServletRegistration addServlet(String servletName, javax.servlet.Servlet servlet)
The registered servlet may be further configured via the returned
ServletRegistration
object.
If this WebappContext already contains a preliminary ServletRegistration for a servlet with the given servletName, it will be completed (by assigning the class name of the given servlet instance to it) and returned.
addServlet
in interface javax.servlet.ServletContext
servletName
- the name of the servletservlet
- the servlet instance to registerIllegalStateException
- if this WebappContext has already
been initializedIllegalArgumentException
- if the given servlet instance
implements SingleThreadModel
public ServletRegistration addServlet(String servletName, String className)
The registered servlet may be further configured via the returned
ServletRegistration
object.
The specified className will be loaded using the classloader associated with the application represented by this WebappContext.
If this WebappContext already contains a preliminary ServletRegistration for a servlet with the given servletName, it will be completed (by assigning the given className to it) and returned.
addServlet
in interface javax.servlet.ServletContext
servletName
- the name of the servletclassName
- the fully qualified class name of the servletIllegalStateException
- if this WebappContext has already
been initializedpublic FilterRegistration getFilterRegistration(String name)
getFilterRegistration
in interface javax.servlet.ServletContext
public Map<String,? extends FilterRegistration> getFilterRegistrations()
The returned Map includes the FilterRegistration objects corresponding to all declared and annotated filters, as well as the FilterRegistration objects corresponding to all filters that have been added via one of the addFilter methods.
Any changes to the returned Map must not affect this WebappContext.
getFilterRegistrations
in interface javax.servlet.ServletContext
public ServletRegistration getServletRegistration(String name)
getServletRegistration
in interface javax.servlet.ServletContext
public Map<String,? extends ServletRegistration> getServletRegistrations()
The returned Map includes the ServletRegistration objects corresponding to all declared and annotated servlets, as well as the ServletRegistration objects corresponding to all servlets that have been added via one of the addServlet methods.
If permitted, any changes to the returned Map must not affect this WebappContext.
getServletRegistrations
in interface javax.servlet.ServletContext
public void addListener(Class<? extends EventListener> listenerClass)
The given listener must be an instance of one or more of the following interfaces:
ServletContextAttributeListener
ServletRequestListener
ServletRequestAttributeListener
HttpSessionListener
HttpSessionAttributeListener
If the given listener is an instance of a listener interface whose
invocation order corresponds to the declaration order (i.e., if it
is an instance of ServletRequestListener
,
ServletContextListener
, or
HttpSessionListener
),
then the listener will be added to the end of the ordered list of
listeners of that interface.
addListener
in interface javax.servlet.ServletContext
IllegalArgumentException
- if the given listener is not
an instance of any of the above interfacesIllegalStateException
- if this WebappContext has already
been initializedpublic void addListener(String className)
The class with the given name will be loaded using the classloader associated with the application represented by this WebappContext, and must implement one or more of the following interfaces:
ServletContextAttributeListener
ServletRequestListener
ServletRequestAttributeListener
HttpSessionListener
HttpSessionAttributeListener
As part of this method call, the container must load the class with the specified class name to ensure that it implements one of the required interfaces.
If the class with the given name implements a listener interface
whose invocation order corresponds to the declaration order (i.e.,
if it implements ServletRequestListener
,
ServletContextListener
, or
HttpSessionListener
),
then the new listener will be added to the end of the ordered list of
listeners of that interface.
addListener
in interface javax.servlet.ServletContext
className
- the fully qualified class name of the listenerIllegalArgumentException
- if the class with the given name
does not implement any of the above interfacesIllegalStateException
- if this WebappContext has already
been initializedpublic <T extends EventListener> void addListener(T eventListener)
addListener
in interface javax.servlet.ServletContext
public <T extends javax.servlet.Servlet> T createServlet(Class<T> clazz) throws javax.servlet.ServletException
createServlet
in interface javax.servlet.ServletContext
javax.servlet.ServletException
public <T extends javax.servlet.Filter> T createFilter(Class<T> clazz) throws javax.servlet.ServletException
createFilter
in interface javax.servlet.ServletContext
javax.servlet.ServletException
public <T extends EventListener> T createListener(Class<T> clazz) throws javax.servlet.ServletException
createListener
in interface javax.servlet.ServletContext
javax.servlet.ServletException
public void declareRoles(String... roleNames)
declareRoles
in interface javax.servlet.ServletContext
public String getContextPath()
getContextPath
in interface javax.servlet.ServletContext
public javax.servlet.ServletContext getContext(String uri)
getContext
in interface javax.servlet.ServletContext
public int getMajorVersion()
getMajorVersion
in interface javax.servlet.ServletContext
public int getMinorVersion()
getMinorVersion
in interface javax.servlet.ServletContext
public int getEffectiveMajorVersion()
getEffectiveMajorVersion
in interface javax.servlet.ServletContext
public int getEffectiveMinorVersion()
getEffectiveMinorVersion
in interface javax.servlet.ServletContext
public String getMimeType(String file)
getMimeType
in interface javax.servlet.ServletContext
public Set<String> getResourcePaths(String path)
getResourcePaths
in interface javax.servlet.ServletContext
public URL getResource(String path) throws MalformedURLException
getResource
in interface javax.servlet.ServletContext
MalformedURLException
public InputStream getResourceAsStream(String path)
getResourceAsStream
in interface javax.servlet.ServletContext
public javax.servlet.RequestDispatcher getRequestDispatcher(String path)
getRequestDispatcher
in interface javax.servlet.ServletContext
public javax.servlet.RequestDispatcher getNamedDispatcher(String name)
getNamedDispatcher
in interface javax.servlet.ServletContext
@Deprecated public javax.servlet.Servlet getServlet(String name) throws javax.servlet.ServletException
getServlet
in interface javax.servlet.ServletContext
javax.servlet.ServletException
@Deprecated public Enumeration<javax.servlet.Servlet> getServlets()
getServlets
in interface javax.servlet.ServletContext
@Deprecated public Enumeration<String> getServletNames()
getServletNames
in interface javax.servlet.ServletContext
public void log(String message)
log
in interface javax.servlet.ServletContext
@Deprecated public void log(Exception e, String message)
log
in interface javax.servlet.ServletContext
public void log(String message, Throwable throwable)
log
in interface javax.servlet.ServletContext
public String getRealPath(String path)
getRealPath
in interface javax.servlet.ServletContext
public String getVirtualServerName()
getVirtualServerName
in interface javax.servlet.ServletContext
public String getServerInfo()
getServerInfo
in interface javax.servlet.ServletContext
public String getInitParameter(String name)
getInitParameter
in interface javax.servlet.ServletContext
public Enumeration<String> getInitParameterNames()
getInitParameterNames
in interface javax.servlet.ServletContext
public boolean setInitParameter(String name, String value)
setInitParameter
in interface javax.servlet.ServletContext
public Object getAttribute(String name)
getAttribute
in interface javax.servlet.ServletContext
public Enumeration<String> getAttributeNames()
getAttributeNames
in interface javax.servlet.ServletContext
public void setAttribute(String name, Object value)
setAttribute
in interface javax.servlet.ServletContext
public void removeAttribute(String name)
removeAttribute
in interface javax.servlet.ServletContext
public String getServletContextName()
getServletContextName
in interface javax.servlet.ServletContext
public javax.servlet.SessionCookieConfig getSessionCookieConfig()
getSessionCookieConfig
in interface javax.servlet.ServletContext
public void setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
setSessionTrackingModes
in interface javax.servlet.ServletContext
public Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
getDefaultSessionTrackingModes
in interface javax.servlet.ServletContext
public Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
getEffectiveSessionTrackingModes
in interface javax.servlet.ServletContext
public javax.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
getJspConfigDescriptor
in interface javax.servlet.ServletContext
public ClassLoader getClassLoader()
getClassLoader
in interface javax.servlet.ServletContext
protected String normalize(String path)
null
instead.path
- Path to be normalizedprotected String getBasePath()
protected void setDispatcherHelper(DispatcherHelper dispatcherHelper)
dispatcherHelper
- protected EventListener[] getEventListeners()
protected void addFilterMap(FilterMap filterMap, boolean isMatchAfter)
filterMap
- The filter mapping to be addedisMatchAfter
- true if the given filter mapping should be matched
against requests after any declared filter mappings of this servlet
context, and false if it is supposed to be matched before any declared
filter mappings of this servlet contextIllegalArgumentException
- if the specified filter name
does not match an existing filter definition, or the filter mapping
is malformedprotected void removeFilterMaps()
protected Collection<String> getServletNameFilterMappings(String filterName)
protected Collection<String> getUrlPatternFilterMappings(String filterName)
protected FilterChainFactory getFilterChainFactory()
protected void unregisterFilter(javax.servlet.Filter f)
protected void unregisterAllFilters()
protected void destroyFilters()
protected javax.servlet.Servlet createServletInstance(ServletRegistration registration) throws Exception
Exception
protected javax.servlet.Servlet createServletInstance(Class<? extends javax.servlet.Servlet> servletClass) throws Exception
Exception
protected javax.servlet.Filter createFilterInstance(FilterRegistration registration) throws Exception
Exception
protected javax.servlet.Filter createFilterInstance(Class<? extends javax.servlet.Filter> filterClass) throws Exception
Exception
protected EventListener createEventListenerInstance(Class<? extends EventListener> eventListenerClass) throws Exception
Exception
protected EventListener createEventListenerInstance(String eventListenerClassname) throws Exception
Exception
public <T extends javax.servlet.http.HttpUpgradeHandler> T createHttpUpgradeHandlerInstance(Class<T> clazz) throws Exception
T
- clazz
- Exception
protected boolean validateURLPattern(String urlPattern)
<url-pattern>
for conformance with specification requirements.urlPattern
- URL pattern to be validatedCopyright © 2014 Oracle Corporation. All Rights Reserved.