Tomcat static resources

Config

$CATALINA_HOME\conf\web.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

When param listings is true, / can list all files

1
2
3
4
5
<!-- The mapping for the default servlet -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

The pattern mapping is this, for all no mapping servlet-mapping request.

Process

1
2
3
4
5
6
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Serve the requested resource, including the data content
serveResource(request, response, true, fileEncoding);
}
  1. judge resource existence
  2. judge file readable
  3. set response head content-type base on file type
  4. judge file time
  5. set response timeout
  6. stream operation

tomcat