Class ServletContextHandler

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
org.eclipse.jetty.server.Handler.Abstract
org.eclipse.jetty.server.Handler.AbstractContainer
org.eclipse.jetty.server.Handler.Wrapper
org.eclipse.jetty.server.handler.ContextHandler
org.eclipse.jetty.ee10.servlet.ServletContextHandler
All Implemented Interfaces:
org.eclipse.jetty.server.AliasCheck, org.eclipse.jetty.server.Deployable, org.eclipse.jetty.server.Handler, org.eclipse.jetty.server.Handler.Container, org.eclipse.jetty.server.Handler.Singleton, org.eclipse.jetty.server.Request.Handler, org.eclipse.jetty.util.Attributes, org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.Dumpable.DumpableContainer, org.eclipse.jetty.util.component.LifeCycle, org.eclipse.jetty.util.thread.Invocable

@ManagedObject("Servlet Context Handler") public class ServletContextHandler extends org.eclipse.jetty.server.handler.ContextHandler
Servlet Context.

This extension to the ContextHandler allows for simple construction of a context with ServletHandler and optionally session and security handlers, et.

  new ServletContext("/context",Context.SESSIONS|Context.NO_SECURITY);

This class should have been called ServletContext, but this would have cause confusion with ServletContext.

  • Field Details

    • ENVIRONMENT

      public static final org.eclipse.jetty.util.component.Environment ENVIRONMENT
    • __environment

      @Deprecated(since="12.0.9", forRemoval=true) public static final org.eclipse.jetty.util.component.Environment __environment
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use ENVIRONMENT instead.
    • SERVLET_LISTENER_TYPES

      public static final Class<?>[] SERVLET_LISTENER_TYPES
    • DEFAULT_LISTENER_TYPE_INDEX

      public static final int DEFAULT_LISTENER_TYPE_INDEX
      See Also:
    • EXTENDED_LISTENER_TYPE_INDEX

      public static final int EXTENDED_LISTENER_TYPE_INDEX
      See Also:
    • MAX_FORM_KEYS_KEY

      public static final String MAX_FORM_KEYS_KEY
      See Also:
    • MAX_FORM_CONTENT_SIZE_KEY

      public static final String MAX_FORM_CONTENT_SIZE_KEY
      See Also:
    • DEFAULT_MAX_FORM_KEYS

      public static final int DEFAULT_MAX_FORM_KEYS
      See Also:
    • DEFAULT_MAX_FORM_CONTENT_SIZE

      public static final int DEFAULT_MAX_FORM_CONTENT_SIZE
      See Also:
    • SESSIONS

      public static final int SESSIONS
      See Also:
    • SECURITY

      public static final int SECURITY
      See Also:
    • NO_SESSIONS

      public static final int NO_SESSIONS
      See Also:
    • NO_SECURITY

      public static final int NO_SECURITY
      See Also:
    • _contextStatus

      protected ServletContextHandler.ContextStatus _contextStatus
    • _objFactory

      protected final org.eclipse.jetty.util.DecoratedObjectFactory _objFactory
    • _sessionHandler

      protected SessionHandler _sessionHandler
    • _securityHandler

      protected org.eclipse.jetty.security.SecurityHandler _securityHandler
    • _servletHandler

      protected ServletHandler _servletHandler
    • _options

      protected int _options
    • _jspConfig

      protected jakarta.servlet.descriptor.JspConfigDescriptor _jspConfig
  • Constructor Details

    • ServletContextHandler

      public ServletContextHandler()
    • ServletContextHandler

      public ServletContextHandler(String contextPath)
    • ServletContextHandler

      public ServletContextHandler(int options)
    • ServletContextHandler

      public ServletContextHandler(String contextPath, int options)
    • ServletContextHandler

      public ServletContextHandler(String contextPath, boolean sessions, boolean security)
    • ServletContextHandler

      public ServletContextHandler(SessionHandler sessionHandler, org.eclipse.jetty.security.SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
    • ServletContextHandler

      public ServletContextHandler(String contextPath, SessionHandler sessionHandler, org.eclipse.jetty.security.SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
    • ServletContextHandler

      public ServletContextHandler(String contextPath, SessionHandler sessionHandler, org.eclipse.jetty.security.SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler, int options)
  • Method Details

    • getServletContextHandler

      public static ServletContextHandler getServletContextHandler(jakarta.servlet.ServletContext servletContext, String purpose)
    • getServletContextHandler

      public static ServletContextHandler getServletContextHandler(jakarta.servlet.ServletContext servletContext)
    • getCurrentServletContext

      public static jakarta.servlet.ServletContext getCurrentServletContext()
    • getServletContext

      public static jakarta.servlet.ServletContext getServletContext(org.eclipse.jetty.server.Context context)
    • getCurrentServletContextHandler

      public static ServletContextHandler getCurrentServletContextHandler()
    • setTempDirectory

      public void setTempDirectory(File tempDirectory)
      Overrides:
      setTempDirectory in class org.eclipse.jetty.server.handler.ContextHandler
    • newServletContextApi

      public ServletContextHandler.ServletContextApi newServletContextApi()
    • getInvocationType

      public org.eclipse.jetty.util.thread.Invocable.InvocationType getInvocationType()
      Specified by:
      getInvocationType in interface org.eclipse.jetty.server.Handler.Singleton
      Specified by:
      getInvocationType in interface org.eclipse.jetty.util.thread.Invocable
      Specified by:
      getInvocationType in interface org.eclipse.jetty.server.Request.Handler
      Overrides:
      getInvocationType in class org.eclipse.jetty.server.Handler.Wrapper
    • dump

      public void dump(Appendable out, String indent) throws IOException
      Specified by:
      dump in interface org.eclipse.jetty.util.component.Dumpable
      Overrides:
      dump in class org.eclipse.jetty.server.handler.ContextHandler
      Throws:
      IOException
    • isUsingSecurityManager

      public boolean isUsingSecurityManager()
    • setUsingSecurityManager

      public void setUsingSecurityManager(boolean usingSecurityManager)
    • getContextPathEncoded

      public String getContextPathEncoded()
      Returns:
      Returns the encoded contextPath.
    • getRequestContextPath

      public String getRequestContextPath()
      Get the context path in a form suitable to be returned from HttpServletRequest.getContextPath() or ServletContext.getContextPath().
      Returns:
      Returns the encoded contextPath, or empty string for root context
    • getInitParameter

      public String getInitParameter(String name)
    • setInitParameter

      public String setInitParameter(String name, String value)
    • getInitParameterNames

      public Enumeration<String> getInitParameterNames()
    • getInitParams

      @ManagedAttribute("Initial Parameter map for the context") public Map<String,String> getInitParams()
      Returns:
      Returns the initParams.
    • removeEventListener

      public boolean removeEventListener(EventListener listener)
      Specified by:
      removeEventListener in interface org.eclipse.jetty.util.component.Container
      Specified by:
      removeEventListener in interface org.eclipse.jetty.util.component.LifeCycle
      Overrides:
      removeEventListener in class org.eclipse.jetty.server.handler.ContextHandler
    • addProgrammaticListener

      protected void addProgrammaticListener(EventListener listener)
      Apply any necessary restrictions on a programmatic added listener.
      Parameters:
      listener - the programmatic listener to add
    • isProgrammaticListener

      public boolean isProgrammaticListener(EventListener listener)
    • isDurableListener

      public boolean isDurableListener(EventListener listener)
    • getLogger

      public org.slf4j.Logger getLogger()
    • setLogger

      public void setLogger(org.slf4j.Logger logger)
    • contextInitialized

      public void contextInitialized() throws Exception
      Call the ServletContextListeners contextInitialized methods. This can be called from a ServletHandler during the proper sequence of initializing filters, servlets and listeners. However, if there is no ServletHandler, the ContextHandler will call this method during doStart().
      Throws:
      Exception
    • contextDestroyed

      public void contextDestroyed() throws Exception
      Call the ServletContextListeners with contextDestroyed. This method can be called from a ServletHandler in the proper sequence of destroying filters, servlets and listeners. If there is no ServletHandler, the ContextHandler must ensure these listeners are called instead.
      Throws:
      Exception
    • requestInitialized

      protected void requestInitialized(org.eclipse.jetty.server.Request baseRequest, jakarta.servlet.http.HttpServletRequest request)
    • requestDestroyed

      protected void requestDestroyed(org.eclipse.jetty.server.Request baseRequest, jakarta.servlet.http.HttpServletRequest request)
    • setDefaultRequestCharacterEncoding

      public void setDefaultRequestCharacterEncoding(String encoding)
    • getDefaultRequestCharacterEncoding

      public String getDefaultRequestCharacterEncoding()
    • setDefaultResponseCharacterEncoding

      public void setDefaultResponseCharacterEncoding(String encoding)
    • getDefaultResponseCharacterEncoding

      public String getDefaultResponseCharacterEncoding()
    • setContextPath

      public void setContextPath(String contextPath)
      Overrides:
      setContextPath in class org.eclipse.jetty.server.handler.ContextHandler
      Parameters:
      contextPath - The _contextPath to set.
    • setWelcomeFiles

      public void setWelcomeFiles(String[] files)
    • getWelcomeFiles

      @ManagedAttribute(value="Partial URIs of directory welcome files", readonly=true) public String[] getWelcomeFiles()
      Returns:
      The names of the files which the server should consider to be welcome files in this context.
      See Also:
    • getMaxFormContentSize

      @ManagedAttribute("The maximum content size") public int getMaxFormContentSize()
      Returns:
      the maximum size of the form content (in bytes).
    • setMaxFormContentSize

      public void setMaxFormContentSize(int maxSize)
      Set the maximum size of a form post, to protect against DOS attacks from large forms.
      Parameters:
      maxSize - the maximum size of the form content (in bytes) or -1 for a default value.
    • getMaxFormKeys

      @ManagedAttribute("The maximum number of form keys") public int getMaxFormKeys()
      Returns:
      the maximum number of form Keys.
    • setMaxFormKeys

      public void setMaxFormKeys(int max)
      Set the maximum number of form Keys to protect against DOS attack from crafted hash keys.
      Parameters:
      max - the maximum number of form keys or -1 for a default value.
    • loadClass

      public Class<?> loadClass(String className) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • addLocaleEncoding

      public void addLocaleEncoding(String locale, String encoding)
    • getLocaleEncoding

      public String getLocaleEncoding(String locale)
    • getLocaleEncoding

      public String getLocaleEncoding(Locale locale)
      Get the character encoding for a locale. The full locale name is first looked up in the map of encodings. If no encoding is found, then the locale language is looked up.
      Parameters:
      locale - a Locale value
      Returns:
      a String representing the character encoding for the locale or null if none found.
    • getLocaleEncodings

      public Map<String,String> getLocaleEncodings()
      Get all of the locale encodings
      Returns:
      a map of all the locale encodings: key is name of the locale and value is the char encoding
    • getResource

      public org.eclipse.jetty.util.resource.Resource getResource(String pathInContext) throws MalformedURLException
      Attempt to get a Resource from the Context.
      Parameters:
      pathInContext - the path within the base resource to attempt to get
      Returns:
      the resource, or null if not available.
      Throws:
      MalformedURLException - if unable to form a Resource from the provided path
    • newResource

      public org.eclipse.jetty.util.resource.Resource newResource(URL url) throws IOException
      Convert URL to Resource wrapper for ResourceFactory.newResource(URL) enables extensions to provide alternate resource implementations.
      Parameters:
      url - the url to convert to a Resource
      Returns:
      the Resource for that url
      Throws:
      IOException - if unable to create a Resource from the URL
    • newResource

      public org.eclipse.jetty.util.resource.Resource newResource(URI uri)
      Convert URL to Resource wrapper for ResourceFactory.newResource(URL) enables extensions to provide alternate resource implementations.
      Parameters:
      uri - the URI to convert to a Resource
      Returns:
      the Resource for that URI
    • newResource

      public org.eclipse.jetty.util.resource.Resource newResource(String urlOrPath)
      Convert a URL or path to a Resource. The default implementation is a wrapper for ResourceFactory.newResource(String).
      Parameters:
      urlOrPath - The URL or path to convert
      Returns:
      The Resource for the URL/path
    • getResourcePaths

      public Set<String> getResourcePaths(String path)
    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
    • newContext

      protected org.eclipse.jetty.server.handler.ContextHandler.ScopedContext newContext()
      Overrides:
      newContext in class org.eclipse.jetty.server.handler.ContextHandler
    • getContext

      Overrides:
      getContext in class org.eclipse.jetty.server.handler.ContextHandler
    • addEventListener

      public boolean addEventListener(EventListener listener)
      Add a context event listeners.
      Specified by:
      addEventListener in interface org.eclipse.jetty.util.component.Container
      Specified by:
      addEventListener in interface org.eclipse.jetty.util.component.LifeCycle
      Overrides:
      addEventListener in class org.eclipse.jetty.server.handler.ContextHandler
      Parameters:
      listener - the event listener to add
      Returns:
      true if the listener was added
      See Also:
      • HttpSessionAttributeListener
      • HttpSessionActivationListener
      • HttpSessionBindingListener
      • HttpSessionListener
      • HttpSessionIdListener
      • ServletContextHandler.ServletContextScopeListener
      • ServletContextListener
      • ServletContextAttributeListener
      • ServletRequestListener
      • ServletRequestAttributeListener
      • ContextHandler.addEventListener(EventListener)
    • setHandler

      public void setHandler(org.eclipse.jetty.server.Handler handler)
      Specified by:
      setHandler in interface org.eclipse.jetty.server.Handler.Singleton
      Overrides:
      setHandler in class org.eclipse.jetty.server.Handler.Wrapper
    • doStart

      protected void doStart() throws Exception
      Overrides:
      doStart in class org.eclipse.jetty.server.handler.ContextHandler
      Throws:
      Exception
    • doStop

      protected void doStop() throws Exception
      Overrides:
      doStop in class org.eclipse.jetty.server.handler.ContextHandler
      Throws:
      Exception
    • newServletContextRequest

      protected ServletContextRequest newServletContextRequest(ServletChannel servletChannel, org.eclipse.jetty.server.Request request, org.eclipse.jetty.server.Response response, String decodedPathInContext, org.eclipse.jetty.http.pathmap.MatchedResource<ServletHandler.MappedServlet> matchedResource)
    • wrapRequest

      protected org.eclipse.jetty.server.handler.ContextRequest wrapRequest(org.eclipse.jetty.server.Request request, org.eclipse.jetty.server.Response response)
      Overrides:
      wrapRequest in class org.eclipse.jetty.server.handler.ContextHandler
    • wrapResponse

      protected org.eclipse.jetty.server.handler.ContextResponse wrapResponse(org.eclipse.jetty.server.handler.ContextRequest request, org.eclipse.jetty.server.Response response)
      Overrides:
      wrapResponse in class org.eclipse.jetty.server.handler.ContextHandler
    • handleByContextHandler

      protected boolean handleByContextHandler(String pathInContext, org.eclipse.jetty.server.handler.ContextRequest request, org.eclipse.jetty.server.Response response, org.eclipse.jetty.util.Callback callback)
      Overrides:
      handleByContextHandler in class org.eclipse.jetty.server.handler.ContextHandler
    • notifyEnterScope

      protected void notifyEnterScope(org.eclipse.jetty.server.Request request)
      Overrides:
      notifyEnterScope in class org.eclipse.jetty.server.handler.ContextHandler
    • notifyExitScope

      protected void notifyExitScope(org.eclipse.jetty.server.Request request)
      Overrides:
      notifyExitScope in class org.eclipse.jetty.server.handler.ContextHandler
    • getDefaultSecurityHandlerClass

      public Class<? extends org.eclipse.jetty.security.SecurityHandler> getDefaultSecurityHandlerClass()
      Get the defaultSecurityHandlerClass.
      Returns:
      the defaultSecurityHandlerClass
    • setDefaultSecurityHandlerClass

      public void setDefaultSecurityHandlerClass(Class<? extends org.eclipse.jetty.security.SecurityHandler> defaultSecurityHandlerClass)
      Set the defaultSecurityHandlerClass.
      Parameters:
      defaultSecurityHandlerClass - the defaultSecurityHandlerClass to set
    • newSessionHandler

      protected SessionHandler newSessionHandler()
    • newSecurityHandler

      protected org.eclipse.jetty.security.SecurityHandler newSecurityHandler()
    • newServletHandler

      protected ServletHandler newServletHandler()
    • startContext

      protected void startContext() throws Exception
      Finish constructing handlers and link them together.
      Throws:
      Exception
    • stopContext

      protected void stopContext() throws Exception
      Throws:
      Exception
    • getSecurityHandler

      @ManagedAttribute(value="context security handler", readonly=true) public org.eclipse.jetty.security.SecurityHandler getSecurityHandler()
      Returns:
      Returns the securityHandler.
    • getServletHandler

      @ManagedAttribute(value="context servlet handler", readonly=true) public ServletHandler getServletHandler()
      Returns:
      Returns the servletHandler.
    • getSessionHandler

      @ManagedAttribute(value="context session handler", readonly=true) public SessionHandler getSessionHandler()
      Returns:
      Returns the sessionHandler.
    • addServlet

      public ServletHolder addServlet(String className, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      className - the servlet class name
      pathSpec - the path spec to map servlet to
      Returns:
      the ServletHolder for the added servlet
    • addServlet

      public ServletHolder addServlet(Class<? extends jakarta.servlet.Servlet> servlet, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      servlet - the servlet class
      pathSpec - the path spec to map servlet to
      Returns:
      the ServletHolder for the added servlet
    • addServlet

      public void addServlet(ServletHolder servlet, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      servlet - the servlet holder
      pathSpec - the path spec
    • addServlet

      public ServletHolder addServlet(jakarta.servlet.http.HttpServlet servlet, String pathSpec)
      Convenience method to add a servlet.
      Parameters:
      servlet - the servlet instance
      pathSpec - the path spec
      Returns:
      the ServletHolder for the added servlet
    • addFilter

      public void addFilter(FilterHolder holder, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter
      Parameters:
      holder - the filter holder
      pathSpec - the path spec
      dispatches - the dispatcher types for this filter
    • addFilter

      public FilterHolder addFilter(Class<? extends jakarta.servlet.Filter> filterClass, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter
      Parameters:
      filterClass - the filter class
      pathSpec - the path spec
      dispatches - the dispatcher types for this filter
      Returns:
      the FilterHolder that was created
    • addFilter

      public FilterHolder addFilter(String filterClass, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a filter
      Parameters:
      filterClass - the filter class name
      pathSpec - the path spec
      dispatches - the dispatcher types for this filter
      Returns:
      the FilterHolder that was created
    • addFilter

      public FilterHolder addFilter(jakarta.servlet.Filter filter, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
      Convenience method to add a servlet.
      Parameters:
      filter - the filter instance
      pathSpec - the path spec
      dispatches - the dispatcher types for this filter
      Returns:
      the FilterHolder that was created
    • addServletContainerInitializer

      public ServletContainerInitializerHolder addServletContainerInitializer(jakarta.servlet.ServletContainerInitializer sci)
      Convenience method to programmatically add a ServletContainerInitializer.
      Parameters:
      sci - the ServletContainerInitializer to register.
      Returns:
      the ServletContainerInitializerHolder that was created
    • addServletContainerInitializer

      public ServletContainerInitializerHolder addServletContainerInitializer(jakarta.servlet.ServletContainerInitializer sci, Class<?>... classes)
      Convenience method to programmatically add a ServletContainerInitializer.
      Parameters:
      sci - the ServletContainerInitializer to register.
      classes - the Set of application classes.
      Returns:
      the ServletContainerInitializerHolder that was created
    • addServletContainerInitializer

      public void addServletContainerInitializer(ServletContainerInitializerHolder... sciHolders)
      Convenience method to programmatically add a list of ServletContainerInitializer. The initializers are guaranteed to be called in the order they are passed into this method.
      Parameters:
      sciHolders - the ServletContainerInitializerHolders
    • dynamicHolderAdded

      protected jakarta.servlet.ServletRegistration.Dynamic dynamicHolderAdded(ServletHolder holder)
      notification that a ServletRegistration has been created so we can track the annotations
      Parameters:
      holder - new holder created through the api.
      Returns:
      the ServletRegistration.Dynamic
    • addRoles

      protected void addRoles(String... roleNames)
      delegate for ServletContext.declareRole method
      Parameters:
      roleNames - role names to add
    • setServletSecurity

      public Set<String> setServletSecurity(jakarta.servlet.ServletRegistration.Dynamic registration, jakarta.servlet.ServletSecurityElement servletSecurityElement)
      Delegate for ServletRegistration.Dynamic.setServletSecurity method
      Parameters:
      registration - ServletRegistration.Dynamic instance that setServletSecurity was called on
      servletSecurityElement - new security info
      Returns:
      the set of exact URL mappings currently associated with the registration that are also present in the web.xml security constraints and thus will be unaffected by this call.
    • callContextInitialized

      public void callContextInitialized(jakarta.servlet.ServletContextListener l, jakarta.servlet.ServletContextEvent e)
    • callContextDestroyed

      public void callContextDestroyed(jakarta.servlet.ServletContextListener l, jakarta.servlet.ServletContextEvent e)
    • setSessionHandler

      public void setSessionHandler(SessionHandler sessionHandler)
      Parameters:
      sessionHandler - The sessionHandler to set.
    • setSecurityHandler

      public void setSecurityHandler(org.eclipse.jetty.security.SecurityHandler securityHandler)
      Parameters:
      securityHandler - The SecurityHandler to set on this context.
    • setServletHandler

      public void setServletHandler(ServletHandler servletHandler)
      Parameters:
      servletHandler - The servletHandler to set.
    • insertHandler

      public void insertHandler(org.eclipse.jetty.server.Handler.Singleton handler)
      Insert a HandlerWrapper before the first Session, Security or ServletHandler but after any other HandlerWrappers.
    • getObjectFactory

      public org.eclipse.jetty.util.DecoratedObjectFactory getObjectFactory()
      The DecoratedObjectFactory for use by IoC containers (weld / spring / etc)
      Returns:
      The DecoratedObjectFactory