tomcat自身web.xml文件

本文详细解读了web.xml文件的默认配置,包括welcome-file-list、session-timeout、MIMETypeMappings和ServletMappings,特别强调了如何通过自定义web.xml进行配置覆盖。核心在于理解Tomcat初始化过程与资源映射规则。
摘要由CSDN通过智能技术生成

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文件到底配置了些什么。

  1. Servlet Definitions
  2. Servlet Mappings
  3. Filter Definitions(默认不配置)
  4. Built In Filter Mappings(默认不配置)
  5. Session Configuration
  6. MIME Type Mappings
  7. 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

  1. 第一个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>
  1. 第二个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,目前看来目的还是达到了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值