web工程中web.xml元素加载顺序以及配置实例

时间:2023-03-08 17:08:54
web工程中web.xml元素加载顺序以及配置实例

简介

web.xml是web工程的配置文件,容器加载web工程时,会首先从WEB-INF中查询web.xml,并加载其中的配置信息,可以将web.xml认为是web工程的入口。

web.xml中包含有welcome欢迎页面、servlet、servlet-mapping、filter、listener和启动加载级别等。

web.xml加载顺序

  1. 容器读取工程的配置文件web.xml,读取<listener>和<context-param>两个节点。
  2. 容器创建ServletContext,所有的web项目都将共享该ServletContext。
  3. 容器将<context-param>转换为键值对,并交给ServletContext。
  4. 容器创建<listener>中的类的实例,创建监听器

web.xml内容详解

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

    id="WebApp_ID" version="3.0">

    <display-name>console</display-name>

    <!--servletContext加载的属性信息 -->

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring.xml,classpath:spring-mybatis.xml,classpath:spring-shiro.xml</param-value>

    </context-param>

    <!-- 定义过滤器的名称,以及其初始化参数-->

    <filter>

        <filter-name>shiroFilter</filter-name>

        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

        <init-param>

            <param-name>targetFilterLifecycle</param-name>

            <param-value>true</param-value>

        </init-param>

    </filter>

    <!-- 定义url被哪个filter过滤,下面的配置表示所有的请求都必须经过shiroFilter  -->

    <filter-mapping>

        <filter-name>shiroFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

    <filter>

        <filter-name>ResponseCrossFilter</filter-name>

        <filter-class>com.emcc.changedig.filter.ResponseCrossFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>ResponseCrossFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

    <!--  定义监听器,控制容器在启动和停止时的行为 -->

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <filter>

        <filter-name>encodingFilter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>

            <description>字符集编码</description>

            <param-name>encoding</param-name>

            <param-value>UTF-8</param-value>

        </init-param>

    </filter>

    <filter-mapping>

        <filter-name>encodingFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

    <!--  清理监听器,当应用停止时,使用该listener释放资源 -->

    <listener>

        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

    </listener>

    <! – 定义servlet,初始化参数以及启动顺序 -->

    <servlet>

        <description>spring mvc servlet</description>

        <servlet-name>springMvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <description>spring mvc 配置文件</description>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:spring-mvc.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet> 

        <servlet-name>DruidStatView</servlet-name> 

        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> 

        <init-param> 

            <!-- 允许清空统计数据 --> 

            <param-name>resetEnable</param-name> 

            <param-value>true</param-value> 

        </init-param> 

        <init-param> 

            <!-- 用户名 --> 

            <param-name>loginUsername</param-name> 

            <param-value>druid</param-value> 

        </init-param> 

        <init-param> 

            <!-- 密码 --> 

            <param-name>loginPassword</param-name> 

            <param-value>druid</param-value> 

        </init-param> 

    </servlet> 

    <!-- 定义servlet 映射规则 -->

    <servlet-mapping> 

        <servlet-name>DruidStatView</servlet-name> 

        <url-pattern>/druid/*</url-pattern> 

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>springMvc</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

    <session-config>

        <!-- 时间单位是分钟 -->

        <session-timeout>360</session-timeout>

    </session-config>

    <!-- default表示,对客户端请求的静态资源,交由默认的servlet来处理 -->

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.css</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.js</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.ico</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.gif</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.jpg</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.png</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.bmp</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.jpeg</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.swf</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.flv</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.xml</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.txt</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.htm</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.html</url-pattern>

    </servlet-mapping>

    <!--  定义错误返回页面  -->

    <error-page>

        <error-code>404</error-code>

        <location>/web/error/404.html</location>

    </error-page>

</web-app>