web.xml文件詳解
前言:一般的web工程中都會用到web.xml,web.xml主要用來配置,可以方便的開發web工程。web.xml主要用來配置Filter、Listener、Servlet等。但是要說明的是web.xml並不是必須的,一個web工程可以沒有web.xml文件。
1、WEB工程加載web.xml過程
經過個人測試,WEB工程加載順序與元素節點在文件中的配置順序無關。即不會因為 filter 寫在 listener 的前面而會先加載 filter。WEB容器的加載順序是:ServletContext -> context-param -> listener -> filter -> servlet。並且這些元素可以配置在文件中的任意位置。
加載過程順序如下:
- 啟動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個結點。
- 緊急着,容創建一個ServletContext(servlet上下文),這個web項目的所有部分都將共享這個上下文。
- 容器將<context-param>轉換為鍵值對,並交給servletContext。
- 容器創建<listener>中的類實例,創建監聽器。
2、web.xml文件元素詳解
1、schema
web.xml的模式文件是由Sun公司定義的,每個web.xml文件的根元素<web-app>中,都必須標明這個 web.xml使用的是哪個模式文件。其它的元素都放在<web-app></web-app>之中。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" 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"> </web-app>
2、<icon>Web應用圖標
指出IDE和GUI工具用來表示Web應用的大圖標和小圖標。
<icon> <small-icon>/images/app_small.gif</small-icon> <large-icon>/images/app_large.gif</large-icon> </icon>
3、<display-name>Web應用名稱
提供GUI工具可能會用來標記這個特定的Web應用的一個名稱
<display-name>Tomcat Example</display-name>
4、<disciption>Web應用描述
給出於此相關的說明性文本
<disciption>Tomcat Example servlets and JSP pages.</disciption>
5、<context-param>上下文參數
聲明應用范圍內的初始化參數。它用於向 ServletContext提供鍵值對,即應用程序上下文信息。我們的listener, filter等在初始化時會用到這些上下文中的信息。在servlet里面可以通過getServletContext().getInitParameter("context/param")得到。
<context-param> <param-name>ContextParameter</para-name> <param-value>test</param-value> <description>It is a test parameter.</description> </context-param>
6、<filter>過濾器
將一個名字與一個實現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>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>setCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
7、<listener>監聽器
<listener> <listerner-class>com.listener.SessionListener</listener-class> </listener>
8、<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。在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
<!-- 基本配置 --> <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>
9、<session-config>會話超時配置
單位為分鍾。
<session-config> <session-timeout>120</session-timeout> </session-config>
10、<mime-mapping>
<mime-mapping> <extension>htm</extension> <mime-type>text/html</mime-type> </mime-mapping>
11、<welcome-file-list>歡迎文件頁
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list>
12、<error-page>錯誤頁面
<!-- 1、通過錯誤碼來配置error-page。當系統發生×××錯誤時,跳轉到錯誤處理頁面。 --> <error-page> <error-code>404</error-code> <location>/NotFound.jsp</location> </error-page> <!-- 2、通過異常的類型配置error-page。當系統發生java.lang.NullException(即空指針異常)時,跳轉到錯誤處理頁面。 --> <error-page> <exception-type>java.lang.NullException</exception-type> <location>/error.jsp</location> </error-page>
13、<jsp-config>設置jsp
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 兩個子元素。其中<taglib> 元素在JSP 1.2 時就已經存在;而<jsp-property-group> 是JSP 2.0 新增的元素。
<jsp-property-group> 元素主要有八個子元素,它們分別為:
- <description>:設定的說明
- <display-name>:設定名稱
- <url-pattern>:設定值所影響的范圍,如: /CH2 或 /*.jsp
- <el-ignored>:若為 true,表示不支持 EL 語法
- <scripting-invalid>:若為 true,表示不支持 <% scripting %>語法
- <page-encoding>:設定 JSP 網頁的編碼
- <include-prelude>:設置 JSP 網頁的抬頭,擴展名為 .jspf
- <include-coda>:設置 JSP 網頁的結尾,擴展名為 .jspf
<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 應用程式來說,Scriptlet 是個不樂意被見到的東西,因為它會使得HTML 與Java 程式碼交相混雜,對於程式的維護來說相當的麻煩,必要的時候,可以在web.xml 中加上<script-invalid> 標簽,設定所有的JSP 網頁都不可以使用Scriptlet。
3、Mapping規則
當一個請求發送到servlet容器的時候,容器先會將請求的url減去當前應用上下文的路徑作為servlet的映射url,比如我訪問的是http://localhost/test/aaa.html,我的應用上下文是test,容器會將http://localhost/test去掉,剩下的/aaa.html部分拿來做servlet的映射匹配。這個映射匹配過程是有順序的,而且當有一個servlet匹配成功以后,就不會去理會剩下的servlet了。
其匹配規則和順序如下:
- 精確路徑匹配。例子:比如servletA 的url-pattern為 /test,servletB的url-pattern為 /* ,這個時候,如果我訪問的url為http://localhost/test ,這個時候容器就會先 進行精確路徑匹配,發現/test正好被servletA精確匹配,那么就去調用servletA,也不會去理會其他的servlet了。
- 最長路徑匹配。例子:servletA的url-pattern為/test/*,而servletB的url-pattern為/test/a/*,此時訪問http://localhost/test/a時,容器會選擇路徑最長的servlet來匹配,也就是這里的servletB。
- 擴展匹配,如果url最后一段包含擴展,容器將會根據擴展選擇合適的servlet。例子:servletA的url-pattern:*.action
以”/’開頭和以”/*”結尾的是用來做路徑映射的。以前綴”*.”開頭的是用來做擴展映射的。所以,為什么定義”/*.action”這樣一個看起來很正常的匹配會錯?因為這個匹配即屬於路徑映射,也屬於擴展映射,導致容器無法判斷。