blob: e6465a5b9258d840837043d8532e08994848adc7 [file] [log] [blame]
Cheng Tang07b30872014-04-27 16:37:18 +08001<html><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Apache Tomcat 7 (7.0.42) - Default Servlet Reference</title><meta name="author" content="Tim Funk"><style type="text/css" media="print">
2 .noPrint {display: none;}
3 td#mainBody {width: 100%;}
4 </style><style type="text/css">
5 code {background-color:rgb(224,255,255);padding:0 0.1em;}
6 code.attributeName, code.propertyName {background-color:transparent;}
7 </style><style type="text/css">
8 .wrapped-source code { display: block; background-color: transparent; }
9 .wrapped-source div { margin: 0 0 0 1.25em; }
10 .wrapped-source p { margin: 0 0 0 1.25em; text-indent: -1.25em; }
11 </style><style type="text/css">
12 p.notice {
13 border: 1px solid rgb(255, 0, 0);
14 background-color: rgb(238, 238, 238);
15 color: rgb(0, 51, 102);
16 padding: 0.5em;
17 margin: 1em 2em 1em 1em;
18 }
19 </style></head><body bgcolor="#ffffff" text="#000000" link="#525D76" alink="#525D76" vlink="#525D76"><table border="0" width="100%" cellspacing="0"><!--PAGE HEADER--><tr><td><!--PROJECT LOGO--><a href="http://tomcat.apache.org/"><img src="./images/tomcat.gif" align="right" alt="
20 The Apache Tomcat Servlet/JSP Container
21 " border="0"></a></td><td><h1><font face="arial,helvetica,sanserif">Apache Tomcat 7</font></h1><font face="arial,helvetica,sanserif">Version 7.0.42, Jul 2 2013</font></td><td><!--APACHE LOGO--><a href="http://www.apache.org/"><img src="./images/asf-logo.gif" align="right" alt="Apache Logo" border="0"></a></td></tr></table><table border="0" width="100%" cellspacing="4"><!--HEADER SEPARATOR--><tr><td colspan="2"><hr noshade size="1"></td></tr><tr><!--LEFT SIDE NAVIGATION--><td width="20%" valign="top" nowrap class="noPrint"><p><strong>Links</strong></p><ul><li><a href="index.html">Docs Home</a></li><li><a href="http://wiki.apache.org/tomcat/FAQ">FAQ</a></li><li><a href="#comments_section">User Comments</a></li></ul><p><strong>User Guide</strong></p><ul><li><a href="introduction.html">1) Introduction</a></li><li><a href="setup.html">2) Setup</a></li><li><a href="appdev/index.html">3) First webapp</a></li><li><a href="deployer-howto.html">4) Deployer</a></li><li><a href="manager-howto.html">5) Manager</a></li><li><a href="realm-howto.html">6) Realms and AAA</a></li><li><a href="security-manager-howto.html">7) Security Manager</a></li><li><a href="jndi-resources-howto.html">8) JNDI Resources</a></li><li><a href="jndi-datasource-examples-howto.html">9) JDBC DataSources</a></li><li><a href="class-loader-howto.html">10) Classloading</a></li><li><a href="jasper-howto.html">11) JSPs</a></li><li><a href="ssl-howto.html">12) SSL</a></li><li><a href="ssi-howto.html">13) SSI</a></li><li><a href="cgi-howto.html">14) CGI</a></li><li><a href="proxy-howto.html">15) Proxy Support</a></li><li><a href="mbeans-descriptor-howto.html">16) MBean Descriptor</a></li><li><a href="default-servlet.html">17) Default Servlet</a></li><li><a href="cluster-howto.html">18) Clustering</a></li><li><a href="balancer-howto.html">19) Load Balancer</a></li><li><a href="connectors.html">20) Connectors</a></li><li><a href="monitoring.html">21) Monitoring and Management</a></li><li><a href="logging.html">22) Logging</a></li><li><a href="apr.html">23) APR/Native</a></li><li><a href="virtual-hosting-howto.html">24) Virtual Hosting</a></li><li><a href="aio.html">25) Advanced IO</a></li><li><a href="extras.html">26) Additional Components</a></li><li><a href="maven-jars.html">27) Mavenized</a></li><li><a href="security-howto.html">28) Security Considerations</a></li><li><a href="windows-service-howto.html">29) Windows Service</a></li><li><a href="windows-auth-howto.html">30) Windows Authentication</a></li><li><a href="jdbc-pool.html">31) Tomcat's JDBC Pool</a></li><li><a href="web-socket-howto.html">32) WebSocket</a></li></ul><p><strong>Reference</strong></p><ul><li><a href="RELEASE-NOTES.txt">Release Notes</a></li><li><a href="config/index.html">Configuration</a></li><li><a href="api/index.html">Tomcat Javadocs</a></li><li><a href="servletapi/index.html">Servlet Javadocs</a></li><li><a href="jspapi/index.html">JSP 2.2 Javadocs</a></li><li><a href="elapi/index.html">EL 2.2 Javadocs</a></li><li><a href="http://tomcat.apache.org/connectors-doc/">JK 1.2 Documentation</a></li></ul><p><strong>Apache Tomcat Development</strong></p><ul><li><a href="building.html">Building</a></li><li><a href="changelog.html">Changelog</a></li><li><a href="http://wiki.apache.org/tomcat/TomcatVersions">Status</a></li><li><a href="developers.html">Developers</a></li><li><a href="architecture/index.html">Architecture</a></li><li><a href="funcspecs/index.html">Functional Specs.</a></li><li><a href="tribes/introduction.html">Tribes</a></li></ul></td><!--RIGHT SIDE MAIN BODY--><td width="80%" valign="top" align="left" id="mainBody"><h1>Default Servlet Reference</h1><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Table of Contents"><!--()--></a><a name="Table_of_Contents"><strong>Table of Contents</strong></a></font></td></tr><tr><td><blockquote>
22<ul><li><a href="#what">What is the DefaultServlet</a></li><li><a href="#where">Where is it declared?</a></li><li><a href="#change">What can I change?</a></li><li><a href="#dir">How do I customize directory listings?</a></li><li><a href="#secure">How do I secure directory listings?</a></li></ul>
23</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="what"><strong>What is the DefaultServlet</strong></a></font></td></tr><tr><td><blockquote>
24The default servlet is the servlet which serves static resources as well
25as serves the directory listings (if directory listings are enabled).
26
27</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="where"><strong>Where is it declared?</strong></a></font></td></tr><tr><td><blockquote>
28It is declared globally in <i>$CATALINA_BASE/conf/web.xml</i>.
29By default here is it's declaration:
30<div align="left"><table cellspacing="4" cellpadding="0" border="0"><tr><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr><tr><td bgcolor="#023264" width="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#ffffff" height="1"><pre>
31 &lt;servlet&gt;
32 &lt;servlet-name&gt;default&lt;/servlet-name&gt;
33 &lt;servlet-class&gt;
34 org.apache.catalina.servlets.DefaultServlet
35 &lt;/servlet-class&gt;
36 &lt;init-param&gt;
37 &lt;param-name&gt;debug&lt;/param-name&gt;
38 &lt;param-value&gt;0&lt;/param-value&gt;
39 &lt;/init-param&gt;
40 &lt;init-param&gt;
41 &lt;param-name&gt;listings&lt;/param-name&gt;
42 &lt;param-value&gt;false&lt;/param-value&gt;
43 &lt;/init-param&gt;
44 &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
45 &lt;/servlet&gt;
46
47...
48
49 &lt;servlet-mapping&gt;
50 &lt;servlet-name&gt;default&lt;/servlet-name&gt;
51 &lt;url-pattern&gt;/&lt;/url-pattern&gt;
52 &lt;/servlet-mapping&gt;
53
54</pre></td><td bgcolor="#023264" width="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr><tr><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr></table></div>
55
56So by default, the default servlet is loaded at webapp startup and
57directory listings are disabled and debugging is turned off.
58</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="change"><strong>What can I change?</strong></a></font></td></tr><tr><td><blockquote>
59The DefaultServlet allows the following initParamters:
60
61<table border="1">
62 <tr>
63 <th valign="top">debug</th>
64 <td valign="top">
65 Debugging level. It is not very useful unless you are a tomcat
66 developer. As
67 of this writing, useful values are 0, 1, 11, 1000. [0]
68 </td>
69 </tr>
70 <tr>
71 <th valign="top">listings</th>
72 <td valign="top">
73 If no welcome file is present, can a directory listing be
74 shown?
75 value may be <b>true</b> or <b>false</b> [false]
76 <br>
77 Welcome files are part of the servlet api.
78 <br>
79 <b>WARNING:</b> Listings of directories containing many entries are
80 expensive. Multiple requests for large directory listings can consume
81 significant proportions of server resources.
82 </td>
83 </tr>
84 <tr>
85 <th valign="top">readmeFile</th>
86 <td valign="top">
87 If a directory listing is presented, a readme file may also
88 be presented with the listing. This file is inserted as is
89 so it may contain HTML.
90 </td>
91 </tr>
92 <tr>
93 <th valign="top">globalXsltFile</th>
94 <td valign="top">
95 If you wish to customize your directory listing, you
96 can use an XSL transformation. This value is an absolute
97 file name which be used for all directory listings.
98 This can be overridden per context and/or per directory. See
99 <strong>contextXsltFile</strong> and <strong>localXsltFile</strong>
100 below. The format of the xml is shown below.
101 </td>
102 </tr>
103 <tr>
104 <th valign="top">contextXsltFile</th>
105 <td valign="top">
106 You may also customize your directory listing by context by
107 configuring <code>contextXsltFile</code>. This should be a context
108 relative path (e.g.: <code>/path/to/context.xslt</code>). This
109 overrides <code>globalXsltFile</code>. If this value is present but a
110 file does not exist, then <code>globalXsltFile</code> will be used. If
111 <code>globalXsltFile</code> does not exist, then the default
112 directory listing will be shown.
113 </td>
114 </tr>
115 <tr>
116 <th valign="top">localXsltFile</th>
117 <td valign="top">
118 You may also customize your directory listing by directory by
119 configuring <code>localXsltFile</code>. This should be a relative
120 file name in the directory where the listing will take place.
121 This overrides <code>globalXsltFile</code> and
122 <code>contextXsltFile</code>. If this value is present but a file
123 does not exist, then <code>contextXsltFile</code> will be used. If
124 <code>contextXsltFile</code> does not exist, then
125 <code>globalXsltFile</code> will be used. If
126 <code>globalXsltFile</code> does not exist, then the default
127 directory listing will be shown.
128 </td>
129 </tr>
130 <tr>
131 <th valign="top">input</th>
132 <td valign="top">
133 Input buffer size (in bytes) when reading
134 resources to be served. [2048]
135 </td>
136 </tr>
137 <tr>
138 <th valign="top">output</th>
139 <td valign="top">
140 Output buffer size (in bytes) when writing
141 resources to be served. [2048]
142 </td>
143 </tr>
144 <tr>
145 <th valign="top">readonly</th>
146 <td valign="top">
147 Is this context "read only", so HTTP commands like PUT and
148 DELETE are rejected? [true]
149 </td>
150 </tr>
151 <tr>
152 <th valign="top">fileEncoding</th>
153 <td valign="top">
154 File encoding to be used when reading static resources.
155 [platform default]
156 </td>
157 </tr>
158 <tr>
159 <th valign="top">sendfileSize</th>
160 <td valign="top">
161 If the connector used supports sendfile, this represents the minimal
162 file size in KB for which sendfile will be used. Use a negative value
163 to always disable sendfile. [48]
164 </td>
165 </tr>
166 <tr>
167 <th valign="top">useAcceptRanges</th>
168 <td valign="top">
169 If true, the Accept-Ranges header will be set when appropriate for the
170 response. [true]
171 </td>
172 </tr>
173
174</table>
175</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="dir"><strong>How do I customize directory listings?</strong></a></font></td></tr><tr><td><blockquote>
176<p>You can override DefaultServlet with you own implementation and use that
177in your web.xml declaration. If you
178can understand what was just said, we will assume you can read the code
179to DefaultServlet servlet and make the appropriate adjustments. (If not,
180then that method isn't for you)
181</p>
182<p>
183You can use either <code>localXsltFile</code> or
184<code>globalXsltFile</code> and DefaultServlet will create
185an xml document and run it through an xsl transformation based
186on the values provided in <code>localXsltFile</code> and
187<code>globalXsltFile</code>. <code>localXsltFile</code> is first
188checked, followed by <code>globalXsltFile</code>, then default
189behaviors takes place.
190</p>
191
192<p>
193Format:
194<div align="left"><table cellspacing="4" cellpadding="0" border="0"><tr><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr><tr><td bgcolor="#023264" width="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#ffffff" height="1"><pre>
195 &lt;listing&gt;
196 &lt;entries&gt;
197 &lt;entry type='file|dir' urlPath='aPath' size='###' date='gmt date'&gt;
198 fileName1
199 &lt;/entry&gt;
200 &lt;entry type='file|dir' urlPath='aPath' size='###' date='gmt date'&gt;
201 fileName2
202 &lt;/entry&gt;
203 ...
204 &lt;/entries&gt;
205 &lt;readme&gt;&lt;/readme&gt;
206 &lt;/listing&gt;
207</pre></td><td bgcolor="#023264" width="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr><tr><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr></table></div>
208<ul>
209 <li>size will be missing if <code>type='dir'</code></li>
210 <li>Readme is a CDATA entry</li>
211</ul>
212</p>
213The following is a sample xsl file which mimics the default tomcat behavior:
214<div align="left"><table cellspacing="4" cellpadding="0" border="0"><tr><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr><tr><td bgcolor="#023264" width="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#ffffff" height="1"><pre>
215&lt;?xml version="1.0"?&gt;
216
217&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
218 version="1.0"&gt;
219
220 &lt;xsl:output method="xhtml" encoding="iso-8859-1" indent="no"/&gt;
221
222 &lt;xsl:template match="listing"&gt;
223 &lt;html&gt;
224 &lt;head&gt;
225 &lt;title&gt;
226 Sample Directory Listing For
227 &lt;xsl:value-of select="@directory"/&gt;
228 &lt;/title&gt;
229 &lt;style&gt;
230 h1{color : white;background-color : #0086b2;}
231 h3{color : white;background-color : #0086b2;}
232 body{font-family : sans-serif,Arial,Tahoma;
233 color : black;background-color : white;}
234 b{color : white;background-color : #0086b2;}
235 a{color : black;} HR{color : #0086b2;}
236 &lt;/style&gt;
237 &lt;/head&gt;
238 &lt;body&gt;
239 &lt;h1&gt;Sample Directory Listing For
240 &lt;xsl:value-of select="@directory"/&gt;
241 &lt;/h1&gt;
242 &lt;hr size="1" /&gt;
243 &lt;table cellspacing="0"
244 width="100%"
245 cellpadding="5"
246 align="center"&gt;
247 &lt;tr&gt;
248 &lt;th align="left"&gt;Filename&lt;/th&gt;
249 &lt;th align="center"&gt;Size&lt;/th&gt;
250 &lt;th align="right"&gt;Last Modified&lt;/th&gt;
251 &lt;/tr&gt;
252 &lt;xsl:apply-templates select="entries"/&gt;
253 &lt;/table&gt;
254 &lt;xsl:apply-templates select="readme"/&gt;
255 &lt;hr size="1" /&gt;
256 &lt;h3&gt;Apache Tomcat/7.0&lt;/h3&gt;
257 &lt;/body&gt;
258 &lt;/html&gt;
259 &lt;/xsl:template&gt;
260
261
262 &lt;xsl:template match="entries"&gt;
263 &lt;xsl:apply-templates select="entry"/&gt;
264 &lt;/xsl:template&gt;
265
266 &lt;xsl:template match="readme"&gt;
267 &lt;hr size="1" /&gt;
268 &lt;pre&gt;&lt;xsl:apply-templates/&gt;&lt;/pre&gt;
269 &lt;/xsl:template&gt;
270
271 &lt;xsl:template match="entry"&gt;
272 &lt;tr&gt;
273 &lt;td align="left"&gt;
274 &lt;xsl:variable name="urlPath" select="@urlPath"/&gt;
275 &lt;a href="{$urlPath}"&gt;
276 &lt;tt&gt;&lt;xsl:apply-templates/&gt;&lt;/tt&gt;
277 &lt;/a&gt;
278 &lt;/td&gt;
279 &lt;td align="right"&gt;
280 &lt;tt&gt;&lt;xsl:value-of select="@size"/&gt;&lt;/tt&gt;
281 &lt;/td&gt;
282 &lt;td align="right"&gt;
283 &lt;tt&gt;&lt;xsl:value-of select="@date"/&gt;&lt;/tt&gt;
284 &lt;/td&gt;
285 &lt;/tr&gt;
286 &lt;/xsl:template&gt;
287
288&lt;/xsl:stylesheet&gt;
289</pre></td><td bgcolor="#023264" width="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr><tr><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr></table></div>
290
291</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="secure"><strong>How do I secure directory listings?</strong></a></font></td></tr><tr><td><blockquote>
292Use web.xml in each individual webapp. See the security section of the
293Servlet specification.
294
295</blockquote></td></tr></table></td></tr><tr class="noPrint"><td width="20%" valign="top" nowrap class="noPrint"></td><td width="80%" valign="top" align="left"><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="comments_section" id="comments_section"><strong>Comments</strong></a></font></td></tr><tr><td><blockquote><p class="notice"><strong>Notice: </strong>This comments section collects your suggestions
296 on improving documentation for Apache Tomcat.<br><br>
297 If you have trouble and need help, read
298 <a href="http://tomcat.apache.org/findhelp.html">Find Help</a> page
299 and ask your question on the tomcat-users
300 <a href="http://tomcat.apache.org/lists.html">mailing list</a>.
301 Do not ask such questions here. This is not a Q&amp;A section.<br><br>
302 The Apache Comments System is explained <a href="/tomcat-7.0-doc/comments.html">here</a>.
303 Comments may be removed by our moderators if they are either
304 implemented or considered invalid/off-topic.</p><script type="text/javascript"><!--//--><![CDATA[//><!--
305 var comments_shortname = 'tomcat';
306 var comments_identifier = 'http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html';
307 (function(w, d) {
308 if (w.location.hostname.toLowerCase() == "tomcat.apache.org") {
309 d.write('<div id="comments_thread"><\/div>');
310 var s = d.createElement('script');
311 s.type = 'text/javascript';
312 s.async = true;
313 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
314 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
315 }
316 else {
317 d.write('<div id="comments_thread"><strong>Comments are disabled for this page at the moment.<\/strong><\/div>');
318 }
319 })(window, document);
320 //--><!]]></script></blockquote></td></tr></table></td></tr><!--FOOTER SEPARATOR--><tr><td colspan="2"><hr noshade size="1"></td></tr><!--PAGE FOOTER--><tr><td colspan="2"><div align="center"><font color="#525D76" size="-1"><em>
321 Copyright &copy; 1999-2013, Apache Software Foundation
322 </em></font></div></td></tr></table></body></html>