> For the complete documentation index, see [llms.txt](https://jun-wang.gitbook.io/learnjava/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jun-wang.gitbook.io/learnjava/ji-shu-xue-xi/spring/web.xml-xiang-jie.md).

# web.xml详解

## 一、web.xml简介

在java工程中，web.xml用来初始化工程配置信息，比如说welcome页面，filter，listener，servlet，servlet-mapping，启动加载级别等等。每一个xml文件都有定义格式规范的schema文件，web.xml所对应的xml Schema文件中定义了多少种标签元素，web.xml中就可以出现它所定义的标签元素，也就具备哪些特定的功能。web.xml的模式文件是由Sun 公司定义的，每个web.xml文件的根元素为`<web-app>`中，必须标明这个`web.xml`使用的是哪个模式文件。

web.xml的根元素定义如下所示:

```markup
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">


</web-app>
```

## 二、web.xml标签介绍

下面就来介绍一下web.xml中常用的标签及其功能：

### 1. description：项目描述

### 2. display-name：项目名称

### 3. icon ：web图标

包含两个子元素small-icon和large-icon，指定web站台中小图标和大图标的路径

```markup
<display-name>Develop Example</display-name>  
<description>JSP 2.0 Tech Book's Examples</description> 
<icon>  
    <small-icon>/images/small.gif</small-icon>    
    <large-icon>/images/large.gir</large-icon> 
</icon>
```

### 4. context-param：初始化参数

元素含有一对参数名和参数值，用作应用的servlet上下文初始化参数。参数名在整个Web应用中必须是惟一的。包含两个子元素param-name和param-value

```markup
<context-param>
    <param-name>param_name</param-name>
    <param-value>param_value</param-value>
</context-param>
```

此所设定的参数,在JSP网页中可以使用下列方法来取得：`${initParam.param_name}`

若在Servlet可以使用下列方法来获得：`String param_name=getServletContext().getInitParamter("param_name");`

### 5. filter：过滤器

指定Web容器中的过滤器，请求和响应对象被servlet处理之前或之后，可以使用过滤器对这两个对象进行操作。包含子元素：

* init-param：与context-param 元素具有相同的作用
* filter-name：用来定义过滤器的名称，该名称在整个应用中都必须是惟一的
* filter-class：指定过滤器类的完全限定的名称

### 6. filter-mapping：映射filter

和filter一起使用，指定过滤器被映射到一个servlet或一个URL模式。和filter必须具有相同的名称。过滤是按照部署描述符的filter-mapping元素出现的顺序执行的。包含子元素：

* filter-name：定义filter名称
* url-pattern：filter所对应的url
* servlet-name：定义filter的名称
* dispatcher：定Filter对应的请求方式，有RQUEST,INCLUDE,FORWAR,ERROR四种，默认为REQUEST

```markup
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
```

### 7. servlet：处理请求响应

用于处理及响应客户端的需求，动态生成web内容。

### 8. srvlet-mapping：映射servlet

用来定义servlet所对应URL，和servlet必须具有相同的名称。

```markup
<servlet>
  <servlet-name>ServletName</servlet-name>   
  <servlet-class>xxxpackage.xxxServlet</servlet-class>   <!--Servlet的类-->
  <init-param>                                     <!--初始化一个变量，可看成全局变量，可省略-->
    <param-name>参数名称</param-name>              <!--变量名称-->
    <param-value>参数值</param-value>              <!--变量值-->
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>ServletName</servlet-name>               
  <url-pattern>/aaa/xxx</url-pattern>                   <!--映射的url路径 -->
</servlet-mapping>
```

### 9. listener：监听器

可以收到事件什么时候发生以及用什么作为响应的通知

```markup
<listener>
    <listener-class>com.foo.hello</listener-class>
</listener>
```

### 10. welcome-file-list：首页

包含一个子元素welcome-file，用来定义首页，服务器会依照设定的顺序来找首页

```markup
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
</welcome-file-list>
```

### 11. error-page： 错误网页

将错误代码(Error Code)或异常(Exception)的种类对应到web应用资源路径

```markup
<error-page>
   <error-code>404</error-code> <!-- http错误码 -->
   <location>/error404.jsp</location> <!-- 在web应用内的相关资源路径 -->
</error-page>
<error-page>
   <exception-type>java.lang.Exception</exception-type> <!-- 一个完整名称的Java异常类型 -->
   <location>/except.jsp</location>
</error-page>
```

> 参考：
>
> <https://blog.csdn.net/m751075306/article/details/9452893>
