一、web.xml配置節(jié)點(diǎn)簡介
(1) context-param
格式定義
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mybatis.xml</param-value>
</context-param>
1
2
3
4
作用:該元素用來聲明應(yīng)用范圍(整個(gè)WEB項(xiàng)目)內(nèi)的上下文初始化參數(shù)竟纳。
param-name 設(shè)定上下文的參數(shù)名稱撵溃。必須是唯一名稱
param-value 設(shè)定的參數(shù)名稱的值,這里的例子是指定spring配置文件的位置
(2) listener
格式定義
//listen-class 指定監(jiān)聽類锥累,該類繼承ServletContextListener 包含初始化方法contextInitialized(ServletContextEvent event) 和銷毀方法contextDestoryed(ServletContextEvent event)
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
1
2
3
4
作用:該元素用來注冊一個(gè)監(jiān)聽器類缘挑。可以收到事件什么時(shí)候發(fā)生以及用什么作為響應(yīng)的通知桶略。事件監(jiān)聽程序在建立语淘、修改和刪除會(huì)話或servlet環(huán)境時(shí)得到通知。常與context-param聯(lián)合使用际歼。
(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>
1
2
3
4
5
6
7
8
9
10
11
12
作用:用于指定WEB容器的過濾器惶翻, filter能夠在一個(gè)請求到達(dá)servlet之前預(yù)處理用戶請求,也可以在離開servlet時(shí)處理http響應(yīng)蹬挺;在執(zhí)行servlet之前维贺,首先執(zhí)行filter程序它掂,并為之做一些預(yù)處理工作巴帮;根據(jù)程序需要修改請求和響應(yīng)溯泣;在servlet被調(diào)用之后截獲servlet的執(zhí)行。
(4)servlet
- 格式定義
//配置Spring MVC榕茧,指定處理請求的Servlet垃沦,有兩種方式:
//1. 默認(rèn)查找MVC配置文件的地址是:/WEB-INF/${servletName}-servlet.xml
//2. 可以通過配置修改MVC配置文件的位置,需要在配置DispatcherServlet時(shí)指定MVC配置文件的位置用押。
//這里使用的是第二種方式
<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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
作用: 創(chuàng)建并返回一個(gè)包含基于客戶請求性質(zhì)的動(dòng)態(tài)內(nèi)容的完整的html頁面肢簿;
創(chuàng)建可嵌入到現(xiàn)有的html頁面中的一部分html頁面(html片段);
讀取客戶端發(fā)來的隱藏?cái)?shù)據(jù)蜻拨;
讀取客戶端發(fā)來的顯示數(shù)據(jù)池充;
與其他服務(wù)器資源(包括數(shù)據(jù)庫和java的應(yīng)用程序)進(jìn)行通信;
二缎讼、 web.xml加載過程(步驟):
啟動(dòng)web項(xiàng)目收夸,容器(如Tomcat、Apache)會(huì)去讀取它的配置文件web.xml 中的兩個(gè)節(jié)點(diǎn)血崭,context-param和listener卧惜。
緊接著,容器將創(chuàng)建一個(gè)ServletContext(又稱為:Servlet上下文)夹纫,應(yīng)用范圍內(nèi)即整個(gè)WEB項(xiàng)目都能使用這個(gè)Servlet上下文咽瓷。
容器將< context-param >轉(zhuǎn)化為鍵值對,并交給ServletContext舰讹。
容器創(chuàng)建< listener >中的類實(shí)例,即創(chuàng)建監(jiān)聽茅姜。(備注:listener定義的類可以是自定義的類但必須需要繼承ServletContextListener)。
在監(jiān)聽中會(huì)有contextInitialized(ServletContextEvent args)初始化方法,在這個(gè)方法中獲得:ServletContext = ServletContextEvent.getServletContext(); context-param的值 = ServletContext.getInitParameter(“context-param的鍵”)月匣; 在這個(gè)類中還必須有一個(gè)contextDestroyed(ServletContextEvent event) 銷毀方法匈睁。用于關(guān)閉應(yīng)用前釋放資源,比如說數(shù)據(jù)庫連接的關(guān)閉桶错。
得到這個(gè)context-param的值之后航唆,你就可以做一些操作了。注意院刁,這個(gè)時(shí)候你的WEB項(xiàng)目還沒有完全啟動(dòng)完成糯钙。這個(gè)動(dòng)作會(huì)比所有的Servlet都要早。
換句話說,這個(gè)時(shí)候,你對 < context-param > 中的鍵值做的操作退腥,將在你的WEB項(xiàng)目完全啟動(dòng)之前被執(zhí)行任岸。
舉例.你可能想在項(xiàng)目啟動(dòng)之前就打開數(shù)據(jù)庫。那么這里就可以在< context-param >中設(shè)置數(shù)據(jù)庫的連接方式狡刘,在監(jiān)聽類中初始化數(shù)據(jù)庫的連接享潜。
補(bǔ)充知識(shí):ServletContext,是一個(gè)全局的儲(chǔ)存信息的空間,服務(wù)器開始嗅蔬,其就存在剑按,服務(wù)器關(guān)閉疾就,其才釋放。request艺蝴,一個(gè)用戶可有多個(gè)猬腰;session,一個(gè)用戶一個(gè)猜敢;而servletContext姑荷,所有用戶共用一個(gè)。所以缩擂,為了節(jié)省空間鼠冕,提高效率,ServletContext中胯盯,要放必須的供鸠、重要的、所有用戶需要共享的線程又是安全的一些信息陨闹。例如楞捂,一個(gè)購物網(wǎng)站,用戶要訪問商品的詳細(xì)信息趋厉,如果放在session域寨闹,每個(gè)用戶都要訪問一遍數(shù)據(jù)庫,這樣效率太低君账;而放在ServletContext中繁堡,服務(wù)器一啟動(dòng),就訪問數(shù)據(jù)庫將商品信息放入數(shù)據(jù)庫乡数,這樣所有用戶只需要通過上下文就能訪問到商品的信息椭蹄。
三、web.xml節(jié)點(diǎn)加載順序:
web.xml節(jié)點(diǎn)的加載順序與它們在web.xml中位置的先后無關(guān)净赴,即不會(huì)因?yàn)?lt; filter >寫在< context-param >前面就先加載< filter >绳矩。
上文也提到到了,< context-param >用于對ServletContext提供鍵值對玖翅,即應(yīng)用程序的上下文信息翼馆。而listener、servlet等節(jié)點(diǎn)在初始化的過程中會(huì)使用到這些上下文信息金度,所以最后我們得出web.xml節(jié)點(diǎn)的加載順序應(yīng)該為:context-param->listener->filter->servlet应媚。
對于某類配置節(jié)點(diǎn)而言,位置的先后是有要求的猜极。以servlet舉例中姜,與servlet相關(guān)的配置節(jié)點(diǎn)是servlet-mapping,對于擁有相同配置節(jié)servlet-name的servlet和servlet-mapping來說跟伏,servlet-mapping必須在servlet后定義丢胚,否則當(dāng)解析到servlet-mapping時(shí)翩瓜,它的servlet-name還沒有定義。web 容器啟動(dòng)時(shí)初始化每個(gè) servlet時(shí)嗜桌,是按照 servlet配置節(jié)出現(xiàn)的順序來初始化的奥溺。
最終結(jié)論: web.xml 的加載順序是:[context-param -> listener -> filter -> servlet -> spring] 辞色,而同類型節(jié)點(diǎn)之間的實(shí)際程序調(diào)用的時(shí)候的順序是根據(jù)對應(yīng)的 mapping 的順序進(jìn)行調(diào)用的骨宠。