一、web.xml配置節(jié)點(diǎn)簡介
(1) context-param
格式定義
1
2
3
4
|
<context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring/spring-mybatis.xml</param-value> </context-param> |
作用:
- 該元素用來聲明應(yīng)用范圍(整個web項目)內(nèi)的上下文初始化參數(shù)。
- param-name 設(shè)定上下文的參數(shù)名稱。必須是唯一名稱
- param-value 設(shè)定的參數(shù)名稱的值,這里的例子是指定spring配置文件的位置
(2) listener
格式定義
1
2
3
4
|
//listen-class 指定監(jiān)聽類,該類繼承servletcontextlistener 包含初始化方法contextinitialized(servletcontextevent event) 和銷毀方法contextdestoryed(servletcontextevent event) <listener> <listener- class >org.springframework.web.context.contextloaderlistener</listener- class > </listener> |
作用:該元素用來注冊一個監(jiān)聽器類。可以收到事件什么時候發(fā)生以及用什么作為響應(yīng)的通知。事件監(jiān)聽程序在建立、修改和刪除會話或servlet環(huán)境時得到通知。常與context-param聯(lián)合使用。
(3) filter
格式定義
1
2
3
4
5
6
7
8
9
10
11
12
|
<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能夠在一個請求到達(dá)servlet之前預(yù)處理用戶請求,也可以在離開servlet時處理http響應(yīng);在執(zhí)行servlet之前,首先執(zhí)行filter程序,并為之做一些預(yù)處理工作;根據(jù)程序需要修改請求和響應(yīng);在servlet被調(diào)用之后截獲servlet的執(zhí)行。
(4)servlet
- 格式定義
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//配置spring mvc,指定處理請求的servlet,有兩種方式: //1. 默認(rèn)查找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> |
作用:
- 創(chuàng)建并返回一個包含基于客戶請求性質(zhì)的動態(tài)內(nèi)容的完整的html頁面;
- 創(chuàng)建可嵌入到現(xiàn)有的html頁面中的一部分html頁面(html片段);
- 讀取客戶端發(fā)來的隱藏數(shù)據(jù);
- 讀取客戶端發(fā)來的顯示數(shù)據(jù);
- 與其他服務(wù)器資源(包括數(shù)據(jù)庫和java的應(yīng)用程序)進(jìn)行通信;
二、 web.xml加載過程(步驟):
- 啟動web項目,容器(如tomcat、apache)會去讀取它的配置文件web.xml 中的兩個節(jié)點(diǎn),context-param和listener。
- 緊接著,容器將創(chuàng)建一個servletcontext(又稱為:servlet上下文),應(yīng)用范圍內(nèi)即整個web項目都能使用這個servlet上下文。
- 容器將< context-param >轉(zhuǎn)化為鍵值對,并交給servletcontext。
- 容器創(chuàng)建< listener >中的類實(shí)例,即創(chuàng)建監(jiān)聽。(備注:listener定義的類可以是自定義的類但必須需要繼承servletcontextlistener)。
- 在監(jiān)聽中會有contextinitialized(servletcontextevent args)初始化方法,在這個方法中獲得:servletcontext = servletcontextevent.getservletcontext(); context-param的值 = servletcontext.getinitparameter(“context-param的鍵”); 在這個類中還必須有一個contextdestroyed(servletcontextevent event) 銷毀方法。用于關(guān)閉應(yīng)用前釋放資源,比如說數(shù)據(jù)庫連接的關(guān)閉。
- 得到這個context-param的值之后,你就可以做一些操作了。注意,這個時候你的web項目還沒有完全啟動完成。這個動作會比所有的servlet都要早。換句話說,這個時候,你對 < context-param > 中的鍵值做的操作,將在你的web項目完全啟動之前被執(zhí)行。
- 舉例.你可能想在項目啟動之前就打開數(shù)據(jù)庫。那么這里就可以在< context-param >中設(shè)置數(shù)據(jù)庫的連接方式,在監(jiān)聽類中初始化數(shù)據(jù)庫的連接。
補(bǔ)充知識:servletcontext,是一個全局的儲存信息的空間,服務(wù)器開始,其就存在,服務(wù)器關(guān)閉,其才釋放。request,一個用戶可有多個;session,一個用戶一個;而servletcontext,所有用戶共用一個。所以,為了節(jié)省空間,提高效率,servletcontext中,要放必須的、重要的、所有用戶需要共享的線程又是安全的一些信息。例如,一個購物網(wǎng)站,用戶要訪問商品的詳細(xì)信息,如果放在session域,每個用戶都要訪問一遍數(shù)據(jù)庫,這樣效率太低;而放在servletcontext中,服務(wù)器一啟動,就訪問數(shù)據(jù)庫將商品信息放入數(shù)據(jù)庫,這樣所有用戶只需要通過上下文就能訪問到商品的信息。
三、web.xml節(jié)點(diǎn)加載順序:
- web.xml節(jié)點(diǎn)的加載順序與它們在web.xml中位置的先后無關(guān),即不會因?yàn)?lt; filter >寫在< context-param >前面就先加載< filter >。
- 上文也提到到了,< context-param >用于對servletcontext提供鍵值對,即應(yīng)用程序的上下文信息。而listener、servlet等節(jié)點(diǎn)在初始化的過程中會使用到這些上下文信息,所以最后我們得出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時,它的servlet-name還沒有定義。web 容器啟動時初始化每個 servlet時,是按照 servlet配置節(jié)出現(xiàn)的順序來初始化的。
- 最終結(jié)論: web.xml 的加載順序是:[context-param -> listener -> filter -> servlet -> spring] ,而同類型節(jié)點(diǎn)之間的實(shí)際程序調(diào)用的時候的順序是根據(jù)對應(yīng)的 mapping 的順序進(jìn)行調(diào)用的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_20805103/article/details/77851996