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.HttpServletThe 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.jpgwill succeed while a request to/context/images/tomcat2.jpgwill 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 classDefaultServlet.Range
-
Field Summary
Fields Modifier and Type Field Description protected ArrayList<org.glassfish.grizzly.http.server.util.AlternateDocBase>alternateDocBasesAlternate doc basesprotected static intBUFFER_SIZESize of file transfer buffer in bytes.protected StringcontextXsltFileAllow customized directory listing per context.protected intdebugThe debugging detail level for this servlet.protected StringfileEncodingFile encoding to be used when reading static files.protected static ArrayList<DefaultServlet.Range>FULLFull range marker.protected StringglobalXsltFileAllow customized directory listing per instance.protected intinputThe input buffer size to use when serving resources.protected booleanlistingsShould we generate directory listings?protected StringlocalXsltFileAllow customized directory listing per directory.protected intmaxHeaderRangeItemsThe maximum number of items allowed in Range header.protected static StringmimeSeparationMIME multipart separation stringprotected intoutputThe output buffer size to use when serving resources.protected static ResourceBundlerbprotected StringreadmeFileAllow a readme file to be included.protected booleanreadOnlyRead only flag.protected ProxyDirContextresourcesProxy directory context.protected static StringRESOURCES_JNDI_NAMEJNDI resources name.protected intsendfileSizeMinimum size for sendfile usage in bytes.protected org.apache.catalina.servlets.DefaultServlet.SortedBysortedByThe sorting mechanism for directory listingsprotected static URLEncoderurlEncoderArray containing the safe characters set.protected booleanuseAcceptRangesShould 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 booleancheckIfHeaders(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 booleancheckIfMatch(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)Check if the if-match condition is satisfied.protected booleancheckIfModifiedSince(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)Check if the if-modified-since condition is satisfied.protected booleancheckIfNoneMatch(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)Check if the if-none-match condition is satisfied.protected booleancheckIfUnmodifiedSince(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)Check if the if-unmodified-since condition is satisfied.protected booleancheckSendfile(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, CacheEntry entry, long length, DefaultServlet.Range range)Check if sendfile can be used.protected voidcopy(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 voidcopy(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 voidcopy(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 voidcopy(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 voidcopy(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 voidcopy(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 IOExceptioncopyRange(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 IOExceptioncopyRange(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 IOExceptioncopyRange(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 IOExceptioncopyRange(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).voiddestroy()Finalize this servlet.protected voiddisplaySize(StringBuilder buf, int filesize)Display the size of a file.protected voiddoDelete(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)Process a POST request for the specified resource.protected voiddoGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)Process a GET request for the specified resource.protected voiddoHead(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)Process a HEAD request for the specified resource.protected voiddoPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)Process a POST request for the specified resource.protected voiddoPut(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)Process a POST request for the specified resource.protected FileexecutePartialPut(jakarta.servlet.http.HttpServletRequest req, DefaultServlet.Range range, String path)Handle a partial PUT.protected SourcefindXsltInputStream(DirContext directory)Return a Source for the xsl template (if possible)protected StringgetReadme(DirContext directory)Get the readme file as a string.protected StringgetRelativePath(jakarta.servlet.http.HttpServletRequest request)Return the relative path associated with this servlet.voidinit()Initialize this servlet.booleanisListings()Return if directory listings are enabledprotected DefaultServlet.RangeparseContentRange(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 InputStreamrender(String contextPath, CacheEntry cacheEntry)Decide which way to render.protected InputStreamrenderHtml(String contextPath, CacheEntry cacheEntry)Return an InputStream to an HTML representation of the contents of this directory.protected StringrenderSize(long size)Render the specified file size (in bytes).protected InputStreamrenderXml(String contextPath, CacheEntry cacheEntry, Source xsltSource)Return an InputStream to an HTML representation of the contents of this directory.protected StringrewriteUrl(String path)URL rewriter.protected voidserveResource(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, boolean content)Serve the specified resource, optionally including the data content.voidsetListings(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:
destroyin interfacejakarta.servlet.Servlet- Overrides:
destroyin classjakarta.servlet.GenericServlet
-
init
public void init() throws jakarta.servlet.ServletExceptionInitialize this servlet.- Overrides:
initin 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.ServletExceptionProcess a GET request for the specified resource.- Overrides:
doGetin 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.ServletExceptionProcess a HEAD request for the specified resource.- Overrides:
doHeadin 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.ServletExceptionProcess a POST request for the specified resource.- Overrides:
doPostin 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, IOExceptionProcess a POST request for the specified resource.- Overrides:
doPutin 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, IOExceptionProcess a POST request for the specified resource.- Overrides:
doDeletein 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 IOExceptionCheck 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.ServletExceptionServe 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:
IOExceptionjakarta.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:
IOExceptionjakarta.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:
IOExceptionjakarta.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:
IOExceptionjakarta.servlet.ServletException
-
findXsltInputStream
protected Source findXsltInputStream(DirContext directory) throws IOException, jakarta.servlet.ServletException
Return a Source for the xsl template (if possible)- Throws:
IOExceptionjakarta.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 IOExceptionCheck 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 IOExceptionCheck 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 IOExceptionCheck 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 IOExceptionCheck 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
-
-