web.xml各個配置的加載順序

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

一、概述

1、啓動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,先讀取<listener>和<context-param>兩個結點。 

2、緊接着,WEB容器創建一個ServletContext(servlet上下文),這個web項目的所有部分都將共享這個上下文。 

3、容器將<context-param>轉換爲鍵值對,並交給servletContext。 

4、容器創建<listener>中的類實例,創建監聽器。 

二 、 load-on-startup

load-on-startup 元素在web應用啓動的時候指定了servlet被加載的順序,它的值必須是一個整數。如果它的值是一個負整數或是這個元素不存在,那麼容器會在該 servlet被調用的時候,加載這個servlet 。如果值是正整數或零,容器在配置的時候就加載並初始化這個servlet,容器必須保證值小的先被加載。如果值相等,容器可以自動選擇先加載誰。  

在servlet的配置當中,<load-on-startup>5</load-on-startup>的含義是: 

標記容器是否在啓動的時候就加載這個servlet。 

當值爲0或者大於0時,表示容器在應用啓動時就加載這個servlet; 

當是一個負數時或者沒有指定時,則指示容器在該servlet被選擇時才加載。 

正數的值越小,啓動該servlet的優先級越高。 

三  、加載順序

首先可以肯定的是,加載順序與它們在 web.xml 文件中的先後順序無關。即不會因爲 filter 寫在 listener 的前面而會先加載 filter。最終得出的結論是:ServletContext -> 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 的加載順序是:ServletContext -> context-param -> listener -> filter -> servlet ,而同個類型之間的實際程序調用的時候的順序是根據對應的 mapping 的順序進行調用的。

四 、 web.xml文件詳解 


我將自己知道的web.xml的元素整理了一下:

1、web.xml首先是肯定要包含它的schema.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

</web-app>

其它的元素都放在<web-app></web-app>之中。

<discription></discription> 是對站臺的描述
<display-name></display-name> 定義站臺的名稱
<distributable/> 是指定該站臺是否可分佈式處理
<context-param></context-param> 用來設定web站臺的環境參數,它包含兩個子元素:
    <param-name></param-name> 用來指定參數的名稱
    <param-value></param-value> 用來設定參數值

比如:
<context-param>
    <param-name>my_param</param-name>
    <param-value>hello</param-value>
</context-param>

在此設定的參數,可以在servlet中用 getServletContext().getInitParameter("my_param") 來取得

2、listener

<listener></listener> 用來設定Listener接口,它的主要子元素爲
    <listener-class></listener-class> 定義Listener的類名稱

比如:
<listener>
    <listener-class>com.myTest.ContextListener</listener-class> 
</listener>

3.1、filter
<filter></filter> 是用來聲明filter的相關設定,它包含以下子元素:
    <filter-name></filter-name> 這當然就是指定filter的名字
    <filter-class></filter-class> 這是用來定義filter的類的名稱
    <init-param></init-param> 用來定義參數,它有兩個子元素:
        <param-name></param-name> 用來指定參數的名稱
        <param-value></param-value> 用來設定參數值

比如:
<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>

3.2、filter-mapping

與<filter></filter>同時使用的是<filter-mapping></filter-mapping> 用來定義filter所對應的URL,它有兩個子元素:
    <filter-name></filter-name> 指定filter的名字
    <url-pattern></url-pattern> 指定filter所對應的URL

比如:
<filter-mapping>
        <filter-name>setCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

4.1、servlet

<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>差不多

4.2、servlet-mapping

與<servlet></servlet>一起使用的是<servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素:
    <servlet-name></servlet-name> 指定servlet的名稱
    <url-pattern></url-pattern> 指定servlet所對應的URL

比如:
<servlet>
        <servlet-name>ShoppingServlet</servlet-name>
        <servlet-class>com.myTest.ShoppingServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>ShoppingServlet</servlet-name>
        <url-pattern>/shop/ShoppingServlet</url-pattern>
</servlet-mapping>

5、session-config
<session-config></session-config> 用來定義web站臺中的session參數,包含一個子元素:
    <session-timeout></session-timeout> 用來定義這個web站臺所有session的有效期限,單位爲 分鐘

6、mime-mapping
<mime-mapping></mime-mapping> 定義某一個擴展名和某一個MIME Type做對映,包含兩個子元素:
    <extension></extension> 擴展名的名稱
    <mime-type></mime-type> MIME格式

比如:
<mime-mapping>
    <extension>doc</extension>
    <mime-type>application/vnd.ms-word</mime-type>
</mime-mapping> 
<mime-mapping>
    <extension>xls</extension>
    <mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>

7、welcom-file-list
<welcome-file-list></welcom-file-list> 用來定義首頁的列單,包含一個子元素:
    <welcome-file></welcome-file> 指定首頁的文件名稱

比如:
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
</welcom-file-list>

8、error-page
<error-page></error-page> 用來處理錯誤代碼或異常的頁面,有三個子元素:
    <error-code></error-code> 指定錯誤代碼
    <exception-type></exception-type> 指定一個JAVA異常類型
    <location></location> 指定在web站臺內的相關資源路徑

比如:
<error-page>
    <error-code>404</error-code>
    <location>/error404.jsp</location>
</error-page>
<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/exception.jsp</location>
</error-page>

9、taglib
<taglib></taglib> 用來設定JSP網頁所用到的Tag Library路徑,有兩個子元素:
    <taglib-uri></taglib-uri> 定義TLD文件的URI,在JSP網頁中用taglib指令便可取得該URI 的TLD文件
    <taglib-location></taglib-location> 指定TLD文件相對於web站臺的存放位置

比如:
<taglib>
      <taglib-uri>myTaglib</taglib-uri>
      <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>

10、resource-ref
<resource-ref></resource-ref> 定義利用JNDI取得站臺可利用的資源,有五個子元素:
    <description></description> 資源說明
    <rec-ref-name></rec-ref-name> 資源名稱
    <res-type></res-type> 資源種類
    <res-auth></res-auth> 資源經由Application或Container來許可
    <res-sharing-scope></res-sharing-scope> 資源是否可以共享,有Shareable和Unshareable兩 個                                                                     值,默認爲Shareable

比如,配置數據庫連接池就可在此配置:
<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>

11、jsp-config
<jsp-config> 包括<taglib> 和<jsp-property-group> 兩個子元素。
    其中<taglib>元素在JSP 1.2時就已經存在;而<jsp-property-group>是JSP 2.0 新增的元素。
    <jsp-property-group>元素主要有八個子元素,它們分別爲:
    1).<description>:設定的說明;
    2).<display-name>:設定名稱;
    3).<url-pattern>:設定值所影響的範圍,如:/CH2 或 /*.jsp;
    4).<el-ignored>:若爲true,表示不支持EL 語法;
    5).<scripting-invalid>:若爲true,表示不支持<% scripting %>語法;
    6).<page-encoding>:設定JSP 網頁的編碼;
    7).<include-prelude>:設置JSP 網頁的擡頭,擴展名爲.jspf;
    8).<include-coda>:設置JSP 網頁的結尾,擴展名爲.jspf。
一個簡單的<jsp-config>元素完整配置:

<jsp-config>
    <taglib>
        <taglib-uri>Taglib</taglib-uri>
        <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
    </taglib>
    <jsp-property-group>
        <description>Special property group for JSP Configuration JSP example.</description>
        <display-name>JSPConfiguration</display-name>
        <url-pattern>/jsp/* </url-pattern>
        <el-ignored>true</el-ignored>
        <page-encoding>GB2312</page-encoding>
        <scripting-invalid>true</scripting-invalid>
        <include-prelude>/include/prelude.jspf</include-prelude>
        <include-coda>/include/coda.jspf</include-coda>
    </jsp-property-group>
</jsp-config> 
配置web.xml來限制對某些servlet的請求

有時我們只希望通過認證的用戶才能請求某些servlet的話,就可以在web.xml中來進行相應的配置,來達到此目的。

這就要用到<security-constraint></security-constraint>元素。
對於 tomcat,中web.xml使用security-constraint元素需要在位於<Tomcat-installation- directory>/conf/tomcat-users.xml的XML文件中創建用戶名和密碼。比如下面的這個tomcat- users.xml文件:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,manager"/>
  <user username="admin" password="admin" roles="admin"/>
</tomcat-users>

此XML片段包括一個tomcat-users根元素,它包含一個或多個role和user元素。
然後在Web應用程序的web.xml中創建security-constraint、login-config和security-role元素。

<security-constraint>
      <web-resource-collection>
          <web-resource-name>HelloServlet</web-resource-name>
          <url-pattern>/HelloServlet</url-pattern>
          <http-method>GET</http-method>
          <http-method>POST</http-method>
      </web-resource-collection>
      <auth-constraint>
          <description>This applies only to the "tomcat" security role</description>
          <role-name>admin</role-name>
      </auth-constraint>
      <user-data-constraint>
          <transport-guarantee>NONE</transport-guarantee>
      </user-data-constraint>
  </security-constraint>
  <login-config>
      <auth-method>BASIC</auth-method>
  </login-config>
  <security-role>
      <role-name>admin</role-name>
  </security-role>

其中security-constraint元素包含一個或多個web-resource-collection元素,它是描述Web應用程序中的 哪些web資源受到指定安全限制的保護。http-method元素指定安全限制覆蓋的HTTP方法。上面的例子中,當我們對/HelloServlet 的GET或POST請求時將觸發配置的安全機制。
auth-constraint元素用於描述允許訪問Web組件的安全角色。此例中安全角色的例子有tomcat、manager、admin。而只有當作爲admin角色的用戶纔可以訪問HelloServlet。

Web應用程序通過login-config元素來認證用戶,並確認該用戶是否爲正確的角色。
longin-config包含的 transport-guarantee子元素用來指定認證方法,BASIC是一種常見的Web認證方式,瀏覽器給用戶提示一個對話框,要求輸入用戶名和 密碼,隨後Tomcat將給出的用戶名和密碼與tomcat-users.xml中的用戶名和密碼進行比較,然後使用前面的security- constraint配置來確定用戶是否可訪問受保護的servlet。

(除BASIC外,還可以是FORM、CLIENT-CERT、DIGEST等)

其實這種認證方法實際上有兩個步驟:
1、檢查提供的用戶名和密碼是否正確。
2、判斷用戶是否映射到特定的安全角色。例如,用戶可能提供了正確的用戶名和密碼,但沒有映射到特定的安全角色,也將被禁止訪問特定的Web資源。


五 、相應元素配置   
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、監聽器配置    
<listener>    
      <listerner-class>listener.SessionListener</listener-class>    
</listener>   
6、過濾器配置:將一個名字與一個實現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>    

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>    
     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章