1.引言
首先它既然是web.xml文件,要么必然配置了些什么。根据文件中的注释
This document defines default values for *all* web applications loaded into this instance of Tomcat. As each application is deployed,this file is processed,
followed by the "/WEB-INF/web.xml" deployment descriptor from your own applications.
可以看出它为所有webAPP配置了一些默认的初始值,并且当web应用运行时,先处理这个web.xml,在处理我们自定义的web.xml文件(所以可能会有配置覆盖现象出现)。
同时在此文件中指出对于每一个webapp的resource资源都应该在它们自己的web.xml文件中配置。因为这个xml文件对所有服务器中的web应用生效。
WARNING: Do not configure application-specific resources here!
They should go in the "/WEB-INF/web.xml" file in your application.
2.文件内容
接下来看看这个xml文件到底配置了些什么。
- Servlet Definitions
- Servlet Mappings
- Filter Definitions(默认不配置)
- Built In Filter Mappings(默认不配置)
- Session Configuration
- MIME Type Mappings
- Welcome File List
1.Welcome File List
先从简单的说起,这个web.xml为所有的web应用配置了三个默认的初始页面
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
这是默认的,由于tomcat处理完这个配置文件后,会在处理我们项目中的配置文件,所以我们可以在自己的配置文件中对其进行覆盖
2.Session Configuration
这个配置针对session,session的时间默认30分钟
<session-config>
<session-timeout>30</session-timeout>
</session-config>
3. MIME Type Mappings
这个模块配置定义一些MIME类型。在提供静态资源时,Tomcat 会根据资源的文件扩展名自动生成一个“Content-Type“标头 ,基于这些映射。
<mime-mapping>
<extension>7z</extension>
<mime-type>application/x-7z-compressed</mime-type>
</mime-mapping>
4. Servlet Definitions&Servlet Mappings
这部分主要用于配置Servlet
- 第一个Servlet用来处理所有静态资源的请求。
The default servlet for all web applications, that serves static resources
并且如果有一个请求,它没有对应servlet来处理,就会被该Servlet处理。
It processes all requests that are not mapped to other
servlets with servlet mappings
文档中的【other servlet】指在tomcat自身与项目中配置的servlet。
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<!--Debugging detail level for messages logged by this servlet -->
<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>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- 第二个servlet是tomcat用来编译与执行jsp页面的。
The JSP page compiler and execution servlet, which is the mechanism
used by Tomcat to support JSP pages. Traditionally, this servlet
is mapped to the URL pattern "*.jsp".
并且文档指出这个servlet通常被映射到*.jsp的路径
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
总结:写这篇博客的初衷是因为学习springMVC,前端控制器的映射路径问题,顺便更好的了解下tomcat,目前看来目的还是达到了。