Top Description Inners Fields Constructors Methods
org.apache.catalina.servlets

public Class DefaultServlet

extends HttpServlet
Class Inheritance
Known Direct Subclasses
org.apache.catalina.servlets.WebdavServlet
Imports
java.io.BufferedInputStream, .ByteArrayInputStream, .ByteArrayOutputStream, .File, .FileInputStream, .FileNotFoundException, .IOException, .InputStream, .InputStreamReader, .OutputStreamWriter, .PrintWriter, .RandomAccessFile, .Reader, .Serializable, .StringReader, .StringWriter, .UnsupportedEncodingException, java.nio.charset.Charset, .StandardCharsets, java.security.AccessController, java.util.ArrayList, .Arrays, .Comparator, .Enumeration, .List, .Locale, java.util.function.Function, javax.xml.parsers.DocumentBuilder, .DocumentBuilderFactory, .ParserConfigurationException, javax.xml.transform.Source, .Transformer, .TransformerException, .TransformerFactory, javax.xml.transform.dom.DOMSource, javax.xml.transform.stream.StreamResult, .StreamSource, jakarta.servlet.DispatcherType, .RequestDispatcher, .ServletContext, .ServletException, .ServletOutputStream, .ServletResponse, .ServletResponseWrapper, .UnavailableException, jakarta.servlet.http.HttpServlet, .HttpServletRequest, .HttpServletResponse, org.apache.catalina.Context, .Globals, .WebResource, .WebResourceRoot, org.apache.catalina.connector.RequestFacade, .ResponseFacade, org.apache.catalina.util.IOTools, .ServerInfo, .URLEncoder, org.apache.catalina.webresources.CachedResource, org.apache.tomcat.util.buf.B2CConverter, org.apache.tomcat.util.http.ResponseUtil, org.apache.tomcat.util.http.parser.ContentRange, .EntityTag, .Ranges, org.apache.tomcat.util.res.StringManager, org.apache.tomcat.util.security.Escape, .PrivilegedGetTccl, .PrivilegedSetTccl, org.w3c.dom.Document, org.xml.sax.InputSource, .SAXException, org.xml.sax.ext.EntityResolver2

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

Authors
Craig R. McClanahan, Remy Maucherat

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum
protected static class
private static class
private static class
DefaultServlet.SecureEntityResolver

This is secure in the sense that any attempt to use an external entity will trigger an exception.

private static class
DefaultServlet.SortManager

A class encapsulating the sorting of resources.

Field Summary

Modifier and TypeField and Description
private boolean
allowPartialPut

Flag that indicates whether partial PUTs are permitted.

protected static final int
BUFFER_SIZE

Size of file transfer buffer in bytes.

protected DefaultServlet.CompressionFormat[]
compressionFormats

List of compression formats to serve and their preference order.

protected String
contextXsltFile

Allow customized directory listing per context.

protected int
debug

The debugging detail level for this servlet.

private static final DocumentBuilderFactory
protected String
fileEncoding

File encoding to be used when reading static files.

private transient Charset
protected static final Ranges
FULL

Full range marker.

protected String
globalXsltFile

Allow customized directory listing per instance.

private static final ContentRange
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 final 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 transient WebResourceRoot
resources

The complete set of web application resources

private static final DefaultServlet.SecureEntityResolver
protected int
sendfileSize

Minimum size for sendfile usage in bytes.

private static final long
protected boolean
showServerInfo

Flag to determine if server information is presented.

protected static final StringManager
sm

The string manager for this package.

protected boolean
sortListings

Flag to determine if resources should be sorted.

protected transient DefaultServlet.SortManager
sortManager

The sorting manager for sorting files and directories.

protected boolean
useAcceptRanges

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

private DefaultServlet.BomConfig
useBomIfPresent

If a file has a BOM, should that be used in preference to fileEncoding?

Inherited from jakarta.servlet.http.HttpServlet:
LEGACY_DO_HEAD

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

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
checkIfHeaders
(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
,
WebResource
The resource
resource
)

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

protected boolean

Returns:

true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
checkIfMatch
(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
,
WebResource
The resource
resource
)

Check if the if-match condition is satisfied.

protected boolean

Returns:

true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
checkIfModifiedSince
(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
,
WebResource
The resource
resource
)

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

protected boolean

Returns:

true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
checkIfNoneMatch
(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
,
WebResource
The resource
resource
)

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

protected boolean

Returns:

true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
checkIfUnmodifiedSince
(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
,
WebResource
The resource
resource
)

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

protected boolean

Returns:

true if sendfile should be used (writing is then delegated to the endpoint)
checkSendfile
(HttpServletRequest
The Servlet request
request
,
HttpServletResponse
The Servlet response
response
,
WebResource
The resource
resource
,
long
The length which will be written (will be used only if range is null)
length
,
Ranges.Entry
The range that will be written
range
)

Check if sendfile can be used.

private static Comparator<WebResource>
protected void
copy(InputStream
The input stream to read the source resource from
is
,
ServletOutputStream
The output stream to write to
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(InputStream
The input stream to read the source resource from
is
,
PrintWriter
The writer to write to
writer
,
String
The encoding to use when reading the source input stream
encoding
)

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(WebResource
The source resource
resource
,
long
the resource length
length
,
ServletOutputStream
The output stream to write to
ostream
,
Ranges.Entry
Range the client wanted to retrieve
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(WebResource
The source resource
resource
,
long
the resource length
length
,
ServletOutputStream
The output stream to write to
ostream
,
Ranges
Enumeration of the ranges the client wanted to retrieve
ranges
,
String
Content type of the resource
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

Returns:

Exception which occurred during processing
copyRange
(InputStream
The input stream to read from
istream
,
ServletOutputStream
The output stream to write to
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

Returns:

Exception which occurred during processing
copyRange
(Reader
The reader to read from
reader
,
PrintWriter
The writer to write to
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 IOException

Returns:

Exception which occurred during processing
copyRange
(InputStream
The input stream to read from
istream
,
ServletOutputStream
The output stream to write to
ostream
,
long
Start of the range which will be copied
start
,
long
End of the range which will be copied
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).

public void
protected String
protected void
doDelete(HttpServletRequest
The servlet request we are processing
req
,
HttpServletResponse
The servlet response we are creating
resp
)

Overrides jakarta.servlet.http.HttpServlet.doDelete.

Process a DELETE request for the specified resource.

private void
protected void
doGet(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
)

Overrides jakarta.servlet.http.HttpServlet.doGet.

Process a GET request for the specified resource.

protected void
doHead(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
)

Overrides jakarta.servlet.http.HttpServlet.doHead.

Process a HEAD request for the specified resource.

protected void
doOptions(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
req
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
resp
)

Overrides jakarta.servlet.http.HttpServlet.doOptions.

Override default implementation to ensure that TRACE is correctly handled.

protected void
doPost(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
)

Overrides jakarta.servlet.http.HttpServlet.doPost.

Process a POST request for the specified resource.

protected void
doPut(HttpServletRequest
The servlet request we are processing
req
,
HttpServletResponse
The servlet response we are creating
resp
)

Overrides jakarta.servlet.http.HttpServlet.doPut.

Process a PUT request for the specified resource.

protected File

Returns:

the associated file object
executePartialPut
(HttpServletRequest
The Servlet request
req
,
ContentRange
The range that will be written
range
,
String
The path
path
)

Handle a partial PUT.

protected Source

Returns:

the source for the specified directory
findXsltSource
(WebResource
The directory to search
directory
)

Return a Source for the xsl template (if possible).

protected String

Returns:

The result of calling WebResource#getETag() on the given resource
generateETag
(WebResource
The resource for which an entity tag is required.
resource
)

Provides the entity tag (the ETag header) for the given resource.

private List<DefaultServlet.PrecompressedResource>
private DefaultServlet.PrecompressedResource

Returns:

The best matching precompressed resource or null if no match was found.
getBestPrecompressedResource
(HttpServletRequest
The servlet request we are processing
request
,
List<DefaultServlet.PrecompressedResource>
List of available precompressed resources.
precompressedResources
)

Match the client preferred encoding formats to the available precompressed resources.

private static long
getEnd(Ranges.Entry range, long length)

private char

Returns:

Either 'A' or 'D', to indicate "ascending" or "descending" sort order.
getOrderChar
(DefaultServlet.SortManager.Order
The order that is currently being applied
order
,
char
The column that will be rendered.
column
)

Gets the ordering character to be used for a particular column.

protected String

Returns:

the prefix to apply to all resources in the listing.
getPathPrefix
(final HttpServletRequest
the request to determine the path for
request
)

Determines the appropriate path to prepend resources with when generating directory listings.

protected String

Returns:

the readme for the specified directory
getReadme
(WebResource
The directory to search
directory
,
String
The readme encoding
encoding
)

Get the readme file as a string.

protected String

Returns:

the relative path
getRelativePath
(HttpServletRequest
The servlet request we are processing
request
)

Return the relative path associated with this servlet.

protected String
getRelativePath(HttpServletRequest request, boolean allowEmptyPath)

private static long
getStart(Ranges.Entry range, long length)

public void
init()

Overrides jakarta.servlet.GenericServlet.init.

Initialize this servlet.

private static boolean
isText(String contentType)

private DefaultServlet.CompressionFormat[]
parseCompressionFormats(String precompressed, String gzip)

protected ContentRange

Returns:

the partial content-range, null if the content-range header was invalid or #IGNORE if there is no header to process
parseContentRange
(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
)

Parse the content-range header.

protected Ranges

Returns:

a list of ranges, null if the range header was invalid or #FULL if the Range header should be ignored.
parseRange
(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
,
WebResource
The resource
resource
)

Parse the range header.

private boolean
private static Charset
processBom(InputStream is, boolean stripBom)

protected InputStream

Returns:

the input stream with the rendered output
render
(HttpServletRequest
The HttpServletRequest being served
request
,
String
The path
contextPath
,
WebResource
The resource
resource
,
String
The encoding to use to process the readme (if any)
encoding
)

Decide which way to render.

protected InputStream

Returns:

the HTML data
renderHtml
(HttpServletRequest
The HttpServletRequest being served
request
,
String
Context path to which our internal paths are relative
contextPath
,
WebResource
The associated resource
resource
,
String
The encoding to use to process the readme (if any)
encoding
)

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

protected String

Returns:

the formatted size
renderSize
(long
File size (in bytes)
size
)

Render the specified file size (in bytes).

protected InputStream

Returns:

the XML data
renderXml
(HttpServletRequest
The HttpServletRequest being served
request
,
String
Context path to which our internal paths are relative
contextPath
,
WebResource
The associated resource
resource
,
Source
The XSL stylesheet
xsltSource
,
String
The encoding to use to process the readme (if any)
encoding
)

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

protected String

Returns:

the rewritten path
rewriteUrl
(String
Path which has to be rewritten
path
)

URL rewriter.

private Source
protected void
protected void
serveResource(HttpServletRequest
The servlet request we are processing
request
,
HttpServletResponse
The servlet response we are creating
response
,
boolean
Should the content be included?
content
,
String
The encoding to use if it is necessary to access the source as characters rather than as bytes
inputEncoding
)

Serve the specified resource, optionally including the data content.

protected void
service(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
req
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
resp
)

Overrides jakarta.servlet.http.HttpServlet.service.

Receives standard HTTP requests from the public service method and dispatches them to the doMethod methods defined in this class.

private static void
skip(InputStream is, int skip, boolean stripBom)

private static boolean
private static boolean
validate(Ranges.Entry range, long length)

private File
private File
Inherited from jakarta.servlet.http.HttpServlet:
doTracegetLastModifiedinitservice