1、WEB工程加載web.xml過程
經過個人測試,WEB工程加載順序與元素節點在文件中的配置順序無關。即不會因爲 filter 寫在 listener 的前面而會先加載 filter。WEB容器的加載順序是:ServletContext -> context-param -> listener -> filter -> servlet。並且這些元素可以配置在文件中的任意位置。
加載過程順序如下:
- 啓動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,讀取和兩個結點。
- 緊急着,容創建一個ServletContext(servlet上下文),這個web項目的所有部分都將共享這個上下文。
- 容器將轉換爲鍵值對,並交給servletContext。
- 容器創建中的類實例,創建監聽器。
2、web.xml文件元素詳解
1、schema
web.xml的模式文件是由Sun公司定義的,每個web.xml文件的根元素中,都必須標明這個 web.xml使用的是哪個模式文件。其它的元素都放在之中。
<?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、Web應用圖標
指出IDE和GUI工具用來表示Web應用的大圖標和小圖標。
<icon>
<small-icon>/images/app_small.gif</small-icon>
<large-icon>/images/app_large.gif</large-icon>
</icon>
3、Web應用名稱
提供GUI工具可能會用來標記這個特定的Web應用的一個名稱
<display-name>Tomcat Example</display-name>
4、Web應用描述
給出於此相關的說明性文本
<disciption>Tomcat Example servlets and JSP pages.</disciption>
5、上下文參數
聲明應用範圍內的初始化參數。它用於向 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、過濾器
將一個名字與一個實現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>
<listerner-class>com.listener.SessionListener</listener-class>
</listener>
常用的監聽接口有以下幾個:
ServletContextAttributeListener監聽對ServletContext屬性的操作,比如增加、刪除、修改屬性。
ServletContextListener監聽ServletContext。當創建ServletContext時,激發 contextInitialized(ServletContextEvent sce)方法;當銷燬ServletContext時,激發contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener監聽HttpSession的操作。當創建一個Session時,激發session Created(HttpSessionEvent se)方法;當銷燬一個Session時,激發essionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener監聽HttpSession中的屬性的操作。當在Session增加一個屬性時,激發 attributeAdded(HttpSessionBindingEvent se)方法;當在Session刪除一個屬性時,激發attributeRemoved(HttpSessionBindingEvent se)方法;當在Session屬性被重新設置時,激發attributeReplaced(HttpSessionBindingEvent se) 方法。
8、
用來聲明一個servlet的數據,主要有以下子元素:
- 指定servlet的名稱
- 指定servlet的類名稱
- 指定web站臺中的某個JSP網頁的完整路徑
- 用來定義參數,可有多個init-param。在servlet類中通過getInitParamenter(String
- name)方法訪問初始化參數
- 指定當Web應用啓動時,裝載Servlet的次序。當值爲正數或零時:Servlet容器先加載數值小的servlet,再依次加載其他數值大的servlet。當值爲負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它。
- 用來定義servlet所對應的URL,包含兩個子元素
- 指定servlet的名稱
- 指定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-timeout>120</session-timeout>
</session-config>
10、
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
11、歡迎文件頁
<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、錯誤頁面
<!-- 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
包括 和 兩個子元素。其中 元素在JSP 1.2 時就已經存在;而 是JSP 2.0 新增的元素。
元素主要有八個子元素,它們分別爲:
- :設定的說明
- :設定名稱
- :設定值所影響的範圍,如:/CH2 或 /*.jsp
- :若爲 true,表示不支持 EL 語法
- :若爲 true,表示不支持 <% scripting %>語法
- :設定 JSP 網頁的編碼
- :設置 JSP 網頁的擡頭,擴展名爲.jspf
- :設置 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 中加上 標籤,設定所有的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”這樣一個看起來很正常的匹配會錯?因爲這個匹配即屬於路徑映射,也屬於擴展映射,導致容器無法判斷。