web.xml 中的listener、 filter、servlet 載入順序及其詳解

hello_xusir發表於2016-12-01

在專案中總會遇到一些關於載入的優先順序問題,剛剛就遇到了一個問題,由於專案中使用了quartz任務排程,quartz在web.xml中是使用listener進行監聽的,使得在tomcat啟動的時候能馬上檢查資料庫檢視那些任務未被按時執行,而資料庫的配置資訊在是在web.xml中使用servlet配置的,導致tomcat啟動後在執行quartz任務時報空指標,原因就是servlet中的資料庫連線資訊未被載入。網上查詢了下web.xml中配置的載入優先順序:

        首先可以肯定的是,載入順序與它們在 web.xml 檔案中的先後順序無關。即不會因為 filter 寫在 listener 的前面而會先載入 filter。最終得出的結論是:listener -> filter -> servlet

        同時還存在著這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程式上下文資訊。我們的 listener, filter 等在初始化時會用到這些上下文中的資訊,那麼 context-param 配置節是不是應該寫在 listener 配置節前呢?實際上 context-param 配置節可寫在任意位置,因此真正的載入順序為:context-param -> listener -> filter -> servlet

        對於某類配置節而言,與它們出現的順序是有關的。以 filter 為例,web.xml 中當然可以定義多個 filter,與 filter 相關的一個配置節是 filter-mapping,這裡一定要注意,對於擁有相同 filter-name 的 filter 和 filter-mapping 配置節而言,filter-mapping 必須出現在 filter 之後,否則當解析到 filter-mapping 時,它所對應的 filter-name 還未定義。web 容器啟動時初始化每個 filter 時,是按照 filter 配置節出現的順序來初始化的,當請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping 配置節出現的順序來依次呼叫 doFilter() 方法的。

        servlet 同 filter 類似 ,此處不再贅述。

       由此,可以看出,web.xml 的載入順序是:context-param -> listener -> filter -> servlet ,而同個型別之間的實際程式呼叫的時候的順序是根據對應的 mapping 的順序進行呼叫的。

web.xml檔案詳解
========================================================================
Web.xml常用元素    
<web-app>    
<display-name></display-name>定義了WEB應用的名字    
<description></description> 宣告WEB應用的描述資訊    

<context-param></context-param> context-param元素宣告應用範圍內的初始化引數。    
<filter></filter> 過濾器元素將一個名字與一個實現javax.servlet.Filter介面的類相關聯。    
<filter-mapping></filter-mapping> 一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。    
<listener></listener>servlet API的版本2.3增加了對事件監聽程式的支援,事件監聽程式在建立、修改和刪除會話或servlet環境時得到通知。    
                     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時,使用哪個檔案。    
<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應用的環境項。    
<ejb-ref></ejb-ref>宣告一個EJB的主目錄的引用。    
< ejb-local-ref></ ejb-local-ref>宣告一個EJB的本地主目錄的應用。    
</web-app>    


相應元素配置    

1、Web應用圖示:指出IDE和GUI工具用來表示Web應用的大圖示和小圖示    
<icon>    
<small-icon>/images/app_small.gif</small-icon>    
<large-icon>/images/app_large.gif</large-icon>    
</icon>    
2、Web 應用名稱:提供GUI工具可能會用來標記這個特定的Web應用的一個名稱    
<display-name>Tomcat Example</display-name>    
3、Web 應用描述: 給出於此相關的說明性文字    
<disciption>Tomcat Example servlets and JSP pages.</disciption>    
4、上下文引數:宣告應用範圍內的初始化引數。    
<context-param>    
    <param-name>ContextParameter</para-name>    
    <param-value>test</param-value>    
    <description>It is a test parameter.</description>    
</context-param>    
在servlet裡面可以通過getServletContext().getInitParameter("context/param")得到    

5、過濾器配置:將一個名字與一個實現javaxs.servlet.Filter介面的類相關聯。    
<filter>    
        <filter-name>setCharacterEncoding</filter-name>    
        <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>    
        <init-param>    
            <param-name>encoding</param-name>    
            <param-value>GB2312</param-value>    
        </init-param>    
</filter>    
<filter-mapping>    
        <filter-name>setCharacterEncoding</filter-name>    
        <url-pattern>/*</url-pattern>    
</filter-mapping>    
6、監聽器配置    
<listener>    
      <listerner-class>listener.SessionListener</listener-class>    
</listener>    
7、Servlet配置    
   基本配置    
   <servlet>    
      <servlet-name>snoop</servlet-name>    
      <servlet-class>SnoopServlet</servlet-class>    
   </servlet>    
   <servlet-mapping>    
      <servlet-name>snoop</servlet-name>    
      <url-pattern>/snoop</url-pattern>    
   </servlet-mapping>    
   高階配置    
   <servlet>    
      <servlet-name>snoop</servlet-name>    
      <servlet-class>SnoopServlet</servlet-class>    
      <init-param>    
         <param-name>foo</param-name>    
         <param-value>bar</param-value>    
      </init-param>    
      <run-as>    
         <description>Security role for anonymous access</description>    
         <role-name>tomcat</role-name>    
      </run-as>    
   </servlet>    
   <servlet-mapping>    
      <servlet-name>snoop</servlet-name>    
      <url-pattern>/snoop</url-pattern>    
   </servlet-mapping>    
   元素說明    
     <servlet></servlet> 用來宣告一個servlet的資料,主要有以下子元素:    
     <servlet-name></servlet-name> 指定servlet的名稱    
     <servlet-class></servlet-class> 指定servlet的類名稱    
     <jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑    
     <init-param></init-param> 用來定義引數,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化引數    
     <load-on-startup></load-on-startup>指定當Web應用啟動時,裝載Servlet的次序。    
                                 當值為正數或零時:Servlet容器先載入數值小的servlet,再依次載入其他數值大的servlet.    
                                 當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時載入它    
     <servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素    
       <servlet-name></servlet-name> 指定servlet的名稱    
       <url-pattern></url-pattern> 指定servlet所對應的URL    
8、會話超時配置(單位為分鐘)    
   <session-config>    
      <session-timeout>120</session-timeout>    
   </session-config>    
9、MIME型別配置    
   <mime-mapping>    
      <extension>htm</extension>    
      <mime-type>text/html</mime-type>    
   </mime-mapping>    
10、指定歡迎檔案頁配置    
   <welcome-file-list>    
      <welcome-file>index.jsp</welcome-file>    
      <welcome-file>index.html</welcome-file>    
      <welcome-file>index.htm</welcome-file>    
   </welcome-file-list>    
11、配置錯誤頁面    
一、 通過錯誤碼來配置error-page    
   <error-page>    
      <error-code>404</error-code>    
      <location>/NotFound.jsp</location>    
   </error-page>    
上面配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp。    
二、通過異常的型別配置error-page    
   <error-page>    
       <exception-type>java.lang.NullException</exception-type>    
       <location>/error.jsp</location>    
   </error-page>    
上面配置了當系統發生java.lang.NullException(即空指標異常)時,跳轉到錯誤處理頁面error.jsp    
12、TLD配置    
   <taglib>    
       <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>    
       <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>    
   </taglib>    
   如果MyEclipse一直在報錯,應該把<taglib> 放到 <jsp-config>中    
   <jsp-config>    
      <taglib>    
          <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>    
          <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>    
      </taglib>    
   </jsp-config>    
13、資源管理物件配置    
   <resource-env-ref>    
       <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>    
   </resource-env-ref>    
14、資源工廠配置    
   <resource-ref>    
       <res-ref-name>mail/Session</res-ref-name>    
       <res-type>javax.mail.Session</res-type>    
       <res-auth>Container</res-auth>    
   </resource-ref>    
   配置資料庫連線池就可在此配置:    
   <resource-ref>    
       <description>JNDI JDBC DataSource of shop</description>    
       <res-ref-name>jdbc/sample_db</res-ref-name>    
       <res-type>javax.sql.DataSource</res-type>    
       <res-auth>Container</res-auth>    
   </resource-ref>    
15、安全限制配置    
   <security-constraint>    
      <display-name>Example Security Constraint</display-name>    
      <web-resource-collection>    
         <web-resource-name>Protected Area</web-resource-name>    
         <url-pattern>/jsp/security/protected/*</url-pattern>    
         <http-method>DELETE</http-method>    
         <http-method>GET</http-method>    
         <http-method>POST</http-method>    
         <http-method>PUT</http-method>    
      </web-resource-collection>    
      <auth-constraint>    
        <role-name>tomcat</role-name>    
        <role-name>role1</role-name>    
      </auth-constraint>    
   </security-constraint>    
16、登陸驗證配置    
   <login-config>    
     <auth-method>FORM</auth-method>    
     <realm-name>Example-Based Authentiation Area</realm-name>    
     <form-login-config>    
        <form-login-page>/jsp/security/protected/login.jsp</form-login-page>    
        <form-error-page>/jsp/security/protected/error.jsp</form-error-page>    
     </form-login-config>    
   </login-config>    
17、安全形色:security-role元素給出安全形色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子元素中。    
    分別地宣告角色可使高階IDE處理安全資訊更為容易。    
<security-role>    
     <role-name>tomcat</role-name>    
</security-role>    
18、Web環境引數:env-entry元素宣告Web應用的環境項    
<env-entry>    
     <env-entry-name>minExemptions</env-entry-name>    
     <env-entry-value>1</env-entry-value>    
     <env-entry-type>java.lang.Integer</env-entry-type>    
</env-entry>    
19、EJB 宣告    
<ejb-ref>    
     <description>Example EJB reference</decription>    
     <ejb-ref-name>ejb/Account</ejb-ref-name>    
     <ejb-ref-type>Entity</ejb-ref-type>    
     <home>com.mycompany.mypackage.AccountHome</home>    
     <remote>com.mycompany.mypackage.Account</remote>    
</ejb-ref>    
20、本地EJB宣告    
<ejb-local-ref>    
     <description>Example Loacal EJB reference</decription>    
     <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>    
     <ejb-ref-type>Session</ejb-ref-type>    
     <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>    
     <local>com.mycompany.mypackage.ProcessOrder</local>    
</ejb-local-ref>    
21、配置DWR    
<servlet>    
      <servlet-name>dwr-invoker</servlet-name>    
      <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>    
</servlet>    
<servlet-mapping>    
      <servlet-name>dwr-invoker</servlet-name>    
      <url-pattern>/dwr/*</url-pattern>    
</servlet-mapping>    
22、配置Struts    
    <display-name>Struts Blank Application</display-name>    
    <servlet>    
        <servlet-name>action</servlet-name>    
        <servlet-class>    
            org.apache.struts.action.ActionServlet    
        </servlet-class>    
        <init-param>    
            <param-name>detail</param-name>    
            <param-value>2</param-value>    
        </init-param>    
        <init-param>    
            <param-name>debug</param-name>    
            <param-value>2</param-value>    
        </init-param>    
        <init-param>    
            <param-name>config</param-name>    
            <param-value>/WEB-INF/struts-config.xml</param-value>    
        </init-param>    
        <init-param>    
            <param-name>application</param-name>    
            <param-value>ApplicationResources</param-value>    
        </init-param>    
        <load-on-startup>2</load-on-startup>    
    </servlet>    
    <servlet-mapping>    
        <servlet-name>action</servlet-name>    
        <url-pattern>*.do</url-pattern>    
    </servlet-mapping>    
    <welcome-file-list>    
        <welcome-file>index.jsp</welcome-file>    
    </welcome-file-list>    

    <!-- Struts Tag Library Descriptors -->    
    <taglib>    
        <taglib-uri>struts-bean</taglib-uri>    
        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>    
    </taglib>    
    <taglib>    
        <taglib-uri>struts-html</taglib-uri>    
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>    
    </taglib>    
    <taglib>    
    <taglib-uri>struts-nested</taglib-uri>    
    <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>    
    </taglib>    
    <taglib>    
        <taglib-uri>struts-logic</taglib-uri>    
        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>    
    </taglib>    
    <taglib>    
        <taglib-uri>struts-tiles</taglib-uri>    
        <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>    
    </taglib>    
23、配置Spring(基本上都是在Struts中配置的)    

   <!-- 指定spring配置檔案位置 -->    
   <context-param>    
      <param-name>contextConfigLocation</param-name>    
      <param-value>    
       <!--載入多個spring配置檔案 -->    
        /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml    
      </param-value>    
   </context-param>    

   <!-- 定義SPRING監聽器,載入spring -->    

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

<listener>    
     <listener-class>    
       org.springframework.web.context.request.RequestContextListener    
     </listener-class>    
</listener>

相關文章