zoukankan      html  css  js  c++  java
  • web.xml 组件加载顺序

      在配置项目组件的过程中, 了解Tomcat加载组件顺序很有必要。 例如某些框架如Quartz的集群功能需要数据库的支持, 数据库的加载肯定要在框架组件加载之前。

      经过查阅和Debug发现, web.xml组件加载顺序为:context-param -> listener -> filter -> servlet(同类则按编写顺序执行)。

    web.xml常用组件解析:

    <web-app>
       
    <display-name></display-name>
    WEB应用的名字 
      
    <description></description> 
    WEB应用的描述    
    
    <context-param></context-param> 
    context-param元素声明应用范围内的初始化参数
    
     <!-- 指定spring配置文件位置 -->    
    <context-param>    
      <param-name>contextConfigLocation</param-name>    
      <param-value>    
    <!--加载多个spring配置文件 -->    
    /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml    
      </param-value>    
    </context-param>    
    
    
    <filter></filter> 
    过滤器将一个名字与一个实现javax.servlet.Filter接口的类相关联    
    
    <filter-mapping></filter-mapping> 
    一旦命名了一个过滤器,就要利用filter-mapping元素把它
    与一个或多个servlet或JSP页面相关联
    
       
    <listener></listener> 
    事件监听程序在建立、修改和删除会话或servlet环境时得到通知。
    Listener元素指出事件监听程序类。    
    
    如Log4j这个广泛使用的监听
    
    <!-- 定义SPRING监听器,加载spring -->   
    <listener>    
      <listenerclass>
        org.springframework.web.context.ContextLoaderListener
      </listener-class>    
    </listener>  
    
    
    <servlet></servlet> 
    在向servlet或JSP页面制定初始化参数或定制URL时,
    必须首先命名servlet或JSP页面。
    Servlet元素就是用来完成此项任务的。
       
    <servlet-mapping></servlet-mapping> 
    服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。
    但是,常常会更改这个URL,
    以便servlet可以访问初始化参数或更容易地处理相对URL。
    在更改缺省URL时,使用servlet-mapping元素    
    
    <session-config></session-config> 
    如果某个会话在一定时间内未被访问,
    服务器可以抛弃它以节省内存。  
    可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,
    或者可利用session-config元素制定缺省超时值    
    
    <mime-mapping></mime-mapping>
    如果Web应用具有想到特殊的文件,
    希望能保证给他们分配特定的MIME类型,
    则mime-mapping元素提供这种保证  
    
     
    <welcome-file-list></welcome-file-list> 
    指示服务器在收到引用一个目录名而不是文件名的URL时,
    使用哪个文件(其实就是欢迎界面或者说入口界面一般为index.*)   
    
    
    <error-page></error-page>
    在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,
    能够制定将要显示的页面。    
    
    
    <taglib></taglib> 
    对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。
    此功能使你能够更改TLD文件的位置,   
    而不用编辑使用这些文件的JSP页面。
    
       
    <resource-env-ref></resource-env-ref>
    声明与资源相关的一个管理对象。 
    
      
    <resource-ref></resource-ref> 
    声明一个资源工厂使用的外部资源。    
    
    
    <security-constraint></security-constraint> 
    制定应该保护的URL。它与login-config元素联合使用 
    
      
    <login-config></login-config> 
    指定服务器应该怎样给试图访问受保护页面的用户授权。
    它与sercurity-constraint元素联合使用。    
    
    
    <security-role></security-role>
    给出安全角色的一个列表,这些角色将出现在servlet元素内的
    security-role-ref元素的role-name子元素中。
    分别地声明角色可使高级IDE处理安全信息更为容易
    
    
    <env-entry></env-entry>
    声明Web应用的环境项
    
    
    </web-app>

    例如我的一个配置:

    <?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"
             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">
      <display-name>jwxt</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    
    
      <!--Spring配置-->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
      </context-param>
    
      <!-- Spring监听器 -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
    
    
      <!-- 处理POST提交乱码问题 -->
      <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
      </filter>
    
      <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>*.action</url-pattern>
      </filter-mapping>
    
    
      <!--MVC 前端控制器 -->
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 默认找 /WEB-INF/[servlet的名称]-servlet.xml -->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:SpringMVC.xml</param-value>
        </init-param>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 1. /* 拦截所有 jsp js png .css 真的全拦截 建议不使用 2. *.action *.do 拦截以do action
            结尾的请求 肯定能使用 ERP 3. / 拦截所有 (不包括jsp) (包含.js .png.css) 强烈建议使用 前台 面向消费者 www.jd.com/search
            /对静态资源放行 -->
        <url-pattern>*.action</url-pattern>
      </servlet-mapping>
    
    <!--配置多个请求的方式-->
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    
    
    
      <!-- shiro过滤器定义 -->
    <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>
      <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
    
      <!-- 404页面 -->
      <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
      </error-page>
      <!-- 500页面 -->
      <error-page>
        <error-code>500</error-code>
        <location>/500.html</location>
      </error-page>
    
    
    </web-app>

    补充:

    context-param中的param-value可以用通配符,例如:classpath:application*.xml

                    也可以用多个逗号分割多个值,如classpath:appli1.xml,classpath:app2.xml

  • 相关阅读:
    11g SPA (sql Performance Analyze) 进行升级测试
    SPA游标采集之去除重复
    C++ 实现分数的四则运算
    计算两个数的最大公约数和最小公倍数(欧几里得算法)
    计算a月的第b个星期c
    完数问题
    求整数的最大质因子
    C++ 读取文本文件内容到结构体数组中并排序
    月饼问题PAT B1020(贪心算法)
    路径打印(set以及字符串的相关操作)
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/9044844.html
Copyright © 2011-2022 走看看