1. 程式人生 > >Tomcat web.xml詳解

Tomcat web.xml詳解

Tomcat載入順序

載入類和資源的順序為:
1、/Web-INF/classes
2、/Web-INF/lib/*.jar
3、Bootstrap
4、System
5、$CATALINA_HOME/common/classes
6、$CATALINA_HOME/common/endores/*.jar
7、$CATALINA_HOME/common/lib/*.jar
8、$CATALINA_HOME/shared/classes
9、$CATALINA_HOME/shared/lib/*.jar

兩個web配置檔案

Servlet定義的時候,我發現在${catalina.home}/conf下以及${catalina.home}/webapps/ROOT/WEB_INF下都有web.xml這個檔案。

web.xml的檔案格式定義在Servlet規範中,因此所有符合Servlet規範的Java Servlet Container都會用到它。當tomcat部署應用程式時(在啟用過程中,或載入應用程式後),它都會讀取通用的conf/web.xml,然後再讀取web應用程式中的WEB-INF/web.xml。其實根據他們的位置,我們就可以知道,conf/web.xml檔案中的設定會應用於所有的web應用程式,而某些web應用程式的WEB-INF/web.xml中的設定只應用於該應用程式本身。

如果沒有WEB-INF/web.xml檔案,tomcat會輸出找不到的訊息,但仍然會部署並使用web應用程式,servlet規範的作者想要實現一種能迅速並簡易設定新範圍的方法,以用作測試,因此,這個web.xml並不是必要的,不過通常最好還是讓每一個上線的web應用程式都有一個自己的WEB-INF/web.xml。

系統web.xml詳解

<?xml version="1.0" encoding="GB2312"?> 
<!--
Web.xml依次定議瞭如下元素:
<web-app>
<display-name></display-name> 定義了WEB應用的名字
<description></description> 宣告WEB應用的描述資訊
<filter></filter>
<filter-mapping></filter-mapping>
<servlet></servlet>
<servlet-mapping></servlet-mapping>
<session-config></session-config>
<welcome-file-list></welcome-file-list>
<taglib></taglib>
<resource-ref></resource-ref>
<security-constraint></security-constraint>
<login-config></login-config>
</web-app>
在web.xml中元素定義的先後順序不能顛倒,否則Tomcat伺服器可能會丟擲SAXParseException.
-->
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Sample Application</display-name> <description> This is a Sample Application </description> <!-- filter 配置Servlet過濾器 filter-name 定義過濾器的名字。當有多個過濾器時,不能同名 filter-class 指定實現這一過濾的類,這個類負責具體的過濾事務 --> <filter> <filter-name>SampleFilter</filter-name> <filter-class>mypack.SampleFilter</filter-class> </filter> <!-- filter-mapping 設定過濾器負責過濾的URL filter-name 過濾器名。這裡的名字一定要和filter中的過濾器名匹配 url-pattern 指定過濾器負責過濾的URL --> <filter-mapping> <filter-name>SampleFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- servlet 配置Servlet. servlet-name 定義Servlet的名字 servlet-class 指定實現這個servlet的類 init-param 定義Servlet的初始化引數和引數值,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化引數 load-on-startup 指定當Web應用啟動時,裝載Servlet的次序。 當值為正數或零時:Servlet容器先載入數值小的servlet,再依次載入其他數值大的servlet. 當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時載入它 --> <servlet> <servlet-name>SampleServlet</servlet-name> <servlet-class>mypack.SampleServlet</servlet-class> <init-param> <param-name>initParam1</param-name> <param-value>2</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置servlet對映(下面程式碼為SampleServlet指定的相對URL為"/sample": servlet-name 指定servlet的名字,這裡的名字應該和<Servlet>元素中定義的名字匹配。 url-pattern 指定訪問這個servlet的URL。只需給出相對路徑。 --> <servlet-mapping> <servlet-name>SampleServlet</servlet-name> <url-pattern>/sample</url-pattern> </servlet-mapping> <!--配置session session用來設定HttpSession的生命週期。單位(秒)--> <session-config> <session-timeout>30</session-timeout> </session-config> <!--配置Wel0come0檔案清單--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list> <!-- 配置Tag Library taglib-uri 設定Tag Library的唯一識別符號,在Web應用中將根據這一識別符號來引用Tag Library taglib-location 指定和Tag Library對應的TLD檔案的位置 --> <taglib> <taglib-uri>/mytaglib</taglib-uri> <taglib-location>/WEB-INF/mytaglib.tld</taglib-location> </taglib> <!-- 配置資源引用 description 對所引用的資源的說明 res-ref-name 指定所引用資源的JNDI名字 res-type 指定所引用資源的類名字 res-auth 指定管理所引用資源的Manager,它有兩個可選值: Container:由容器來建立和管理resource Application:同WEB應用來建立和管理Resource --> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/sampleDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <!-- 配置安全約束(以下程式碼指定當使用者訪問該WEB應用下的所有資源時,必須具備guest角色) web-resource-collection 宣告受保護的WEB資源 auth-constraint 宣告可以訪問受保護資源的角色,可以包含多個<role-name>子元素 web-resource-name 標識受保護的WEB資源 url-pattern 指定受保護的URL路徑 --> <Security-constraint> <web-resource-collection> <web-resource-name>sample appliction</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>guest</role-name> </auth-constraint> </Security-constraint> <!-- 配置安全驗證登入介面:指定當WEB客戶訪問受保護的WEB資源時,系統彈出的登入對話方塊的型別。 auth-method 指定驗證方法,它有三個可選值:BASIC(基本驗證)、DIGEST(摘要驗證)、FORM(表單驗證) realm-name 設定安全域的名稱 form-login-config 當驗證方法為FORM時,配置驗證網頁和出錯網頁 form-login-page 當驗證方法為FORM時,設定驗證網頁 form-error-page 當驗證方法為FORM時,設定出錯網頁 --> <login-config> <auth-method>FORM</auth-method> <realm-name> Tomcat Server Configuration form-Based Authentication Area </realm-name> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/error.jsp</form-error-page> </form-login-config> </login-config> <!--配置對安全驗證角色的引用--> <security-role> <description> The role that is required to log into the sample application </description> <role-name>guest</role-name> </security-role> </web-app>

五、web.xml配置簡介:
1、預設(歡迎)檔案的設定
在tomcat4\conf\web.xml中,與IIS中的預設檔案意思相同。

<welcome-file-list>
   <welcome-file>index.html</welcome-file>
   <welcome-file>index.htm</welcome-file>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

2、報錯檔案的設定

<error-page>
<error-code>404</error-code>
<location>/notFileFound.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/null.jsp</location>
</error-page>

如果某檔案資源沒有找到,伺服器要報404錯誤,按上述配置則會呼叫\webapps\ROOT\notFileFound.jsp。
如果執行的某個JSP檔案產生NullPointException ,則會呼叫\webapps\ROOT\null.jsp

3、會話超時的設定
設定session 的過期時間,單位是分鐘;

<session-config>
<session-timeout>30</session-timeout>
</session-config>

4、過濾器的設定

<filter>
<filter-name>FilterSource</filter-name>
<filter-class>project4. FilterSource </filter-class>
</filter>
<filter-mapping>
<filter-name>FilterSource</filter-name>
<url-pattern>/WwwServlet</url-pattern>
(<url-pattern>/haha/*</url-pattern>)
</filter-mapping>

過濾:
1) 身份驗證的過濾Authentication Filters
2) 日誌和稽核的過濾Logging and Auditing Filters
3) 圖片轉化的過濾Image conversion Filters
4) 資料壓縮的過濾Data compression Filters
5) 加密過濾Encryption Filters
6) Tokenizing Filters
7) 資源訪問事件觸發的過濾Filters that trigger resource access events XSL/T 過濾XSL/T filters
8) 內容型別的過濾Mime-type chain Filter 注意監聽器的順序,如:先安全過濾,然後資源,然後內容型別等,這個順序可以自己定。(更加詳細的解釋,見參考文件)

參考文件

tomcat中載入順序

首先可以肯定的是,載入順序與它們在 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一直在報錯,應該把 放到 中

   <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 -->