org.apache.catalina.servlets

## Class DefaultServlet

- java.lang.Object
- - javax.servlet.GenericServlet
  - - javax.servlet.http.HttpServlet
    - - org.apache.catalina.servlets.DefaultServlet

All Implemented Interfaces:
  Serializable, Servlet, ServletConfig

Direct Known Subclasses:
  WebdavServlet

    public class DefaultServlet
    extends HttpServlet

The default resource-serving servlet for most web applications, used to serve static resources such as HTML pages and images.

This servlet is intended to be mapped to / e.g.:

       <servlet-mapping>
           <servlet-name>default</servlet-name>
           <url-pattern>/</url-pattern>
       </servlet-mapping>

It can be mapped to sub-paths, however in all cases resources are served from the web appplication resource root using the full path from the root of the web application context.
e.g. given a web application structure:

     /context
       /images
         tomcat2.jpg
       /static
         /images
           tomcat.jpg

... and a servlet mapping that maps only `/static/*` to the default servlet:

       <servlet-mapping>
           <servlet-name>default</servlet-name>
           <url-pattern>/static/*</url-pattern>
       </servlet-mapping>

Then a request to `/context/static/images/tomcat.jpg` will succeed while a request to `/context/images/tomcat2.jpg` will fail.

Author:
  Craig R. McClanahan, Remy Maucherat
See Also:
  Serialized Form

### Nested Class Summary

Nested Classes
`protected static class` `DefaultServlet.Range`

### Field Summary

Fields
`protected static int` `BUFFER_SIZE` Size of file transfer buffer in bytes.
`protected String` `contextXsltFile` Allow customized directory listing per context.
`protected int` `debug` The debugging detail level for this servlet.
`protected String` `fileEncoding` File encoding to be used when reading static files.
`protected static ArrayList<DefaultServlet.Range>` `FULL` Full range marker.
`protected String` `globalXsltFile` Allow customized directory listing per instance.
`protected int` `input` The input buffer size to use when serving resources.
`protected boolean` `listings` Should we generate directory listings?
`protected String` `localXsltFile` Allow customized directory listing per directory.
`protected static String` `mimeSeparation` MIME multipart separation string
`protected int` `output` The output buffer size to use when serving resources.
`protected String` `readmeFile` Allow a readme file to be included.
`protected boolean` `readOnly` Read only flag.
`protected ProxyDirContext` `resources` Proxy directory context.
`protected static String` `RESOURCES_JNDI_NAME` JNDI resources name.
`protected int` `sendfileSize` Minimum size for sendfile usage in bytes.
`protected boolean` `showServerInfo` Flag to determine if server information is presented.
`protected static StringManager` `sm` The string manager for this package.
`protected static URLEncoder` `urlEncoder` Array containing the safe characters set.
`protected boolean` `useAcceptRanges` Should the Accept-Ranges: bytes header be send with static resources?

### Constructor Summary

Constructors
Constructor and Description
`DefaultServlet()`

### Method Summary

Methods
`protected boolean` `checkIfHeaders(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)` Check if the conditions specified in the optional If headers are satisfied.
`protected boolean` `checkIfMatch(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)` Check if the if-match condition is satisfied.
`protected boolean` `checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)` Check if the if-modified-since condition is satisfied.
`protected boolean` `checkIfNoneMatch(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)` Check if the if-none-match condition is satisfied.
`protected boolean` `checkIfUnmodifiedSince(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)` Check if the if-unmodified-since condition is satisfied.
`protected boolean` `checkSendfile(HttpServletRequest request, HttpServletResponse response, CacheEntry entry, long length, DefaultServlet.Range range)` Check if sendfile can be used.
`protected void` `copy(CacheEntry cacheEntry, InputStream is, PrintWriter writer)` Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
`protected void` `copy(CacheEntry cacheEntry, InputStream is, ServletOutputStream ostream)` Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
`protected void` `copy(CacheEntry cacheEntry, ServletOutputStream ostream, DefaultServlet.Range range)` Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
`protected void` `copy(CacheEntry cacheEntry, ServletOutputStream ostream, Iterator<DefaultServlet.Range> ranges, String contentType)` Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
`protected IOException` `copyRange(InputStream istream, ServletOutputStream ostream)` Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
`protected IOException` `copyRange(InputStream istream, ServletOutputStream ostream, long start, long end)` Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
`protected IOException` `copyRange(Reader reader, PrintWriter writer)` Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
`void` `destroy()` Finalize this servlet.
`protected void` `displaySize(StringBuilder buf, int filesize)` Deprecated. Will be removed in Tomcat 8.0.x. Replaced by `renderSize(long)`
`protected void` `doDelete(HttpServletRequest req, HttpServletResponse resp)` Process a DELETE request for the specified resource.
`protected void` `doGet(HttpServletRequest request, HttpServletResponse response)` Process a GET request for the specified resource.
`protected void` `doHead(HttpServletRequest request, HttpServletResponse response)` Process a HEAD request for the specified resource.
`protected void` `doOptions(HttpServletRequest req, HttpServletResponse resp)` Override default implementation to ensure that TRACE is correctly handled.
`protected void` `doPost(HttpServletRequest request, HttpServletResponse response)` Process a POST request for the specified resource.
`protected void` `doPut(HttpServletRequest req, HttpServletResponse resp)` Process a PUT request for the specified resource.
`protected File` `executePartialPut(HttpServletRequest req, DefaultServlet.Range range, String path)` Handle a partial PUT.
`protected Source` `findXsltInputStream(DirContext directory)` Return a Source for the xsl template (if possible)
`protected String` `getPathPrefix(HttpServletRequest request)` Determines the appropriate path to prepend resources with when generating directory listings.
`protected String` `getReadme(DirContext directory)` Get the readme file as a string.
`protected String` `getRelativePath(HttpServletRequest request)` Return the relative path associated with this servlet.
`void` `init()` Initialize this servlet.
`protected DefaultServlet.Range` `parseContentRange(HttpServletRequest request, HttpServletResponse response)` Parse the content-range header.
`protected ArrayList<DefaultServlet.Range>` `parseRange(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)` Parse the range header.
`protected InputStream` `render(String contextPath, CacheEntry cacheEntry)` Decide which way to render.
`protected InputStream` `renderHtml(String contextPath, CacheEntry cacheEntry)` Return an InputStream to an HTML representation of the contents of this directory.
`protected String` `renderSize(long size)` Render the specified file size (in bytes).
`protected InputStream` `renderXml(String contextPath, CacheEntry cacheEntry, Source xsltSource)` Return an InputStream to an HTML representation of the contents of this directory.
`protected String` `rewriteUrl(String path)` URL rewriter.
`protected void` `serveResource(HttpServletRequest request, HttpServletResponse response, boolean content)` Serve the specified resource, optionally including the data content.

### Methods inherited from class javax.servlet.http.HttpServlet

`doTrace, getLastModified, service, service`

### Methods inherited from class javax.servlet.GenericServlet

`getInitParameter, 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 Detail

#### debug

    protected int debug

The debugging detail level for this servlet.

#### input

    protected int input

The input buffer size to use when serving resources.

#### listings

    protected boolean listings

Should we generate directory listings?

#### readOnly

    protected boolean readOnly

Read only flag. By default, it's set to true.

#### output

    protected int output

The output buffer size to use when serving resources.

#### urlEncoder

    protected static final URLEncoder urlEncoder

Array containing the safe characters set.

#### localXsltFile

    protected String localXsltFile

Allow customized directory listing per directory.

#### contextXsltFile

    protected String contextXsltFile

Allow customized directory listing per context.

#### globalXsltFile

    protected String globalXsltFile

Allow customized directory listing per instance.

#### readmeFile

    protected String readmeFile

Allow a readme file to be included.

#### resources

    protected transient ProxyDirContext resources

Proxy directory context.

#### fileEncoding

    protected String fileEncoding

File encoding to be used when reading static files. If none is specified the platform default is used.

#### sendfileSize

    protected int sendfileSize

Minimum size for sendfile usage in bytes.

#### useAcceptRanges

    protected boolean useAcceptRanges

Should the Accept-Ranges: bytes header be send with static resources?

#### FULL

    protected static final ArrayList<DefaultServlet.Range> FULL

Full range marker.

#### showServerInfo

    protected boolean showServerInfo

Flag to determine if server information is presented.

#### mimeSeparation

    protected static final String mimeSeparation

MIME multipart separation string

See Also:
  Constant Field Values

#### RESOURCES_JNDI_NAME

    protected static final String RESOURCES_JNDI_NAME

JNDI resources name.

See Also:
  Constant Field Values

#### sm

    protected static final StringManager sm

The string manager for this package.

#### BUFFER_SIZE

    protected static final int BUFFER_SIZE

Size of file transfer buffer in bytes.

See Also:
  Constant Field Values

### Constructor Detail

#### DefaultServlet

    public DefaultServlet()

### Method Detail

#### destroy

    public void destroy()

Finalize this servlet.

Specified by:
  `destroy` in interface `Servlet`
Overrides:
  `destroy` in class `GenericServlet`

#### init

    public void init()
              throws ServletException

Initialize this servlet.

Overrides:
  `init` in class `GenericServlet`
Throws:
  `ServletException` - if an exception occurs that interrupts the servlet's normal operation

#### getRelativePath

    protected String getRelativePath(HttpServletRequest request)

Return the relative path associated with this servlet.

Parameters:
  `request` - The servlet request we are processing

#### getPathPrefix

    protected String getPathPrefix(HttpServletRequest request)

Determines the appropriate path to prepend resources with when generating directory listings. Depending on the behaviour of `getRelativePath(HttpServletRequest)` this will change.

Parameters:
  `request` - the request to determine the path for
Returns:
  the prefix to apply to all resources in the listing.

#### doGet

    protected void doGet(HttpServletRequest request,
             HttpServletResponse response)
                  throws IOException,
                         ServletException

Process a GET request for the specified resource.

Overrides:
  `doGet` in class `HttpServlet`
Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
Throws:
  `IOException` - if an input/output error occurs
  `ServletException` - if a servlet-specified error occurs
See Also:
  `ServletResponse.setContentType(java.lang.String)`

#### doHead

    protected void doHead(HttpServletRequest request,
              HttpServletResponse response)
                   throws IOException,
                          ServletException

Process a HEAD request for the specified resource.

Overrides:
  `doHead` in class `HttpServlet`
Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
Throws:
  `IOException` - if an input/output error occurs
  `ServletException` - if a servlet-specified error occurs

#### doOptions

    protected void doOptions(HttpServletRequest req,
                 HttpServletResponse resp)
                      throws ServletException,
                             IOException

Override default implementation to ensure that TRACE is correctly handled.

Overrides:
  `doOptions` in class `HttpServlet`
Parameters:
  `req` - the `HttpServletRequest` object that contains the request the client made of the servlet
  `resp` - the `HttpServletResponse` object that contains the response the servlet returns to the client
Throws:
  `IOException` - if an input or output error occurs while the servlet is handling the OPTIONS request
  `ServletException` - if the request for the OPTIONS cannot be handled

#### doPost

    protected void doPost(HttpServletRequest request,
              HttpServletResponse response)
                   throws IOException,
                          ServletException

Process a POST request for the specified resource.

Overrides:
  `doPost` in class `HttpServlet`
Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
Throws:
  `IOException` - if an input/output error occurs
  `ServletException` - if a servlet-specified error occurs
See Also:
  `ServletOutputStream`, `ServletResponse.setContentType(java.lang.String)`

#### doPut

    protected void doPut(HttpServletRequest req,
             HttpServletResponse resp)
                  throws ServletException,
                         IOException

Process a PUT request for the specified resource.

Overrides:
  `doPut` in class `HttpServlet`
Parameters:
  `req` - The servlet request we are processing
  `resp` - The servlet response we are creating
Throws:
  `IOException` - if an input/output error occurs
  `ServletException` - if a servlet-specified error occurs

#### executePartialPut

    protected File executePartialPut(HttpServletRequest req,
                         DefaultServlet.Range range,
                         String path)
                              throws IOException

Handle a partial PUT. New content specified in request is appended to existing content in oldRevisionContent (if present). This code does not support simultaneous partial updates to the same resource.

Throws:
  `IOException`

#### doDelete

    protected void doDelete(HttpServletRequest req,
                HttpServletResponse resp)
                     throws ServletException,
                            IOException

Process a DELETE request for the specified resource.

Overrides:
  `doDelete` in class `HttpServlet`
Parameters:
  `req` - The servlet request we are processing
  `resp` - The servlet response we are creating
Throws:
  `IOException` - if an input/output error occurs
  `ServletException` - if a servlet-specified error occurs

#### checkIfHeaders

    protected boolean checkIfHeaders(HttpServletRequest request,
                         HttpServletResponse response,
                         ResourceAttributes resourceAttributes)
                              throws IOException

Check if the conditions specified in the optional If headers are satisfied.

Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
  `resourceAttributes` - The resource information
Returns:
  boolean true if the resource meets all the specified conditions, and false if any of the conditions is not satisfied, in which case request processing is stopped
Throws:
  `IOException`

#### rewriteUrl

    protected String rewriteUrl(String path)

URL rewriter.

Parameters:
  `path` - Path which has to be rewritten

#### displaySize

    @Deprecated
    protected void displaySize(StringBuilder buf,
                              int filesize)

Deprecated. Will be removed in Tomcat 8.0.x. Replaced by `renderSize(long)`
Display the size of a file.

#### serveResource

    protected void serveResource(HttpServletRequest request,
                     HttpServletResponse response,
                     boolean content)
                          throws IOException,
                                 ServletException

Serve the specified resource, optionally including the data content.

Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
  `content` - Should the content be included?
Throws:
  `IOException` - if an input/output error occurs
  `ServletException` - if a servlet-specified error occurs

#### parseContentRange

    protected DefaultServlet.Range parseContentRange(HttpServletRequest request,
                                         HttpServletResponse response)
                                              throws IOException

Parse the content-range header.

Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
Returns:
  Range
Throws:
  `IOException`

#### parseRange

    protected ArrayList<DefaultServlet.Range> parseRange(HttpServletRequest request,
                                             HttpServletResponse response,
                                             ResourceAttributes resourceAttributes)
                                                  throws IOException

Parse the range header.

Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
Returns:
  Vector of ranges
Throws:
  `IOException`

#### render

    protected InputStream render(String contextPath,
                     CacheEntry cacheEntry)
                          throws IOException,
                                 ServletException

Decide which way to render. HTML or XML.

Throws:
  `IOException`
  `ServletException`

#### renderXml

    protected InputStream renderXml(String contextPath,
                        CacheEntry cacheEntry,
                        Source xsltSource)
                             throws IOException,
                                    ServletException

Return an InputStream to an HTML representation of the contents of this directory.

Parameters:
  `contextPath` - Context path to which our internal paths are relative
Throws:
  `IOException`
  `ServletException`

#### renderHtml

    protected InputStream renderHtml(String contextPath,
                         CacheEntry cacheEntry)
                              throws IOException,
                                     ServletException

Return an InputStream to an HTML representation of the contents of this directory.

Parameters:
  `contextPath` - Context path to which our internal paths are relative
Throws:
  `IOException`
  `ServletException`

#### renderSize

    protected String renderSize(long size)

Render the specified file size (in bytes).

Parameters:
  `size` - File size (in bytes)

#### getReadme

    protected String getReadme(DirContext directory)
                        throws IOException

Get the readme file as a string.

Throws:
  `IOException`

#### findXsltInputStream

    protected Source findXsltInputStream(DirContext directory)
                                  throws IOException

Return a Source for the xsl template (if possible)

Throws:
  `IOException`

#### checkSendfile

    protected boolean checkSendfile(HttpServletRequest request,
                        HttpServletResponse response,
                        CacheEntry entry,
                        long length,
                        DefaultServlet.Range range)

Check if sendfile can be used.

#### checkIfMatch

    protected boolean checkIfMatch(HttpServletRequest request,
                       HttpServletResponse response,
                       ResourceAttributes resourceAttributes)
                            throws IOException

Check if the if-match condition is satisfied.

Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
  `resourceAttributes` - File object
Returns:
  boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
  `IOException`

#### checkIfModifiedSince

    protected boolean checkIfModifiedSince(HttpServletRequest request,
                               HttpServletResponse response,
                               ResourceAttributes resourceAttributes)

Check if the if-modified-since condition is satisfied.

Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
  `resourceAttributes` - File object
Returns:
  boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped

#### checkIfNoneMatch

    protected boolean checkIfNoneMatch(HttpServletRequest request,
                           HttpServletResponse response,
                           ResourceAttributes resourceAttributes)
                                throws IOException

Check if the if-none-match condition is satisfied.

Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
  `resourceAttributes` - File object
Returns:
  boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
  `IOException`

#### checkIfUnmodifiedSince

    protected boolean checkIfUnmodifiedSince(HttpServletRequest request,
                                 HttpServletResponse response,
                                 ResourceAttributes resourceAttributes)
                                      throws IOException

Check if the if-unmodified-since condition is satisfied.

Parameters:
  `request` - The servlet request we are processing
  `response` - The servlet response we are creating
  `resourceAttributes` - File object
Returns:
  boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
  `IOException`

#### copy

    protected void copy(CacheEntry cacheEntry,
            InputStream is,
            ServletOutputStream ostream)
                 throws IOException

Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
  `cacheEntry` - The cache entry for the source resource
  `is` - The input stream to read the source resource from
  `ostream` - The output stream to write to
Throws:
  `IOException` - if an input/output error occurs

#### copy

    protected void copy(CacheEntry cacheEntry,
            InputStream is,
            PrintWriter writer)
                 throws IOException

Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
  `cacheEntry` - The cache entry for the source resource
  `is` - The input stream to read the source resource from
  `writer` - The writer to write to
Throws:
  `IOException` - if an input/output error occurs

#### copy

    protected void copy(CacheEntry cacheEntry,
            ServletOutputStream ostream,
            DefaultServlet.Range range)
                 throws IOException

Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
  `cacheEntry` - The cache entry for the source resource
  `ostream` - The output stream to write to
  `range` - Range the client wanted to retrieve
Throws:
  `IOException` - if an input/output error occurs

#### copy

    protected void copy(CacheEntry cacheEntry,
            ServletOutputStream ostream,
            Iterator<DefaultServlet.Range> ranges,
            String contentType)
                 throws IOException

Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
  `cacheEntry` - The cache entry for the source resource
  `ostream` - The output stream to write to
  `ranges` - Enumeration of the ranges the client wanted to retrieve
  `contentType` - Content type of the resource
Throws:
  `IOException` - if an input/output error occurs

#### copyRange

    protected IOException copyRange(InputStream istream,
                        ServletOutputStream ostream)

Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
  `istream` - The input stream to read from
  `ostream` - The output stream to write to
Returns:
  Exception which occurred during processing

#### copyRange

    protected IOException copyRange(Reader reader,
                        PrintWriter writer)

Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
  `reader` - The reader to read from
  `writer` - The writer to write to
Returns:
  Exception which occurred during processing

#### copyRange

    protected IOException copyRange(InputStream istream,
                        ServletOutputStream ostream,
                        long start,
                        long end)

Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
  `istream` - The input stream to read from
  `ostream` - The output stream to write to
  `start` - Start of the range which will be copied
  `end` - End of the range which will be copied
Returns:
  Exception which occurred during processing