web.xml加載順序與web.xml常用節點解析

web.xml加載順序與web.xml常用節點解析


web.xml加載順序

應用服務器啓動時web.xml加載過程,至於這些節點在xml文件中的前後順序沒有關係,不過有些應用服務器,我曾碰到過的 websphere就嚴格要求web.xml的節點順序,否則部署不成功,所以還是贊成按照web.xml標準格式寫
content-param --> listener --> filter --> servlet

1、啓動WEB項目的時候,應用服務器會去讀它的配置文件web.xml.讀兩個節點:<listener></listener> 和 <context-param></context-param>  

2、緊接着,容器創建一個ServletContext(上下文),這個WEB項目所有部分都將共享這個上下文.

3、容器將<context-param></context-param>轉化爲鍵值對,並交給ServletContext.

4、容器創建<listener></listener>中的類實例,即創建監聽.

5、在監聽中會有contextInitialized(ServletContextEvent args)初始化方法,在這個方法中獲得:
 ServletContext = ServletContextEvent.getServletContext();
 context-param的值 = ServletContext.getInitParameter("context-param的鍵");   

6、得到這個context-param的值之後,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啓動完成.這個動作會比所有的Servlet都要早.換句話說,這個時候,你對<context-param>中的鍵值做的操作,將在你的WEB項目完全啓動之前被執行.如果想在項目啓動之前就打開數據庫,那麼這裏就可以在<context-param>中設置數據庫的連接方式,在監聽類中初始化數據庫的連接,這個監聽是自己寫的一個類,除了初始化方法,它還有銷燬方法.用於關閉應用前釋放資源.比如說數據庫連接的關閉.


對於某類配置節而言,與它們出現的順序是有關的
以 filter 爲例,web.xml 中當然可以定義多個 filter,與 filter 相關的一個配置節是 filter-mapping,這裏一定要注意,對於擁有相同 filter-name 的 filter 和 filter-mapping 配置節而言,filter-mapping 必須出現在 filter 之後,否則當解析到 filter-mapping 時,它所對應的 filter-name 還未定義。
web 容器啓動時初始化每個 filter 時,是按照 filter 配置節出現的順序來初始化的,當請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping 配置節出現的順序來依次調用 doFilter() 方法的。
servlet 同 filter 類似,此處不再贅述。

比如filter 需要用到 bean ,但加載順序是: 先加載filter 後加載spring,則filter中初始化操作中的bean爲null;所以,如果過濾器中要使用到 bean,可以將spring 的加載 改成 Listener的方式
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 

web.xml節點解析

根節點

1、
<web-app></web-app>

常用節點介紹
2、 <context-param />  用來設定web站臺的環境參數
 它包含兩個子元素:
 <param-name></param-name> 用來指定參數的名稱
 <param-value></param-value> 用來設定參數值
 在此設定的參數,可以在servlet中用 getServletContext().getInitParameter("my_param") 來取得
 例子:
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath*:/log4j.properties</param-value>
 </context-param>

3、 <listener /> 用來設定Listener接口
 它的主要子元素爲
 <listener-class></listener-class> 定義Listener的類名稱
 例子:
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

4、 <filter />  是用來聲明filter的相關設定
 <filter-name></filter-name> 這當然就是指定filter的名字
 <filter-class></filter-class> 這是用來定義filter的類的名稱
 <init-param></init-param> 用來定義參數,它有兩個子元素:
 <param-name></param-name> 用來指定參數的名稱
 <param-value></param-value> 用來設定參數值
 例子:
 <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
  </init-param>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>

5、 <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></servlet>一起使用的是
 <servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素:
 <servlet-name></servlet-name> 指定servlet的名稱
 <url-pattern></url-pattern> 指定servlet所對應的URL
 <servlet>
  <servlet-name>DemoServlet</servlet-name>
  <servlet-class>com.test.DemoServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>DemoServlet</servlet-name>
  <url-pattern>/demoServlet</url-pattern>
 </servlet-mapping>

基本節點:

6、 <description/> 是對站臺的描述
 例子:<description>傳道、授業、解惑</description>

7、 <display-name/> 定義站臺的名稱
 例子:<display-name>我的站點</display-name>

8、 <icon>
icon元素包含small-icon和large-icon兩個子元素.用來指定web站臺中小圖標和大圖標的路徑.
<small-icon>/路徑/smallicon.gif</small-icon>
small-icon元素應指向web站臺中某個小圖標的路徑,大小爲16 X 16 pixel,但是圖象文件必須爲GIF或JPEG格式,擴展名必須爲:.gif或.jpg.
<large-icon>/路徑/largeicon-jpg</large-icon>
large-icon元素應指向web站臺中某個大圖表路徑,大小爲32 X 32 pixel,但是圖象文件必須爲GIF或JPEG的格式,擴展名必須爲; gif或jpg.
例子:
<icon>
 <small-icon>/images/small.gif</small-icon>
 <large-icon>/images/large.gir</large-icon>
</icon>

9、 <distributable/> 是指定該站臺是否可分佈式處理

10、 <session-config/> 用來定義web站臺中的session參數
 包含一個子元素:
 <session-timeout></session-timeout> 用來定義這個web站臺所有session的有效期限,單位爲 分鐘

11、 <mime-mapping /> 定義某一個擴展名和某一個MIME Type做對應該
 包含兩個子元素:
 <extension></extension> 擴展名的名稱
 <mime-type></mime-type> MIME格式
 例子:
 <mime-mapping>
  <extension>doc</extension> 
  <mime-type>application/vnd.ms-word</mime-type>
 </mime-mapping>   
 <mime-mapping>
  <extension>xls</extension>
  <mime-type>application/vnd.ms-excel</mime-type>
 </mime-mapping>

12、 <error-page>
 <error-page>
     <error-code>500</error-code>
     <location>/message.jsp</location>
 </error-page>
 <error-page>
     <error-code>400</error-code>
     <location>/message.jsp</location>
 </error-page>
 <error-page>
     <error-code>403</error-code>
     <location>/message.jsp</location>
 </error-page>
 <error-page>
     <error-code>404</error-code>
     <location>/message.jsp</location>
 </error-page>
 <error-page>
     <error-code>502</error-code>
     <location>/index.jsp</location>
 </error-page>

13、
<jsp-config/>
 <jsp-config>
 <taglib>
 <taglib-uri>/struts-tags</taglib-uri>
 <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/struts-dojo-tags</taglib-uri>
 <taglib-location>/WEB-INF/struts-dojo-tags.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/s</taglib-uri>
 <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
 </taglib>
 </jsp-config>

14、 <welcome-file-list/>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

15<resource-ref></resource-ref> 定義利用JNDI取得站臺可利用的資源
 有五個子元素:
 <description></description> 資源說明
 <rec-ref-name></rec-ref-name> 資源名稱
 <res-type></res-type> 資源種類
 <res-auth></res-auth> 資源經由Application或Container來許可
 <res-sharing-scope></res-sharing-scope> 資源是否可以共享,有Shareable和Unshareable兩個值,默認爲Shareable

 比如,配置數據庫連接池就可在此配置
 <resource-ref>
 <description>JNDI JDBC DataSource of shop</description>
 <res-ref-name>jdbc/sample_db</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
 </resource-ref>

還有其他元素如果想更爲詳細的瞭解它可以到http://java.sun.com/xml/ns/j2ee/web-mapp_2_4.xsd網頁


發佈了63 篇原創文章 · 獲贊 67 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章