一、web.xml配置節點簡介
(1) context-param
- 格式定義
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mybatis.xml</param-value>
</context-param>
-
作用:該元素用來聲明應用範圍(整個WEB項目)內的上下文初始化參數。 param-name 設定上下文的參數名稱。必須是唯一名稱 param-value 設定的參數名稱的值,這裏的例子是指定spring配置文件的位置
(2) listener
- 格式定義
//listen-class 指定監聽類,該類繼承ServletContextListener 包含初始化方法contextInitialized(ServletContextEvent event) 和銷燬方法contextDestoryed(ServletContextEvent event)
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 作用:該元素用來註冊一個監聽器類。可以收到事件什麼時候發生以及用什麼作爲響應的通知。事件監聽程序在建立、修改和刪除會話或servlet環境時得到通知。常與context-param聯合使用。
(3)filter
- 格式定義
- <filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 作用:用於指定WEB容器的過濾器, filter能夠在一個請求到達servlet之前預處理用戶請求,也可以在離開servlet時處理http響應;在執行servlet之前,首先執行filter程序,併爲之做一些預處理工作;根據程序需要修改請求和響應;在servlet被調用之後截獲servlet的執行。
(4)servlet
- 格式定義
//配置Spring MVC,指定處理請求的Servlet,有兩種方式:
//1. 默認查找MVC配置文件的地址是:/WEB-INF/${servletName}-servlet.xml
//2. 可以通過配置修改MVC配置文件的位置,需要在配置DispatcherServlet時指定MVC配置文件的位置。
//這裏使用的是第二種方式
<!-- Springmvc的核心控制器 -->
<servlet>
<servlet-name>dispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatchServlet</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
作用: 創建並返回一個包含基於客戶請求性質的動態內容的完整的html頁面;
創建可嵌入到現有的html頁面中的一部分html頁面(html片段);
讀取客戶端發來的隱藏數據;
讀取客戶端發來的顯示數據;
與其他服務器資源(包括數據庫和java的應用程序)進行通信;
二、 web.xml加載過程(步驟):
- 啓動web項目,容器(如Tomcat、Apache)會去讀取它的配置文件web.xml 中的兩個節點,context-param和listener。
- 緊接着,容器將創建一個ServletContext(又稱爲:Servlet上下文),應用範圍內即整個WEB項目都能使用這個Servlet上下文。
容器將< context-param >轉化爲鍵值對,並交給ServletContext。 - 容器創建< listener >中的類實例,即創建監聽。(備註:listener定義的類可以是自定義的類但必須需要繼承ServletContextListener)。
- 在監聽中會有contextInitialized(ServletContextEvent args)初始化方法,在這個方法中獲得:ServletContext = ServletContextEvent.getServletContext(); context-param的值 = ServletContext.getInitParameter(“context-param的鍵”); 在這個類中還必須有一個contextDestroyed(ServletContextEvent event) 銷燬方法。用於關閉應用前釋放資源,比如說數據庫連接的關閉。
- 得到這個context-param的值之後,你就可以做一些操作了。注意,這個時候你的WEB項目還沒有完全啓動完成。這個動作會比所有的Servlet都要早。
- 舉例.你可能想在項目啓動之前就打開數據庫。那麼這裏就可以在< context-param >中設置數據庫的連接方式,在監聽類中初始化數據庫的連接
- 補充知識:ServletContext,是一個全局的儲存信息的空間,服務器開始,其就存在,服務器關閉,其才釋放。request,一個用戶可有多個;session,一個用戶一個;而servletContext,所有用戶共用一個。所以,爲了節省空間,提高效率,ServletContext中,要放必須的、重要的、所有用戶需要共享的線程又是安全的一些信息。例如,一個購物網站,用戶要訪問商品的詳細信息,如果放在session域,每個用戶都要訪問一遍數據庫,這樣效率太低;而放在ServletContext中,服務器一啓動,就訪問數據庫將商品信息放入數據庫,這樣所有用戶只需要通過上下文就能訪問到商品的信息。
三、web.xml節點加載順序:
1. web.xml節點的加載順序與它們在web.xml中位置的先後無關,即不會因爲< filter >寫在< context-param >前面就先加載< filter >。
2. 上文也提到到了,< context-param >用於對ServletContext提供鍵值對,即應用程序的上下文信息。而listener、servlet等節點在初始化的過程中會使用到這些上下文信息,所以最後我們得出web.xml節點的加載順序應該爲:context-param->listener->filter->servlet。
3. 對於某類配置節點而言,位置的先後是有要求的。以servlet舉例,與servlet相關的配置節點是servlet-mapping,對於擁有相同配置節servlet-name的servlet和servlet-mapping來說,servlet-mapping必須在servlet後定義,否則當解析到servlet-mapping時,它的servlet-name還沒有定義。web 容器啓動時初始化每個 servlet時,是按照 servlet配置節出現的順序來初始化的。
4. 最終結論: web.xml 的加載順序是:[context-param -> listener -> filter -> servlet -> spring] ,而同類型節點之間的實際程序調用的時候的順序是根據對應的 mapping 的順序進行調用的。