Package org.apache.catalina.servlets
Class DefaultServlet
- java.lang.Object
-
- jakarta.servlet.GenericServlet
-
- jakarta.servlet.http.HttpServlet
-
- org.apache.catalina.servlets.DefaultServlet
-
- All Implemented Interfaces:
jakarta.servlet.Servlet
,jakarta.servlet.ServletConfig
,Serializable
- Direct Known Subclasses:
WebdavServlet
public class DefaultServlet extends jakarta.servlet.http.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.- Version:
- $Revision: 1.16 $ $Date: 2007/06/06 16:01:12 $
- Author:
- Craig R. McClanahan, Remy Maucherat
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
DefaultServlet.Range
-
Field Summary
Fields Modifier and Type Field Description protected ArrayList<org.glassfish.grizzly.http.server.util.AlternateDocBase>
alternateDocBases
Alternate doc basesprotected 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 int
maxHeaderRangeItems
The maximum number of items allowed in Range header.protected static String
mimeSeparation
MIME multipart separation stringprotected int
output
The output buffer size to use when serving resources.protected static ResourceBundle
rb
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 org.apache.catalina.servlets.DefaultServlet.SortedBy
sortedBy
The sorting mechanism for directory listingsprotected 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 Description DefaultServlet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
checkIfHeaders(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the conditions specified in the optional If headers are satisfied.protected boolean
checkIfMatch(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-match condition is satisfied.protected boolean
checkIfModifiedSince(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-modified-since condition is satisfied.protected boolean
checkIfNoneMatch(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-none-match condition is satisfied.protected boolean
checkIfUnmodifiedSince(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-unmodified-since condition is satisfied.protected boolean
checkSendfile(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, CacheEntry entry, long length, DefaultServlet.Range range)
Check if sendfile can be used.protected void
copy(CacheEntry cacheEntry, jakarta.servlet.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 void
copy(CacheEntry cacheEntry, jakarta.servlet.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, InputStream is, jakarta.servlet.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, 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, PrintWriter writer, 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 void
copy(CacheEntry cacheEntry, PrintWriter writer, 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 IOException
copyRange(InputStream istream, jakarta.servlet.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, jakarta.servlet.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).protected IOException
copyRange(Reader reader, PrintWriter writer, 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).void
destroy()
Finalize this servlet.protected void
displaySize(StringBuilder buf, int filesize)
Display the size of a file.protected void
doDelete(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)
Process a POST request for the specified resource.protected void
doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
Process a GET request for the specified resource.protected void
doHead(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
Process a HEAD request for the specified resource.protected void
doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
Process a POST request for the specified resource.protected void
doPut(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)
Process a POST request for the specified resource.protected File
executePartialPut(jakarta.servlet.http.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
getReadme(DirContext directory)
Get the readme file as a string.protected String
getRelativePath(jakarta.servlet.http.HttpServletRequest request)
Return the relative path associated with this servlet.void
init()
Initialize this servlet.boolean
isListings()
Return if directory listings are enabledprotected DefaultServlet.Range
parseContentRange(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
Parse the content-range header.protected ArrayList<DefaultServlet.Range>
parseRange(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, boolean content)
Serve the specified resource, optionally including the data content.void
setListings(boolean listings)
Enables or disables directory listings for this DefaultServlet.-
Methods inherited from class jakarta.servlet.http.HttpServlet
doOptions, doTrace, getLastModified, init, service, service
-
-
-
-
Field Detail
-
rb
protected static final ResourceBundle rb
-
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 volatile boolean listings
Should we generate directory listings?
-
sortedBy
protected org.apache.catalina.servlets.DefaultServlet.SortedBy sortedBy
The sorting mechanism for 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.
-
alternateDocBases
protected transient ArrayList<org.glassfish.grizzly.http.server.util.AlternateDocBase> alternateDocBases
Alternate doc bases
-
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.
-
maxHeaderRangeItems
protected int maxHeaderRangeItems
The maximum number of items allowed in Range header. -1 means unbounded.
-
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
-
BUFFER_SIZE
protected static final int BUFFER_SIZE
Size of file transfer buffer in bytes.- See Also:
- Constant Field Values
-
-
Method Detail
-
destroy
public void destroy()
Finalize this servlet.- Specified by:
destroy
in interfacejakarta.servlet.Servlet
- Overrides:
destroy
in classjakarta.servlet.GenericServlet
-
init
public void init() throws jakarta.servlet.ServletException
Initialize this servlet.- Overrides:
init
in classjakarta.servlet.GenericServlet
- Throws:
jakarta.servlet.ServletException
-
isListings
public boolean isListings()
Return if directory listings are enabled
-
setListings
public void setListings(boolean listings)
Enables or disables directory listings for this DefaultServlet.- Parameters:
listings
- true if directory listings are to be enabled, false otherwise
-
getRelativePath
protected String getRelativePath(jakarta.servlet.http.HttpServletRequest request)
Return the relative path associated with this servlet.- Parameters:
request
- The servlet request we are processing
-
doGet
protected void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException
Process a GET request for the specified resource.- Overrides:
doGet
in classjakarta.servlet.http.HttpServlet
- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creating- Throws:
IOException
- if an input/output error occursjakarta.servlet.ServletException
- if a servlet-specified error occurs
-
doHead
protected void doHead(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException
Process a HEAD request for the specified resource.- Overrides:
doHead
in classjakarta.servlet.http.HttpServlet
- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creating- Throws:
IOException
- if an input/output error occursjakarta.servlet.ServletException
- if a servlet-specified error occurs
-
doPost
protected void doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException
Process a POST request for the specified resource.- Overrides:
doPost
in classjakarta.servlet.http.HttpServlet
- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creating- Throws:
IOException
- if an input/output error occursjakarta.servlet.ServletException
- if a servlet-specified error occurs
-
doPut
protected void doPut(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws jakarta.servlet.ServletException, IOException
Process a POST request for the specified resource.- Overrides:
doPut
in classjakarta.servlet.http.HttpServlet
- Parameters:
req
- The servlet request we are processingresp
- The servlet response we are creating- Throws:
IOException
- if an input/output error occursjakarta.servlet.ServletException
- if a servlet-specified error occurs
-
executePartialPut
protected File executePartialPut(jakarta.servlet.http.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(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws jakarta.servlet.ServletException, IOException
Process a POST request for the specified resource.- Overrides:
doDelete
in classjakarta.servlet.http.HttpServlet
- Parameters:
req
- The servlet request we are processingresp
- The servlet response we are creating- Throws:
IOException
- if an input/output error occursjakarta.servlet.ServletException
- if a servlet-specified error occurs
-
checkIfHeaders
protected boolean checkIfHeaders(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 processingresponse
- The servlet response we are creatingresourceAttributes
- 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
protected void displaySize(StringBuilder buf, int filesize)
Display the size of a file.
-
serveResource
protected void serveResource(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, boolean content) throws IOException, jakarta.servlet.ServletException
Serve the specified resource, optionally including the data content.- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creatingcontent
- Should the content be included?- Throws:
IOException
- if an input/output error occursjakarta.servlet.ServletException
- if a servlet-specified error occurs
-
parseContentRange
protected DefaultServlet.Range parseContentRange(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
Parse the content-range header.- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creating- Returns:
- Range
- Throws:
IOException
-
parseRange
protected ArrayList<DefaultServlet.Range> parseRange(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes) throws IOException
Parse the range header.- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creating- Returns:
- Vector of ranges
- Throws:
IOException
-
render
protected InputStream render(String contextPath, CacheEntry cacheEntry) throws IOException, jakarta.servlet.ServletException
Decide which way to render. HTML or XML.- Throws:
IOException
jakarta.servlet.ServletException
-
renderXml
protected InputStream renderXml(String contextPath, CacheEntry cacheEntry, Source xsltSource) throws IOException, jakarta.servlet.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
jakarta.servlet.ServletException
-
renderHtml
protected InputStream renderHtml(String contextPath, CacheEntry cacheEntry) throws IOException, jakarta.servlet.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
jakarta.servlet.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, jakarta.servlet.ServletException
Get the readme file as a string.- Throws:
IOException
jakarta.servlet.ServletException
-
findXsltInputStream
protected Source findXsltInputStream(DirContext directory) throws IOException, jakarta.servlet.ServletException
Return a Source for the xsl template (if possible)- Throws:
IOException
jakarta.servlet.ServletException
-
checkSendfile
protected boolean checkSendfile(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, CacheEntry entry, long length, DefaultServlet.Range range)
Check if sendfile can be used.
-
checkIfMatch
protected boolean checkIfMatch(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes) throws IOException
Check if the if-match condition is satisfied.- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creatingresourceAttributes
- 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(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes) throws IOException
Check if the if-modified-since condition is satisfied.- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creatingresourceAttributes
- 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
-
checkIfNoneMatch
protected boolean checkIfNoneMatch(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes) throws IOException
Check if the if-none-match condition is satisfied.- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creatingresourceAttributes
- 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(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes) throws IOException
Check if the if-unmodified-since condition is satisfied.- Parameters:
request
- The servlet request we are processingresponse
- The servlet response we are creatingresourceAttributes
- 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, jakarta.servlet.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 CacheEntry objectis
- The InputStreamostream
- 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 entryis
- The InputStreamwriter
- The writer to write to- Throws:
IOException
- if an input/output error occurs
-
copy
protected void copy(CacheEntry cacheEntry, jakarta.servlet.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 CacheEntry objectostream
- The output stream to write torange
- Range the client wanted to retrieve- Throws:
IOException
- if an input/output error occurs
-
copy
protected void copy(CacheEntry cacheEntry, PrintWriter writer, 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 CacheEntry objectwriter
- The writer to write torange
- Range the client wanted to retrieve- Throws:
IOException
- if an input/output error occurs
-
copy
protected void copy(CacheEntry cacheEntry, jakarta.servlet.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 CacheEntry objectostream
- The output stream to write toranges
- Enumeration of the ranges the client wanted to retrievecontentType
- Content type of the resource- Throws:
IOException
- if an input/output error occurs
-
copy
protected void copy(CacheEntry cacheEntry, PrintWriter writer, 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 CacheEntry objectwriter
- The writer to write toranges
- Enumeration of the ranges the client wanted to retrievecontentType
- Content type of the resource- Throws:
IOException
- if an input/output error occurs
-
copyRange
protected IOException copyRange(InputStream istream, jakarta.servlet.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 fromostream
- 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 fromwriter
- The writer to write to- Returns:
- Exception which occurred during processing
-
copyRange
protected IOException copyRange(InputStream istream, jakarta.servlet.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 fromostream
- The output stream to write tostart
- Start of the range which will be copiedend
- End of the range which will be copied- Returns:
- Exception which occurred during processing
-
copyRange
protected IOException copyRange(Reader reader, PrintWriter writer, 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:
reader
- The reader to read fromwriter
- The writer to write tostart
- Start of the range which will be copiedend
- End of the range which will be copied- Returns:
- Exception which occurred during processing
-
-