目錄
web.xml文件是Java Web項目中的一個配置文件,主要用於配置歡迎頁、Filter、Listener、Servlet等,但並不是必須的,一個Java Web項目沒有web.xml文件也是照樣能跑起來的。
1. web.xml各版本區別
首先來看一下Tomcat官網的Servlet和JSP規范規范與的Apache Tomcat版本之間的對應關系,如圖:
從中可以清晰的看到不同版本的web.xml文件要使用相應版本的Tomcat服務器。
下面是各版本的web.xml配置的頭部聲明:
servlet 2.3
<?xml version="1.0" encoding="UTF-8"?> <!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> </web-app>
servlet 2.4
<?xml version="1.0" encoding="UTF-8"?> <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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> </web-app>
servlet 2.5
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> </web-app>
servlet 3.0
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> </web-app>
servlet 3.1
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> </web-app>
servlet 4.0
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> </web-app>
Tomcat在激活、加載、部署web應用時,會解析加載${CATALINA_HOME}/conf目錄下所有web應用通用的web.xml,然后解析加載web應用目錄中的WEB-INF/web.xml。如果沒有WEB-INF/web.xml文件,tomcat會輸出找不到的消息,但仍然會部署並使用web應用程序,因此,這個web.xml並不是必要的,不過通常最好還是讓每一個上線的web應用程序都有一個自己的WEB-INF/web.xml。
conf/web.xml文件中的設定會應用於所有的web應用程序,而web應用程序的WEB-INF/web.xml中的設定只應用於該應用程序本身。
2. web.xml配置詳解
2.1 java web項目啟動加載順序
(1)啟動WEB項目的時候,容器(如:Tomcat)會去讀它的配置文件web.xml的兩個節點:
<listener></listener> 和 <context-param></context-param>;
(2)容器創建一個ServletContext(上下文);
(3)容器將<context-param></context-param>轉化為鍵值對,並交給ServletContext;
(4)容器創建<listener></listener>中的類實例,即創建監聽;
(5)容器初始化<filter></filter>,web.xml中可以定義多個 filter,初始化每個 filter 時,是按照 filter 配置節出現的順序來初始化的,當請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping 配置節出現的順序來依次調用 doFilter() 方法的;
(6)容器初始化<servlet></servlet>, servlet 同 filter 類似。
那么,web項目啟動時,可以知道web.xml文件各個節點的加載順序:context-param -> listener -> filter -> servlet
2.2 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應用圖標
<icon> <small-icon>/images/app_small.gif</small-icon> <large-icon>/images/app_large.gif</large-icon> </icon>
(2)Web 應用名稱
<display-name>App Name</display-name>
(3)Web 應用描述
<disciption>This is a app disciption.</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)過濾器配置
<filter> <filter-name>setCharacterEncoding</filter-name> <filter-class>com.myTest.setCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</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>test</servlet-name> <servlet-class>TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping>
高級配置:<servlet>
<servlet-name>test</servlet-name> <servlet-class>TestServlet</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> <!--
當Web應用啟動時,裝載Servlet的次序 當值為正數或零時:Servlet容器先加載數值小的servlet,再依次加載其他數值大的servlet. 當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping>
(8)會話超時配置(單位為分鍾)
<session-config> <session-timeout>120</session-timeout> </session-config>
(9)MIME類型配置
<mime-mapping> <extension>html</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)配置錯誤頁面
方法1:通過錯誤碼來配置error-page
<error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>
上面的配置當系統發生404錯誤時,頁面將跳轉到錯誤處理頁面404.jsp
方法2:通過異常類型配置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>