Class WebappContext

  • All Implemented Interfaces:
    jakarta.servlet.ServletContext

    public class WebappContext
    extends Object
    implements 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
    • Constructor Detail

      • WebappContext

        protected WebappContext()
      • WebappContext

        public WebappContext​(String displayName)

        Creates a simple WebappContext with the root being "/".

        Parameters:
        displayName -
      • WebappContext

        public WebappContext​(String displayName,
                             String contextPath)
      • WebappContext

        public WebappContext​(String displayName,
                             String contextPath,
                             String basePath)
    • Method Detail

      • setServerInfo

        public void setServerInfo​(String serverInfo)
        Set the value of the Server header to be sent in the response. If the value is a zero-length String or null, no Server header 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

        public void addContextInitParameter​(String name,
                                            String value)
        Parameters:
        name -
        value -
      • removeContextInitParameter

        public void removeContextInitParameter​(String name)
        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 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.

        Specified by:
        addFilter in interface jakarta.servlet.ServletContext
        Parameters:
        filterName - the name of the filter
        filterClass - 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

        public FilterRegistration addFilter​(String filterName,
                                            jakarta.servlet.Filter filter)
        Registers the given filter instance with this WebappContext under the given filterName.

        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.

        Specified by:
        addFilter in interface jakarta.servlet.ServletContext
        Parameters:
        filterName - the name of the filter
        filter - 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

        public FilterRegistration addFilter​(String filterName,
                                            String className)
        Adds the filter with the given name and class name to this servlet context.

        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.

        Specified by:
        addFilter in interface jakarta.servlet.ServletContext
        Parameters:
        filterName - the name of the filter
        className - 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 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.

        Specified by:
        addServlet in interface jakarta.servlet.ServletContext
        Parameters:
        servletName - the name of the servlet
        servletClass - 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

        public ServletRegistration addServlet​(String servletName,
                                              jakarta.servlet.Servlet servlet)
        Registers the given servlet instance with this WebappContext under the given servletName.

        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.

        Specified by:
        addServlet in interface jakarta.servlet.ServletContext
        Parameters:
        servletName - the name of the servlet
        servlet - 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 initialized
        IllegalArgumentException - if the given servlet instance implements jakarta.servlet.SingleThreadModel
      • addServlet

        public ServletRegistration addServlet​(String servletName,
                                              String className)
        Adds the servlet with the given name and class name to this servlet context.

        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.

        Specified by:
        addServlet in interface jakarta.servlet.ServletContext
        Parameters:
        servletName - the name of the servlet
        className - 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

        public FilterRegistration getFilterRegistration​(String name)
        Gets the FilterRegistration corresponding to the filter with the given filterName.
        Specified by:
        getFilterRegistration in interface jakarta.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

        public 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.

        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:
        getFilterRegistrations in interface jakarta.servlet.ServletContext
        Returns:
        Map of the (complete and preliminary) FilterRegistration objects corresponding to all filters currently registered with this WebappContext
      • getServletRegistration

        public ServletRegistration getServletRegistration​(String name)
        Gets the ServletRegistration corresponding to the servlet with the given servletName.
        Specified by:
        getServletRegistration in interface jakarta.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

        public 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.

        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:
        getServletRegistrations in interface jakarta.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

        public void addListener​(Class<? extends EventListener> listenerClass)
        Adds the given listener class to this WebappContext.

        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.

        Specified by:
        addListener in interface jakarta.servlet.ServletContext
        Throws:
        IllegalArgumentException - if the given listener is not an instance of any of the above interfaces
        IllegalStateException - if this WebappContext has already been initialized
      • addListener

        public void addListener​(String className)
        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:

        • 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.

        Specified by:
        addListener in interface jakarta.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 interfaces
        IllegalStateException - if this WebappContext has already been initialized
      • addListener

        public <T extends EventListener> void addListener​(T eventListener)
        Specified by:
        addListener in interface jakarta.servlet.ServletContext
      • createServlet

        public <T extends jakarta.servlet.Servlet> T createServlet​(Class<T> clazz)
                                                            throws jakarta.servlet.ServletException
        Specified by:
        createServlet in interface jakarta.servlet.ServletContext
        Throws:
        jakarta.servlet.ServletException
      • createFilter

        public <T extends jakarta.servlet.Filter> T createFilter​(Class<T> clazz)
                                                          throws jakarta.servlet.ServletException
        Specified by:
        createFilter in interface jakarta.servlet.ServletContext
        Throws:
        jakarta.servlet.ServletException
      • createListener

        public <T extends EventListener> T createListener​(Class<T> clazz)
                                                   throws jakarta.servlet.ServletException
        Specified by:
        createListener in interface jakarta.servlet.ServletContext
        Throws:
        jakarta.servlet.ServletException
      • declareRoles

        public void declareRoles​(String... roleNames)
        Specified by:
        declareRoles in interface jakarta.servlet.ServletContext
      • getContextPath

        public String getContextPath()
        Specified by:
        getContextPath in interface jakarta.servlet.ServletContext
      • getContext

        public jakarta.servlet.ServletContext getContext​(String uri)
        Specified by:
        getContext in interface jakarta.servlet.ServletContext
      • getMajorVersion

        public int getMajorVersion()
        Specified by:
        getMajorVersion in interface jakarta.servlet.ServletContext
      • getMinorVersion

        public int getMinorVersion()
        Specified by:
        getMinorVersion in interface jakarta.servlet.ServletContext
      • getEffectiveMajorVersion

        public int getEffectiveMajorVersion()
        Specified by:
        getEffectiveMajorVersion in interface jakarta.servlet.ServletContext
      • getEffectiveMinorVersion

        public int getEffectiveMinorVersion()
        Specified by:
        getEffectiveMinorVersion in interface jakarta.servlet.ServletContext
      • getMimeType

        public String getMimeType​(String file)
        Specified by:
        getMimeType in interface jakarta.servlet.ServletContext
      • getResourcePaths

        public Set<String> getResourcePaths​(String path)
        Specified by:
        getResourcePaths in interface jakarta.servlet.ServletContext
      • getResourceAsStream

        public InputStream getResourceAsStream​(String path)
        Specified by:
        getResourceAsStream in interface jakarta.servlet.ServletContext
      • getRequestDispatcher

        public jakarta.servlet.RequestDispatcher getRequestDispatcher​(String path)
        Specified by:
        getRequestDispatcher in interface jakarta.servlet.ServletContext
      • getNamedDispatcher

        public jakarta.servlet.RequestDispatcher getNamedDispatcher​(String name)
        Specified by:
        getNamedDispatcher in interface jakarta.servlet.ServletContext
      • addJspFile

        public jakarta.servlet.ServletRegistration.Dynamic addJspFile​(String servletName,
                                                                      String jspFile)
        Specified by:
        addJspFile in interface jakarta.servlet.ServletContext
      • getSessionTimeout

        public int getSessionTimeout()
        Specified by:
        getSessionTimeout in interface jakarta.servlet.ServletContext
      • setSessionTimeout

        public void setSessionTimeout​(int sessionTimeout)
        Specified by:
        setSessionTimeout in interface jakarta.servlet.ServletContext
      • getRequestCharacterEncoding

        public String getRequestCharacterEncoding()
        Specified by:
        getRequestCharacterEncoding in interface jakarta.servlet.ServletContext
      • setRequestCharacterEncoding

        public void setRequestCharacterEncoding​(String requestEncoding)
        Specified by:
        setRequestCharacterEncoding in interface jakarta.servlet.ServletContext
      • getResponseCharacterEncoding

        public String getResponseCharacterEncoding()
        Specified by:
        getResponseCharacterEncoding in interface jakarta.servlet.ServletContext
      • setResponseCharacterEncoding

        public void setResponseCharacterEncoding​(String responseEncoding)
        Specified by:
        setResponseCharacterEncoding in interface jakarta.servlet.ServletContext
      • log

        public void log​(String message)
        Specified by:
        log in interface jakarta.servlet.ServletContext
      • log

        public void log​(String message,
                        Throwable throwable)
        Specified by:
        log in interface jakarta.servlet.ServletContext
      • getRealPath

        public String getRealPath​(String path)
        Specified by:
        getRealPath in interface jakarta.servlet.ServletContext
      • getVirtualServerName

        public String getVirtualServerName()
        Specified by:
        getVirtualServerName in interface jakarta.servlet.ServletContext
      • getServerInfo

        public String getServerInfo()
        Specified by:
        getServerInfo in interface jakarta.servlet.ServletContext
      • getInitParameter

        public String getInitParameter​(String name)
        Specified by:
        getInitParameter in interface jakarta.servlet.ServletContext
      • getInitParameterNames

        public Enumeration<String> getInitParameterNames()
        Specified by:
        getInitParameterNames in interface jakarta.servlet.ServletContext
      • setInitParameter

        public boolean setInitParameter​(String name,
                                        String value)
        Specified by:
        setInitParameter in interface jakarta.servlet.ServletContext
      • getAttribute

        public Object getAttribute​(String name)
        Specified by:
        getAttribute in interface jakarta.servlet.ServletContext
      • getAttributeNames

        public Enumeration<String> getAttributeNames()
        Specified by:
        getAttributeNames in interface jakarta.servlet.ServletContext
      • setAttribute

        public void setAttribute​(String name,
                                 Object value)
        Specified by:
        setAttribute in interface jakarta.servlet.ServletContext
      • removeAttribute

        public void removeAttribute​(String name)
        Specified by:
        removeAttribute in interface jakarta.servlet.ServletContext
      • getServletContextName

        public String getServletContextName()
        Specified by:
        getServletContextName in interface jakarta.servlet.ServletContext
      • getSessionCookieConfig

        public jakarta.servlet.SessionCookieConfig getSessionCookieConfig()
        Specified by:
        getSessionCookieConfig in interface jakarta.servlet.ServletContext
      • setSessionTrackingModes

        public void setSessionTrackingModes​(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes)
        Specified by:
        setSessionTrackingModes in interface jakarta.servlet.ServletContext
      • getDefaultSessionTrackingModes

        public Set<jakarta.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
        Specified by:
        getDefaultSessionTrackingModes in interface jakarta.servlet.ServletContext
      • getEffectiveSessionTrackingModes

        public Set<jakarta.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
        Specified by:
        getEffectiveSessionTrackingModes in interface jakarta.servlet.ServletContext
      • getJspConfigDescriptor

        public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
        Specified by:
        getJspConfigDescriptor in interface jakarta.servlet.ServletContext
      • getClassLoader

        public ClassLoader getClassLoader()
        Specified by:
        getClassLoader in interface jakarta.servlet.ServletContext
      • normalize

        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. If the specified path attempts to go outside the boundaries of the current context (i.e. too many ".." path elements are present), return null instead.
        Parameters:
        path - Path to be normalized
      • getBasePath

        protected String 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 the SessionManager that should be used by this WebappContext. The default is an instance of ServletSessionManager
        Parameters:
        sessionManager - an implementation of SessionManager
      • getEventListeners

        protected EventListener[] getEventListeners()
        Returns:
      • addFilterMap

        protected void addFilterMap​(FilterMap filterMap,
                                    boolean isMatchAfter)
        Add a filter mapping to this Context.
        Parameters:
        filterMap - The filter mapping to be added
        isMatchAfter - 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
      • removeFilterMaps

        protected void removeFilterMaps()
        Removes any filter mappings from this Context.
      • getServletNameFilterMappings

        protected Collection<String> getServletNameFilterMappings​(String filterName)
        Gets the current servlet name mappings of the Filter with the given name.
      • getUrlPatternFilterMappings

        protected Collection<String> getUrlPatternFilterMappings​(String filterName)
        Gets the current URL pattern mappings of the Filter with the given name.
      • 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

        protected EventListener createEventListenerInstance​(String eventListenerClassname)
                                                     throws Exception
        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

        protected boolean validateURLPattern​(String urlPattern)
        Validate the syntax of a proposed <url-pattern> for conformance with specification requirements.
        Parameters:
        urlPattern - URL pattern to be validated