71
* The default resource-serving servlet for most web applications,
71
* <p>The default resource-serving servlet for most web applications,
72
72
* used to serve static resources such as HTML pages and images.
75
* This servlet is intended to be mapped to <em>/</em> e.g.:
78
* <servlet-mapping>
79
* <servlet-name>default</servlet-name>
80
* <url-pattern>/</url-pattern>
81
* </servlet-mapping>
83
* <p>It can be mapped to sub-paths, however in all cases resources are served
84
* from the web appplication resource root using the full path from the root
85
* of the web application context.
86
* <br/>e.g. given a web application structure:
97
* ... and a servlet mapping that maps only <code>/static/*</code> to the default servlet:
100
* <servlet-mapping>
101
* <servlet-name>default</servlet-name>
102
* <url-pattern>/static/*</url-pattern>
103
* </servlet-mapping>
106
* Then a request to <code>/context/static/images/tomcat.jpg</code> will succeed
107
* while a request to <code>/context/images/tomcat2.jpg</code> will fail.
74
109
* @author Craig R. McClanahan
75
110
* @author Remy Maucherat
76
* @version $Id: DefaultServlet.java 939518 2010-04-30 00:08:58Z kkolinko $
111
* @version $Id: DefaultServlet.java 1061395 2011-01-20 17:08:54Z markt $
79
114
public class DefaultServlet
296
331
* @param request The servlet request we are processing
298
333
protected String getRelativePath(HttpServletRequest request) {
334
// IMPORTANT: DefaultServlet can be mapped to '/' or '/path/*' but always
335
// serves resources from the web app root with context rooted paths.
336
// i.e. it can not be used to mount the web app root under a sub-path
337
// This method must construct a complete context rooted path, although
338
// subclasses can change this behaviour.
300
340
// Are we being processed by a RequestDispatcher.include()?
301
341
if (request.getAttribute(Globals.INCLUDE_REQUEST_URI_ATTR) != null) {
302
342
String result = (String) request.getAttribute(
303
343
Globals.INCLUDE_PATH_INFO_ATTR);
344
if (result == null) {
305
345
result = (String) request.getAttribute(
306
346
Globals.INCLUDE_SERVLET_PATH_ATTR);
307
if ((result == null) || (result.equals("")))
348
result = (String) request.getAttribute(
349
Globals.INCLUDE_SERVLET_PATH_ATTR) + result;
351
if ((result == null) || (result.equals(""))) {
373
* Determines the appropriate path to prepend resources with
374
* when generating directory listings. Depending on the behaviour of
375
* {@link #getRelativePath(HttpServletRequest)} this will change.
376
* @param request the request to determine the path for
377
* @return the prefix to apply to all resources in the listing.
379
protected String getPathPrefix(final HttpServletRequest request) {
380
return request.getContextPath();
326
385
* Process a GET request for the specified resource.
328
387
* @param request The servlet request we are processing
782
boolean isError = false;
784
(Integer) request.getAttribute("javax.servlet.error.status_code");
785
if (status != null) {
786
isError = status.intValue() >= HttpServletResponse.SC_BAD_REQUEST;
723
789
// Check if the conditions specified in the optional If headers are
725
791
if (cacheEntry.context == null) {
727
793
// Checking If headers
728
794
boolean included =
729
795
(request.getAttribute(Globals.INCLUDE_CONTEXT_PATH_ATTR) != null);
731
&& !checkIfHeaders(request, response, cacheEntry.attributes)) {
796
if (!included && !isError &&
797
!checkIfHeaders(request, response, cacheEntry.attributes)) {
756
822
contentType = "text/html;charset=UTF-8";
759
if (useAcceptRanges) {
760
// Accept ranges header
761
response.setHeader("Accept-Ranges", "bytes");
826
if (useAcceptRanges) {
827
// Accept ranges header
828
response.setHeader("Accept-Ranges", "bytes");
831
// Parse range specifier
832
ranges = parseRange(request, response, cacheEntry.attributes);
835
response.setHeader("ETag", cacheEntry.attributes.getETag());
837
// Last-Modified header
838
response.setHeader("Last-Modified",
839
cacheEntry.attributes.getLastModifiedHttp());
764
// Parse range specifier
765
ranges = parseRange(request, response, cacheEntry.attributes);
768
response.setHeader("ETag", cacheEntry.attributes.getETag());
770
// Last-Modified header
771
response.setHeader("Last-Modified",
772
cacheEntry.attributes.getLastModifiedHttp());
774
842
// Get content length
775
843
contentLength = cacheEntry.attributes.getContentLength();
776
844
// Special case for zero length files, which would cause a