Class DefaultServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
org.eclipse.jetty.servlet.DefaultServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable, ResourceService.WelcomeFactory, ResourceFactory

public class DefaultServlet extends jakarta.servlet.http.HttpServlet implements ResourceFactory, ResourceService.WelcomeFactory
The default servlet.

This servlet, normally mapped to /, provides the handling for static content, OPTION and TRACE methods for the context. The following initParameters are supported, these can be set either on the servlet itself or as ServletContext initParameters with a prefix of org.eclipse.jetty.servlet.Default. :

  acceptRanges      If true, range requests and responses are
                    supported

  dirAllowed        If true, directory listings are returned if no
                    welcome file is found. Else 403 Forbidden.

  welcomeServlets   If true, attempt to dispatch to welcome files
                    that are servlets, but only after no matching static
                    resources could be found. If false, then a welcome
                    file must exist on disk. If "exact", then exact
                    servlet matches are supported without an existing file.
                    Default is false.

                    This must be false if you want directory listings,
                    but have index.jsp in your welcome file list.

  redirectWelcome   If true, welcome files are redirected rather than
                    forwarded to.

  gzip              If set to true, then static content will be served as
                    gzip content encoded if a matching resource is
                    found ending with ".gz" (default false)
                    (deprecated: use precompressed)

  precompressed     If set to a comma separated list of encoding types (that may be
                    listed in a requests Accept-Encoding header) to file
                    extension mappings to look for and serve. For example:
                    "br=.br,gzip=.gz,bzip2=.bz".
                    If set to a boolean True, then a default set of compressed formats
                    will be used, otherwise no precompressed formats.

  resourceBase      Set to replace the context resource base

  resourceCache     If set, this is a context attribute name, which the servlet
                    will use to look for a shared ResourceCache instance.

  relativeResourceBase
                    Set with a pathname relative to the base of the
                    servlet context root. Useful for only serving static content out
                    of only specific subdirectories.

  pathInfoOnly      If true, only the path info will be applied to the resourceBase

  stylesheet        Set with the location of an optional stylesheet that will be used
                    to decorate the directory listing html.

  etags             If True, weak etags will be generated and handled.

  maxCacheSize      The maximum total size of the cache or 0 for no cache.
  maxCachedFileSize The maximum size of a file to cache
  maxCachedFiles    The maximum number of files to cache

  useFileMappedBuffer
                    If set to true, it will use mapped file buffer to serve static content
                    when using NIO connector. Setting this value to false means that
                    a direct buffer will be used instead of a mapped file buffer.
                    This is set to false by default by this class, but may be overridden
                    by eg webdefault.xml

  cacheControl      If set, all static content will have this value set as the cache-control
                    header.

  otherGzipFileExtensions
                    Other file extensions that signify that a file is already compressed. Eg ".svgz"

  encodingHeaderCacheSize
                    Max entries in a cache of ACCEPT-ENCODING headers.
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected void
    doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
     
    protected void
    doHead(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
     
    protected void
    doOptions(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
     
    protected void
    doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
     
    protected void
    doTrace(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
     
     
    getResource(String pathInContext)
    get Resource to serve.
    getWelcomeFile(String pathInContext)
    Finds a matching welcome file for the supplied Resource.
    void
     
    protected ContextHandler
    initContextHandler(jakarta.servlet.ServletContext servletContext)
    Compute the field _contextHandler.
    In the case where the DefaultServlet is deployed on the HttpService it is likely that this method needs to be overwritten to unwrap the ServletContext facade until we reach the original jetty's ContextHandler.

    Methods inherited from class jakarta.servlet.http.HttpServlet

    doDelete, doPut, getLastModified, service, service

    Methods inherited from class jakarta.servlet.GenericServlet

    getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • DefaultServlet

      public DefaultServlet(ResourceService resourceService)
    • DefaultServlet

      public DefaultServlet()
  • Method Details

    • init

      public void init() throws jakarta.servlet.UnavailableException
      Overrides:
      init in class jakarta.servlet.GenericServlet
      Throws:
      jakarta.servlet.UnavailableException
    • initContextHandler

      protected ContextHandler initContextHandler(jakarta.servlet.ServletContext servletContext)
      Compute the field _contextHandler.
      In the case where the DefaultServlet is deployed on the HttpService it is likely that this method needs to be overwritten to unwrap the ServletContext facade until we reach the original jetty's ContextHandler.
      Parameters:
      servletContext - The servletContext of this servlet.
      Returns:
      the jetty's ContextHandler for this servletContext.
    • getInitParameter

      public String getInitParameter(String name)
      Specified by:
      getInitParameter in interface jakarta.servlet.ServletConfig
      Overrides:
      getInitParameter in class jakarta.servlet.GenericServlet
    • getResource

      public Resource getResource(String pathInContext)
      get Resource to serve. Map a path to a resource. The default implementation calls HttpContext.getResource but derived servlets may provide their own mapping.
      Specified by:
      getResource in interface ResourceFactory
      Parameters:
      pathInContext - The path to find a resource for.
      Returns:
      The resource to serve.
    • doGet

      protected void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Overrides:
      doGet in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
      IOException
    • doPost

      protected void doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Overrides:
      doPost in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
      IOException
    • doHead

      protected void doHead(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Overrides:
      doHead in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
      IOException
    • doTrace

      protected void doTrace(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Overrides:
      doTrace in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
      IOException
    • doOptions

      protected void doOptions(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Overrides:
      doOptions in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
      IOException
    • destroy

      public void destroy()
      Specified by:
      destroy in interface jakarta.servlet.Servlet
      Overrides:
      destroy in class jakarta.servlet.GenericServlet
    • getWelcomeFile

      public String getWelcomeFile(String pathInContext)
      Description copied from interface: ResourceService.WelcomeFactory
      Finds a matching welcome file for the supplied Resource.
      Specified by:
      getWelcomeFile in interface ResourceService.WelcomeFactory
      Parameters:
      pathInContext - the path of the request
      Returns:
      The path of the matching welcome file in context or null.