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 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.
Modifier and Type | Class and Description |
---|---|
protected static class |
DefaultServlet.CompressionFormat |
protected static class |
DefaultServlet.Range |
Modifier and Type | Field and Description |
---|---|
protected static 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.
|
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 WebResourceRoot |
resources
The complete set of web application resources
|
protected static String |
RESOURCES_JNDI_NAME
Deprecated.
Unused. Will be removed in Tomcat 9.
|
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 boolean |
sortListings
Flag to determine if resources should be sorted.
|
protected org.apache.catalina.servlets.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?
|
Constructor and Description |
---|
DefaultServlet() |
Modifier and Type | Method and Description |
---|---|
protected boolean |
checkIfHeaders(HttpServletRequest request,
HttpServletResponse response,
WebResource resource)
Check if the conditions specified in the optional If headers are
satisfied.
|
protected boolean |
checkIfMatch(HttpServletRequest request,
HttpServletResponse response,
WebResource resource)
Check if the if-match condition is satisfied.
|
protected boolean |
checkIfModifiedSince(HttpServletRequest request,
HttpServletResponse response,
WebResource resource)
Check if the if-modified-since condition is satisfied.
|
protected boolean |
checkIfNoneMatch(HttpServletRequest request,
HttpServletResponse response,
WebResource resource)
Check if the if-none-match condition is satisfied.
|
protected boolean |
checkIfUnmodifiedSince(HttpServletRequest request,
HttpServletResponse response,
WebResource resource)
Check if the if-unmodified-since condition is satisfied.
|
protected boolean |
checkSendfile(HttpServletRequest request,
HttpServletResponse response,
WebResource resource,
long length,
DefaultServlet.Range range)
Check if sendfile can be used.
|
protected void |
copy(InputStream is,
PrintWriter writer,
String 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(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(WebResource resource,
InputStream is,
PrintWriter writer,
String encoding)
Deprecated.
Unused. This will be removed in Tomcat 9.
Use
copy(InputStream, PrintWriter, String) |
protected void |
copy(WebResource resource,
InputStream is,
ServletOutputStream ostream)
Deprecated.
Unused. This will be removed in Tomcat 9.
Use
copy(InputStream, ServletOutputStream) |
protected void |
copy(WebResource resource,
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(WebResource resource,
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 String |
determineMethodsAllowed(HttpServletRequest req) |
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 |
findXsltSource(WebResource directory)
Return a Source for the xsl template (if possible).
|
protected String |
generateETag(WebResource resource)
Provides the entity tag (the ETag header) for the given resource.
|
protected String |
getPathPrefix(HttpServletRequest request)
Determines the appropriate path to prepend resources with
when generating directory listings.
|
protected String |
getReadme(WebResource directory)
Deprecated.
Unused. Will be removed in Tomcat 9
|
protected String |
getReadme(WebResource directory,
String encoding)
Get the readme file as a string.
|
protected String |
getRelativePath(HttpServletRequest request)
Return the relative path associated with this servlet.
|
protected String |
getRelativePath(HttpServletRequest request,
boolean allowEmptyPath) |
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,
WebResource resource)
Parse the range header.
|
protected InputStream |
render(HttpServletRequest request,
String contextPath,
WebResource resource,
String encoding)
Decide which way to render.
|
protected InputStream |
render(String contextPath,
WebResource resource)
Deprecated.
Unused. Will be removed in Tomcat 9
|
protected InputStream |
render(String contextPath,
WebResource resource,
String encoding)
Deprecated.
|
protected InputStream |
renderHtml(HttpServletRequest request,
String contextPath,
WebResource resource,
String encoding)
Return an InputStream to an HTML representation of the contents of this
directory.
|
protected InputStream |
renderHtml(String contextPath,
WebResource resource)
Deprecated.
Unused. Will be removed in Tomcat 9
|
protected InputStream |
renderHtml(String contextPath,
WebResource resource,
String encoding)
Deprecated.
Unused. Will be removed in Tomcat 10
|
protected String |
renderSize(long size)
Render the specified file size (in bytes).
|
protected InputStream |
renderXml(HttpServletRequest request,
String contextPath,
WebResource resource,
Source xsltSource,
String encoding)
Return an InputStream to an XML representation of the contents this
directory.
|
protected InputStream |
renderXml(String contextPath,
WebResource resource,
Source xsltSource)
Deprecated.
Unused. Will be removed in Tomcat 9
|
protected InputStream |
renderXml(String contextPath,
WebResource resource,
Source xsltSource,
String encoding)
Deprecated.
Unused. Will be removed in Tomcat 10
|
protected String |
rewriteUrl(String path)
URL rewriter.
|
protected void |
sendNotAllowed(HttpServletRequest req,
HttpServletResponse resp) |
protected void |
serveResource(HttpServletRequest request,
HttpServletResponse response,
boolean content,
String inputEncoding)
Serve the specified resource, optionally including the data content.
|
protected void |
service(HttpServletRequest req,
HttpServletResponse resp)
Receives standard HTTP requests from the public
service method and dispatches
them to the do Method methods defined in
this class. |
doTrace, getLastModified, service
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
protected static final StringManager sm
protected static final ArrayList<DefaultServlet.Range> FULL
protected static final String mimeSeparation
@Deprecated protected static final String RESOURCES_JNDI_NAME
protected static final int BUFFER_SIZE
protected int debug
protected int input
protected boolean listings
protected boolean readOnly
protected DefaultServlet.CompressionFormat[] compressionFormats
protected int output
protected String localXsltFile
protected String contextXsltFile
protected String globalXsltFile
protected String readmeFile
protected transient WebResourceRoot resources
protected String fileEncoding
protected int sendfileSize
protected boolean useAcceptRanges
protected boolean showServerInfo
protected boolean sortListings
protected transient org.apache.catalina.servlets.DefaultServlet.SortManager sortManager
public void destroy()
destroy
in interface Servlet
destroy
in class GenericServlet
public void init() throws ServletException
init
in class GenericServlet
ServletException
- if an exception occurs that interrupts the servlet's
normal operationprotected String getRelativePath(HttpServletRequest request)
request
- The servlet request we are processingprotected String getRelativePath(HttpServletRequest request, boolean allowEmptyPath)
protected String getPathPrefix(HttpServletRequest request)
getRelativePath(HttpServletRequest)
this will change.request
- the request to determine the path forprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
javax.servlet.http.HttpServlet
service
method and dispatches
them to the do
Method methods defined in
this class. This method is an HTTP-specific version of the
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
method. There's no
need to override this method.service
in class HttpServlet
req
- the HttpServletRequest
object that
contains the request the client made of
the servletresp
- the HttpServletResponse
object that
contains the response the servlet returns
to the clientServletException
- if the HTTP request
cannot be handledIOException
- if an input or output error occurs
while the servlet is handling the
HTTP requestServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
doGet
in class HttpServlet
request
- The servlet request we are processingresponse
- The servlet response we are creatingIOException
- if an input/output error occursServletException
- if a servlet-specified error occursServletResponse.setContentType(java.lang.String)
protected void doHead(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
doHead
in class HttpServlet
request
- The servlet request we are processingresponse
- The servlet response we are creatingIOException
- if an input/output error occursServletException
- if a servlet-specified error occursprotected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
doOptions
in class HttpServlet
req
- the HttpServletRequest
object that
contains the request the client made of
the servletresp
- the HttpServletResponse
object that
contains the response the servlet returns
to the clientIOException
- if an input or output error occurs
while the servlet is handling the
OPTIONS requestServletException
- if the request for the
OPTIONS cannot be handledprotected String determineMethodsAllowed(HttpServletRequest req)
protected void sendNotAllowed(HttpServletRequest req, HttpServletResponse resp) throws IOException
IOException
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
doPost
in class HttpServlet
request
- The servlet request we are processingresponse
- The servlet response we are creatingIOException
- if an input/output error occursServletException
- if a servlet-specified error occursServletOutputStream
,
ServletResponse.setContentType(java.lang.String)
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
doPut
in class HttpServlet
req
- The servlet request we are processingresp
- The servlet response we are creatingIOException
- if an input/output error occursServletException
- if a servlet-specified error occursprotected File executePartialPut(HttpServletRequest req, DefaultServlet.Range range, String path) throws IOException
req
- The Servlet requestrange
- The range that will be writtenpath
- The pathIOException
- an IO error occurredprotected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
doDelete
in class HttpServlet
req
- The servlet request we are processingresp
- The servlet response we are creatingIOException
- if an input/output error occursServletException
- if a servlet-specified error occursprotected boolean checkIfHeaders(HttpServletRequest request, HttpServletResponse response, WebResource resource) throws IOException
request
- The servlet request we are processingresponse
- The servlet response we are creatingresource
- The resourcetrue
if the resource meets all the specified
conditions, and false
if any of the conditions is not
satisfied, in which case request processing is stoppedIOException
- an IO error occurredprotected String rewriteUrl(String path)
path
- Path which has to be rewrittenprotected void serveResource(HttpServletRequest request, HttpServletResponse response, boolean content, String inputEncoding) throws IOException, ServletException
request
- The servlet request we are processingresponse
- The servlet response we are creatingcontent
- Should the content be included?inputEncoding
- The encoding to use if it is necessary to access the
source as characters rather than as bytesIOException
- if an input/output error occursServletException
- if a servlet-specified error occursprotected DefaultServlet.Range parseContentRange(HttpServletRequest request, HttpServletResponse response) throws IOException
request
- The servlet request we are processingresponse
- The servlet response we are creatingnull
if the content-range
header was invalid or #IGNORE
if there is no header to
processIOException
- an IO error occurredprotected ArrayList<DefaultServlet.Range> parseRange(HttpServletRequest request, HttpServletResponse response, WebResource resource) throws IOException
request
- The servlet request we are processingresponse
- The servlet response we are creatingresource
- The resourcenull
if the range header was invalid or
#FULL
if the Range header should be ignored.IOException
- an IO error occurred@Deprecated protected InputStream render(String contextPath, WebResource resource) throws IOException, ServletException
contextPath
- The pathresource
- The resourceIOException
- an IO error occurredServletException
- rendering error@Deprecated protected InputStream render(String contextPath, WebResource resource, String encoding) throws IOException, ServletException
render(HttpServletRequest, String, WebResource, String)
insteadcontextPath
- The pathresource
- The resourceencoding
- The encoding to use to process the readme (if any)IOException
- an IO error occurredServletException
- rendering errorprotected InputStream render(HttpServletRequest request, String contextPath, WebResource resource, String encoding) throws IOException, ServletException
request
- The HttpServletRequest being servedcontextPath
- The pathresource
- The resourceencoding
- The encoding to use to process the readme (if any)IOException
- an IO error occurredServletException
- rendering error@Deprecated protected InputStream renderXml(String contextPath, WebResource resource, Source xsltSource) throws IOException, ServletException
contextPath
- Context path to which our internal paths are relativeresource
- The associated resourcexsltSource
- The XSL stylesheetIOException
- an IO error occurredServletException
- rendering error@Deprecated protected InputStream renderXml(String contextPath, WebResource resource, Source xsltSource, String encoding) throws ServletException, IOException
contextPath
- Context path to which our internal paths are relativeresource
- The associated resourcexsltSource
- The XSL stylesheetencoding
- The encoding to use to process the readme (if any)IOException
- an IO error occurredServletException
- rendering errorprotected InputStream renderXml(HttpServletRequest request, String contextPath, WebResource resource, Source xsltSource, String encoding) throws IOException, ServletException
request
- The HttpServletRequest being servedcontextPath
- Context path to which our internal paths are relativeresource
- The associated resourcexsltSource
- The XSL stylesheetencoding
- The encoding to use to process the readme (if any)IOException
- an IO error occurredServletException
- rendering error@Deprecated protected InputStream renderHtml(String contextPath, WebResource resource) throws IOException
contextPath
- Context path to which our internal paths are relativeresource
- The associated resourceIOException
- an IO error occurred@Deprecated protected InputStream renderHtml(String contextPath, WebResource resource, String encoding) throws IOException
contextPath
- Context path to which our internal paths are relativeresource
- The associated resourceencoding
- The encoding to use to process the readme (if any)IOException
- an IO error occurredprotected InputStream renderHtml(HttpServletRequest request, String contextPath, WebResource resource, String encoding) throws IOException
request
- The HttpServletRequest being servedcontextPath
- Context path to which our internal paths are relativeresource
- The associated resourceencoding
- The encoding to use to process the readme (if any)IOException
- an IO error occurredprotected String renderSize(long size)
size
- File size (in bytes)@Deprecated protected String getReadme(WebResource directory)
directory
- The directory to searchprotected String getReadme(WebResource directory, String encoding)
directory
- The directory to searchencoding
- The readme encodingprotected Source findXsltSource(WebResource directory) throws IOException
directory
- The directory to searchIOException
- an IO error occurredprotected boolean checkSendfile(HttpServletRequest request, HttpServletResponse response, WebResource resource, long length, DefaultServlet.Range range)
request
- The Servlet requestresponse
- The Servlet responseresource
- The resourcelength
- The length which will be written (will be used only if
range is null)range
- The range that will be writtentrue
if sendfile should be used (writing is then
delegated to the endpoint)protected boolean checkIfMatch(HttpServletRequest request, HttpServletResponse response, WebResource resource) throws IOException
request
- The servlet request we are processingresponse
- The servlet response we are creatingresource
- The resourcetrue
if the resource meets the specified condition,
and false
if the condition is not satisfied, in which case
request processing is stoppedIOException
- an IO error occurredprotected boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response, WebResource resource)
request
- The servlet request we are processingresponse
- The servlet response we are creatingresource
- The resourcetrue
if the resource meets the specified condition,
and false
if the condition is not satisfied, in which case
request processing is stoppedprotected boolean checkIfNoneMatch(HttpServletRequest request, HttpServletResponse response, WebResource resource) throws IOException
request
- The servlet request we are processingresponse
- The servlet response we are creatingresource
- The resourcetrue
if the resource meets the specified condition,
and false
if the condition is not satisfied, in which case
request processing is stoppedIOException
- an IO error occurredprotected boolean checkIfUnmodifiedSince(HttpServletRequest request, HttpServletResponse response, WebResource resource) throws IOException
request
- The servlet request we are processingresponse
- The servlet response we are creatingresource
- The resourcetrue
if the resource meets the specified condition,
and false
if the condition is not satisfied, in which case
request processing is stoppedIOException
- an IO error occurredprotected String generateETag(WebResource resource)
resource
- The resource for which an entity tag is required.WebResource.getETag()
on the given
resource@Deprecated protected void copy(WebResource resource, InputStream is, ServletOutputStream ostream) throws IOException
copy(InputStream, ServletOutputStream)
resource
- The source resourceis
- The input stream to read the source resource fromostream
- The output stream to write toIOException
- if an input/output error occursprotected void copy(InputStream is, ServletOutputStream ostream) throws IOException
is
- The input stream to read the source resource fromostream
- The output stream to write toIOException
- if an input/output error occurs@Deprecated protected void copy(WebResource resource, InputStream is, PrintWriter writer, String encoding) throws IOException
copy(InputStream, PrintWriter, String)
resource
- The source resourceis
- The input stream to read the source resource fromwriter
- The writer to write toencoding
- The encoding to use when reading the source input streamIOException
- if an input/output error occursprotected void copy(InputStream is, PrintWriter writer, String encoding) throws IOException
is
- The input stream to read the source resource fromwriter
- The writer to write toencoding
- The encoding to use when reading the source input streamIOException
- if an input/output error occursprotected void copy(WebResource resource, ServletOutputStream ostream, DefaultServlet.Range range) throws IOException
resource
- The source resourceostream
- The output stream to write torange
- Range the client wanted to retrieveIOException
- if an input/output error occursprotected void copy(WebResource resource, ServletOutputStream ostream, Iterator<DefaultServlet.Range> ranges, String contentType) throws IOException
resource
- The source resourceostream
- The output stream to write toranges
- Enumeration of the ranges the client wanted to
retrievecontentType
- Content type of the resourceIOException
- if an input/output error occursprotected IOException copyRange(InputStream istream, ServletOutputStream ostream)
istream
- The input stream to read fromostream
- The output stream to write toprotected IOException copyRange(Reader reader, PrintWriter writer)
reader
- The reader to read fromwriter
- The writer to write toprotected IOException copyRange(InputStream istream, ServletOutputStream ostream, long start, long end)
istream
- The input stream to read fromostream
- The output stream to write tostart
- Start of the range which will be copiedend
- End of the range which will be copiedCopyright © 2000-2020 Apache Software Foundation. All Rights Reserved.