- All Implemented Interfaces:
jakarta.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.
- Since:
- 2.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected final Map<String,FilterRegistration> protected final Map<String,ServletRegistration> protected final Map<String,FilterRegistration> Fields inherited from interface jakarta.servlet.ServletContext
ORDERED_LIBS, TEMPDIR -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedWebappContext(String displayName) Creates a simpleWebappContextwith the root being "/".WebappContext(String displayName, String contextPath) WebappContext(String displayName, String contextPath, String basePath) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddContextInitParameter(String name, String value) Registers the given filter instance with this WebappContext under the given filterName.Adds the filter with the given name and class type to this servlet context.Adds the filter with the given name and class name to this servlet context.protected voidaddFilterMap(FilterMap filterMap, boolean isMatchAfter) Add a filter mapping to this Context.jakarta.servlet.ServletRegistration.DynamicaddJspFile(String servletName, String jspFile) voidaddListener(Class<? extends EventListener> listenerClass) Adds the given listener class to this WebappContext.voidaddListener(String className) Adds the listener with the given class name to this WebappContext.<T extends EventListener>
voidaddListener(T eventListener) addServlet(String servletName, jakarta.servlet.Servlet servlet) Registers the given servlet instance with this WebappContext under the given servletName.addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass) Adds the servlet with the given name and class type to this servlet context.addServlet(String servletName, String className) Adds the servlet with the given name and class name to this servlet context.voidprotected EventListenercreateEventListenerInstance(Class<? extends EventListener> eventListenerClass) Instantiates the given EventListener class.protected EventListenercreateEventListenerInstance(String eventListenerClassname) Instantiates the given EventListener class.<T extends jakarta.servlet.Filter>
TcreateFilter(Class<T> clazz) protected jakarta.servlet.FiltercreateFilterInstance(Class<? extends jakarta.servlet.Filter> filterClass) Instantiates the given Filter class.protected jakarta.servlet.FiltercreateFilterInstance(FilterRegistration registration) Instantiates the given Filter class.<T extends jakarta.servlet.http.HttpUpgradeHandler>
TcreateHttpUpgradeHandlerInstance(Class<T> clazz) Instantiates the given HttpUpgradeHandler class.<T extends EventListener>
TcreateListener(Class<T> clazz) <T extends jakarta.servlet.Servlet>
TcreateServlet(Class<T> clazz) protected jakarta.servlet.ServletcreateServletInstance(Class<? extends jakarta.servlet.Servlet> servletClass) Instantiates the given Servlet class.protected jakarta.servlet.ServletcreateServletInstance(ServletRegistration registration) Instantiates the given Servlet class.voiddeclareRoles(String... roleNames) voiddeploy(org.glassfish.grizzly.http.server.HttpServer targetServer) protected voidgetAttribute(String name) protected Stringjakarta.servlet.ServletContextgetContext(String uri) Set<jakarta.servlet.SessionTrackingMode>intintSet<jakarta.servlet.SessionTrackingMode>protected EventListener[]protected FilterChainFactorygetFilterRegistration(String name) Gets the FilterRegistration corresponding to the filter with the given filterName.Map<String,? extends FilterRegistration> Gets a (possibly empty) Map of the FilterRegistration objects (keyed by filter name) corresponding to all filters registered with this WebappContext.getInitParameter(String name) jakarta.servlet.descriptor.JspConfigDescriptorintgetMimeType(String file) intjakarta.servlet.RequestDispatchergetNamedDispatcher(String name) getRealPath(String path) jakarta.servlet.RequestDispatchergetRequestDispatcher(String path) getResource(String path) getResourceAsStream(String path) getResourcePaths(String path) protected Collection<String>getServletNameFilterMappings(String filterName) Gets the current servlet name mappings of the Filter with the given name.getServletRegistration(String name) Gets the ServletRegistration corresponding to the servlet with the given servletName.Map<String,? extends ServletRegistration> Gets a (possibly empty) Map of the ServletRegistration objects (keyed by servlet name) corresponding to all servlets registered with this WebappContext.jakarta.servlet.SessionCookieConfigintprotected Collection<String>getUrlPatternFilterMappings(String filterName) Gets the current URL pattern mappings of the Filter with the given name.voidvoidprotected StringReturn a context-relative path, beginning with a "/", that represents the canonical version of the specified path after ".." and "." elements are resolved out.voidremoveAttribute(String name) voidprotected voidRemoves any filter mappings from this Context.voidsetAttribute(String name, Object value) protected voidsetDispatcherHelper(org.glassfish.grizzly.http.server.util.DispatcherHelper dispatcherHelper) booleansetInitParameter(String name, String value) voidsetRequestCharacterEncoding(String requestEncoding) voidsetResponseCharacterEncoding(String responseEncoding) voidsetServerInfo(String serverInfo) Set the value of theServerheader to be sent in the response.voidsetSessionManager(org.glassfish.grizzly.http.server.SessionManager sessionManager) Sets theSessionManagerthat should be used by thisWebappContext.voidsetSessionTimeout(int sessionTimeout) voidsetSessionTrackingModes(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes) voidundeploy()protected voidprotected voidunregisterFilter(jakarta.servlet.Filter f) protected booleanvalidateURLPattern(String urlPattern) Validate the syntax of a proposed<url-pattern>for conformance with specification requirements.
-
Field Details
-
servletRegistrations
-
filterRegistrations
-
unmodifiableFilterRegistrations
-
deployed
protected boolean deployed
-
-
Constructor Details
-
WebappContext
protected WebappContext() -
WebappContext
Creates a simple
WebappContextwith the root being "/".- Parameters:
displayName-
-
WebappContext
-
WebappContext
-
-
Method Details
-
setServerInfo
Set the value of theServerheader to be sent in the response. If the value is a zero-length String or null, noServerheader will be sent.- Parameters:
serverInfo- the string to be sent with the response.
-
deploy
public void deploy(org.glassfish.grizzly.http.server.HttpServer targetServer) - Parameters:
targetServer-
-
undeploy
public void undeploy() -
addContextInitParameter
- Parameters:
name-value-
-
removeContextInitParameter
- Parameters:
name-
-
clearContextInitParameters
public void clearContextInitParameters() -
addFilter
public FilterRegistration addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass) Adds the filter with the given name and class type to this servlet context.The registered filter may be further configured via the returned
FilterRegistrationobject.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.
- Specified by:
addFilterin interfacejakarta.servlet.ServletContext- Parameters:
filterName- the name of the filterfilterClass- the class object from which the filter will be instantiated- Returns:
- a FilterRegistration object that may be used to further configure the registered filter, or null if this WebappContext already contains a complete FilterRegistration for a filter with the given filterName
- Throws:
IllegalStateException- if this WebappContext has already been initialized
-
addFilter
Registers the given filter instance with this WebappContext under the given filterName.The registered filter may be further configured via the returned
FilterRegistrationobject.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.
- Specified by:
addFilterin interfacejakarta.servlet.ServletContext- Parameters:
filterName- the name of the filterfilter- the filter instance to register- Returns:
- a FilterRegistration object that may be used to further configure the given filter, or null if this WebappContext 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 WebappContext in the same container
- Throws:
IllegalStateException- if this WebappContext has already been initialized- Since:
- Servlet 3.0
-
addFilter
Adds the filter with the given name and class name to this servlet context.The registered filter may be further configured via the returned
FilterRegistrationobject.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.
- Specified by:
addFilterin interfacejakarta.servlet.ServletContext- Parameters:
filterName- the name of the filterclassName- the fully qualified class name of the filter- Returns:
- a FilterRegistration object that may be used to further configure the registered filter, or null if this WebappContext already contains a complete FilterRegistration for a filter with the given filterName
- Throws:
IllegalStateException- if this WebappContext has already been initialized
-
addServlet
public ServletRegistration addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass) Adds the servlet with the given name and class type to this servlet context.The registered servlet may be further configured via the returned
ServletRegistrationobject.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.
- Specified by:
addServletin interfacejakarta.servlet.ServletContext- Parameters:
servletName- the name of the servletservletClass- the class object from which the servlet will be instantiated- Returns:
- a ServletRegistration object that may be used to further configure the registered servlet, or null if this WebappContext already contains a complete ServletRegistration for the given servletName
- Throws:
IllegalStateException- if this WebappContext has already been initialized
-
addServlet
Registers the given servlet instance with this WebappContext under the given servletName.The registered servlet may be further configured via the returned
ServletRegistrationobject.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.
- Specified by:
addServletin interfacejakarta.servlet.ServletContext- Parameters:
servletName- the name of the servletservlet- the servlet instance to register- Returns:
- a ServletRegistration object that may be used to further configure the given servlet, or null if this WebappContext 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 WebappContext in the same container
- Throws:
IllegalStateException- if this WebappContext has already been initializedIllegalArgumentException- if the given servlet instance implementsjakarta.servlet.SingleThreadModel
-
addServlet
Adds the servlet with the given name and class name to this servlet context.The registered servlet may be further configured via the returned
ServletRegistrationobject.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.
- Specified by:
addServletin interfacejakarta.servlet.ServletContext- Parameters:
servletName- the name of the servletclassName- the fully qualified class name of the servlet- Returns:
- a ServletRegistration object that may be used to further configure the registered servlet, or null if this WebappContext already contains a complete ServletRegistration for a servlet with the given servletName
- Throws:
IllegalStateException- if this WebappContext has already been initialized
-
getFilterRegistration
Gets the FilterRegistration corresponding to the filter with the given filterName.- Specified by:
getFilterRegistrationin interfacejakarta.servlet.ServletContext- Returns:
- the (complete or preliminary) FilterRegistration for the filter with the given filterName, or null if no FilterRegistration exists under that name
-
getFilterRegistrations
Gets a (possibly empty) Map of the FilterRegistration objects (keyed by filter name) corresponding to all filters registered with this WebappContext.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.
- Specified by:
getFilterRegistrationsin interfacejakarta.servlet.ServletContext- Returns:
- Map of the (complete and preliminary) FilterRegistration objects corresponding to all filters currently registered with this WebappContext
-
getServletRegistration
Gets the ServletRegistration corresponding to the servlet with the given servletName.- Specified by:
getServletRegistrationin interfacejakarta.servlet.ServletContext- Returns:
- the (complete or preliminary) ServletRegistration for the servlet with the given servletName, or null if no ServletRegistration exists under that name
-
getServletRegistrations
Gets a (possibly empty) Map of the ServletRegistration objects (keyed by servlet name) corresponding to all servlets registered with this WebappContext.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.
- Specified by:
getServletRegistrationsin interfacejakarta.servlet.ServletContext- Returns:
- Map of the (complete and preliminary) ServletRegistration objects corresponding to all servlets currently registered with this WebappContext
- Since:
- Servlet 3.0
-
addListener
Adds the given listener class to this WebappContext.The given listener must be an instance of one or more of the following interfaces:
ServletContextAttributeListenerServletRequestListenerServletRequestAttributeListenerHttpSessionListenerHttpSessionAttributeListener
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, orHttpSessionListener), then the listener will be added to the end of the ordered list of listeners of that interface.- Specified by:
addListenerin interfacejakarta.servlet.ServletContext- Throws:
IllegalArgumentException- if the given listener is not an instance of any of the above interfacesIllegalStateException- if this WebappContext has already been initialized
-
addListener
Adds the listener with the given class name to this WebappContext.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:
ServletContextAttributeListenerServletRequestListenerServletRequestAttributeListenerHttpSessionListenerHttpSessionAttributeListener
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, orHttpSessionListener), then the new listener will be added to the end of the ordered list of listeners of that interface.- Specified by:
addListenerin interfacejakarta.servlet.ServletContext- Parameters:
className- the fully qualified class name of the listener- Throws:
IllegalArgumentException- if the class with the given name does not implement any of the above interfacesIllegalStateException- if this WebappContext has already been initialized
-
addListener
- Specified by:
addListenerin interfacejakarta.servlet.ServletContext
-
createServlet
public <T extends jakarta.servlet.Servlet> T createServlet(Class<T> clazz) throws jakarta.servlet.ServletException - Specified by:
createServletin interfacejakarta.servlet.ServletContext- Throws:
jakarta.servlet.ServletException
-
createFilter
public <T extends jakarta.servlet.Filter> T createFilter(Class<T> clazz) throws jakarta.servlet.ServletException - Specified by:
createFilterin interfacejakarta.servlet.ServletContext- Throws:
jakarta.servlet.ServletException
-
createListener
public <T extends EventListener> T createListener(Class<T> clazz) throws jakarta.servlet.ServletException - Specified by:
createListenerin interfacejakarta.servlet.ServletContext- Throws:
jakarta.servlet.ServletException
-
declareRoles
- Specified by:
declareRolesin interfacejakarta.servlet.ServletContext
-
getContextPath
- Specified by:
getContextPathin interfacejakarta.servlet.ServletContext
-
getContext
- Specified by:
getContextin interfacejakarta.servlet.ServletContext
-
getMajorVersion
public int getMajorVersion()- Specified by:
getMajorVersionin interfacejakarta.servlet.ServletContext
-
getMinorVersion
public int getMinorVersion()- Specified by:
getMinorVersionin interfacejakarta.servlet.ServletContext
-
getEffectiveMajorVersion
public int getEffectiveMajorVersion()- Specified by:
getEffectiveMajorVersionin interfacejakarta.servlet.ServletContext
-
getEffectiveMinorVersion
public int getEffectiveMinorVersion()- Specified by:
getEffectiveMinorVersionin interfacejakarta.servlet.ServletContext
-
getMimeType
- Specified by:
getMimeTypein interfacejakarta.servlet.ServletContext
-
getResourcePaths
- Specified by:
getResourcePathsin interfacejakarta.servlet.ServletContext
-
getResource
- Specified by:
getResourcein interfacejakarta.servlet.ServletContext- Throws:
MalformedURLException
-
getResourceAsStream
- Specified by:
getResourceAsStreamin interfacejakarta.servlet.ServletContext
-
getRequestDispatcher
- Specified by:
getRequestDispatcherin interfacejakarta.servlet.ServletContext
-
getNamedDispatcher
- Specified by:
getNamedDispatcherin interfacejakarta.servlet.ServletContext
-
addJspFile
- Specified by:
addJspFilein interfacejakarta.servlet.ServletContext
-
getSessionTimeout
public int getSessionTimeout()- Specified by:
getSessionTimeoutin interfacejakarta.servlet.ServletContext
-
setSessionTimeout
public void setSessionTimeout(int sessionTimeout) - Specified by:
setSessionTimeoutin interfacejakarta.servlet.ServletContext
-
getRequestCharacterEncoding
- Specified by:
getRequestCharacterEncodingin interfacejakarta.servlet.ServletContext
-
setRequestCharacterEncoding
- Specified by:
setRequestCharacterEncodingin interfacejakarta.servlet.ServletContext
-
getResponseCharacterEncoding
- Specified by:
getResponseCharacterEncodingin interfacejakarta.servlet.ServletContext
-
setResponseCharacterEncoding
- Specified by:
setResponseCharacterEncodingin interfacejakarta.servlet.ServletContext
-
log
- Specified by:
login interfacejakarta.servlet.ServletContext
-
log
- Specified by:
login interfacejakarta.servlet.ServletContext
-
getRealPath
- Specified by:
getRealPathin interfacejakarta.servlet.ServletContext
-
getVirtualServerName
- Specified by:
getVirtualServerNamein interfacejakarta.servlet.ServletContext
-
getServerInfo
- Specified by:
getServerInfoin interfacejakarta.servlet.ServletContext
-
getInitParameter
- Specified by:
getInitParameterin interfacejakarta.servlet.ServletContext
-
getInitParameterNames
- Specified by:
getInitParameterNamesin interfacejakarta.servlet.ServletContext
-
setInitParameter
- Specified by:
setInitParameterin interfacejakarta.servlet.ServletContext
-
getAttribute
- Specified by:
getAttributein interfacejakarta.servlet.ServletContext
-
getAttributeNames
- Specified by:
getAttributeNamesin interfacejakarta.servlet.ServletContext
-
setAttribute
- Specified by:
setAttributein interfacejakarta.servlet.ServletContext
-
removeAttribute
- Specified by:
removeAttributein interfacejakarta.servlet.ServletContext
-
getServletContextName
- Specified by:
getServletContextNamein interfacejakarta.servlet.ServletContext
-
getSessionCookieConfig
public jakarta.servlet.SessionCookieConfig getSessionCookieConfig()- Specified by:
getSessionCookieConfigin interfacejakarta.servlet.ServletContext
-
setSessionTrackingModes
- Specified by:
setSessionTrackingModesin interfacejakarta.servlet.ServletContext
-
getDefaultSessionTrackingModes
- Specified by:
getDefaultSessionTrackingModesin interfacejakarta.servlet.ServletContext
-
getEffectiveSessionTrackingModes
- Specified by:
getEffectiveSessionTrackingModesin interfacejakarta.servlet.ServletContext
-
getJspConfigDescriptor
public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()- Specified by:
getJspConfigDescriptorin interfacejakarta.servlet.ServletContext
-
getClassLoader
- Specified by:
getClassLoaderin interfacejakarta.servlet.ServletContext
-
normalize
Return a context-relative path, beginning with a "/", that represents the canonical version of the specified path after ".." and "." elements are resolved out. If the specified path attempts to go outside the boundaries of the current context (i.e. too many ".." path elements are present), returnnullinstead.- Parameters:
path- Path to be normalized
-
getBasePath
- Returns:
-
setDispatcherHelper
protected void setDispatcherHelper(org.glassfish.grizzly.http.server.util.DispatcherHelper dispatcherHelper) - Parameters:
dispatcherHelper-
-
setSessionManager
public void setSessionManager(org.glassfish.grizzly.http.server.SessionManager sessionManager) Sets theSessionManagerthat should be used by thisWebappContext. The default is an instance ofServletSessionManager- Parameters:
sessionManager- an implementation of SessionManager
-
getEventListeners
- Returns:
-
addFilterMap
Add a filter mapping to this Context.- Parameters:
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 context- Throws:
IllegalArgumentException- if the specified filter name does not match an existing filter definition, or the filter mapping is malformed
-
getFilterMaps
-
removeFilterMaps
protected void removeFilterMaps()Removes any filter mappings from this Context. -
getServletNameFilterMappings
Gets the current servlet name mappings of the Filter with the given name. -
getUrlPatternFilterMappings
Gets the current URL pattern mappings of the Filter with the given name. -
getFilterChainFactory
-
unregisterFilter
protected void unregisterFilter(jakarta.servlet.Filter f) -
unregisterAllFilters
protected void unregisterAllFilters() -
destroyFilters
protected void destroyFilters() -
createServletInstance
protected jakarta.servlet.Servlet createServletInstance(ServletRegistration registration) throws Exception Instantiates the given Servlet class.- Returns:
- the new Servlet instance
- Throws:
Exception
-
createServletInstance
protected jakarta.servlet.Servlet createServletInstance(Class<? extends jakarta.servlet.Servlet> servletClass) throws Exception Instantiates the given Servlet class.- Returns:
- the new Servlet instance
- Throws:
Exception
-
createFilterInstance
protected jakarta.servlet.Filter createFilterInstance(FilterRegistration registration) throws Exception Instantiates the given Filter class.- Returns:
- the new Filter instance
- Throws:
Exception
-
createFilterInstance
protected jakarta.servlet.Filter createFilterInstance(Class<? extends jakarta.servlet.Filter> filterClass) throws Exception Instantiates the given Filter class.- Returns:
- the new Filter instance
- Throws:
Exception
-
createEventListenerInstance
protected EventListener createEventListenerInstance(Class<? extends EventListener> eventListenerClass) throws Exception Instantiates the given EventListener class.- Returns:
- the new EventListener instance
- Throws:
Exception
-
createEventListenerInstance
Instantiates the given EventListener class.- Returns:
- the new EventListener instance
- Throws:
Exception
-
createHttpUpgradeHandlerInstance
public <T extends jakarta.servlet.http.HttpUpgradeHandler> T createHttpUpgradeHandlerInstance(Class<T> clazz) throws Exception Instantiates the given HttpUpgradeHandler class.- Type Parameters:
T-- Parameters:
clazz-- Returns:
- a new T instance
- Throws:
Exception
-
validateURLPattern
Validate the syntax of a proposed<url-pattern>for conformance with specification requirements.- Parameters:
urlPattern- URL pattern to be validated
-