濮阳杆衣贸易有限公司

主頁 > 知識庫 > 關(guān)于JSP配置文件web.xml加載順序詳解

關(guān)于JSP配置文件web.xml加載順序詳解

熱門標(biāo)簽:汝南縣地圖標(biāo)注app 湖北地圖標(biāo)注公司 甘肅銷售電銷機器人公司 地圖標(biāo)注專業(yè)和非專業(yè) 智能電話機器人銷售話術(shù) 外呼直播語音系統(tǒng) 福建電銷貓機器人收費 四川正規(guī)外呼系統(tǒng)軟件 山東ai外呼電銷機器人好用嗎

一、

1、啟動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,讀取context-param>和listener>兩個結(jié)點。

2、緊急著,容創(chuàng)建一個ServletContext(servlet上下文),這個web項目的所有部分都將共享這個上下文。

3、容器將context-param>轉(zhuǎn)換為鍵值對,并交給servletContext。

4、容器創(chuàng)建listener>中的類實例,創(chuàng)建監(jiān)聽器。

二、

load-on-startup 元素在web應(yīng)用啟動的時候指定了servlet被加載的順序,它的值必須是一個整數(shù)。如果它的值是一個負整數(shù)或是這個元素不存在,那么容器會在該servlet被調(diào)用的時候,加載這個servlet 。如果值是正整數(shù)或零,容器在配置的時候就加載并初始化這個servlet,容器必須保證值小的先被加載。如果值相等,容器可以自動選擇先加載誰。

在servlet的配置當(dāng)中,load-on-startup>5/load-on-startup>的含義是:

標(biāo)記容器是否在啟動的時候就加載這個servlet。

當(dāng)值為0或者大于0時,表示容器在應(yīng)用啟動時就加載這個servlet;

當(dāng)是一個負數(shù)時或者沒有指定時,則指示容器在該servlet被選擇時才加載。

正數(shù)的值越小,啟動該servlet的優(yōu)先級越高。

三、

在項目中總會遇到一些關(guān)于加載的優(yōu)先級問題,近期也同樣遇到過類似的,所以自己查找資料總結(jié)了下,下面有些是轉(zhuǎn)載其他人的,畢竟人家寫的不錯,自己也就不重復(fù)造輪子了,只是略加點了自己的修飾。

首先可以肯定的是,加載順序與它們在 web.xml 文件中的先后順序無關(guān)。即不會因為 filter 寫在 listener 的前面而會先加載 filter。最終得出的

結(jié)論是:listener –> filter –> servlet

同時還存在著這樣一種配置節(jié):context-param,它用于向 ServletContext 提供鍵值對,即應(yīng)用程序上下文信息。我們的 listener, filter 等在初始化時會用到這些上下文中的信息,那么 context-param 配置節(jié)是不是應(yīng)該寫在 listener 配置節(jié)前呢?實際上 context-param 配置節(jié)可寫在任意位置,因此真正的加載順序為:

context-param –> listener –> filter –> servlet

 

對于某類配置節(jié)而言,與它們出現(xiàn)的順序是有關(guān)的。以 filter 為例,web.xml 中當(dāng)然可以定義多個 filter,與 filter 相關(guān)的一個配置節(jié)是 filter-mapping,這里一定要注意,對于擁有相同 filter-name 的 filter 和 filter-mapping 配置節(jié)而言,filter-mapping 必須出現(xiàn)在 filter 之后,否則當(dāng)解析到 filter-mapping 時,它所對應(yīng)的 filter-name 還未定義。web 容器啟動時初始化每個 filter 時,是按照 filter 配置節(jié)出現(xiàn)的順序來初始化的,當(dāng)請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping 配置節(jié)出現(xiàn)的順序來依次調(diào)用 doFilter() 方法的。

servlet 同 filter 類似 ,此處不再贅述。

由此,可以看出,web.xml 的加載順序是:context-param -> listener -> filter -> servlet ,而同個類型之間的實際程序調(diào)用的時候的順序是根據(jù)對應(yīng)的 mapping 的順序進行調(diào)用的。

 

web.xml文件詳解

Web.xml常用元素

復(fù)制代碼 代碼如下:

web-app> 
     display-name>/display-name> 定義了WEB應(yīng)用的名字 
     description>/description> 聲明WEB應(yīng)用的描述信息 
     context-param>/context-param> context-param元素聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。 
     filter>/filter> 過濾器元素將一個名字與一個實現(xiàn)javax.servlet.Filter接口的類相關(guān)聯(lián)。 
     filter-mapping>/filter-mapping> 一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關(guān)聯(lián)。 
     listener>/listener> servlet API的版本2.3增加了對事件監(jiān)聽程序的支持,事件監(jiān)聽程序在建立、修改和刪除會話或servlet環(huán)境時得到通知。Listener元素指出事件監(jiān)聽程序類。 
     servlet>/servlet> 在向servlet或JSP頁面制定初始化參數(shù)或定制URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務(wù)的。 
     servlet-mapping>/servlet-mapping> 服務(wù)器一般為servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。但是,常常會更改這個URL,以便servlet可以訪問初始化參數(shù)或更容易地處理相對URL。在更改缺省URL時,使用servlet-mapping元素。 
     session-config>/session-config> 如果某個會話在一定時間內(nèi)未被訪問,服務(wù)器可以拋棄它以節(jié)省內(nèi)存。可通過使用HttpSession的setMaxInactiveInterval方法明確設(shè)置單個會話對象的超時值,或者可利用session-config元素制定缺省超時值。 
     mime-mapping>/mime-mapping> 如果Web應(yīng)用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。 
     welcome-file-list>/welcome-file-list> 指示服務(wù)器在收到引用一個目錄名而不是文件名的URL時,使用哪個文件。 
     error-page>/error-page> 在返回特定HTTP狀態(tài)代碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面。 
     taglib>/taglib> 對標(biāo)記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置,而不用編輯使用這些文件的JSP頁面。 
     resource-env-ref>/resource-env-ref> 聲明與資源相關(guān)的一個管理對象。 
     resource-ref>/resource-ref> 聲明一個資源工廠使用的外部資源。 
     security-constraint>/security-constraint> 制定應(yīng)該保護的URL。它與login-config元素聯(lián)合使用 
     login-config>/login-config> 指定服務(wù)器應(yīng)該怎樣給試圖訪問受保護頁面的用戶授權(quán)。它與sercurity-constraint元素聯(lián)合使用。 
     security-role>/security-role> 給出安全角色的一個列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。分別地聲明角色可使高級IDE處理安全信息更為容易。 
     env-entry>/env-entry> 聲明Web應(yīng)用的環(huán)境項。 
     ejb-ref>/ejb-ref> 聲明一個EJB的主目錄的引用。 
     ejb-local-ref>/ejb-local-ref> 聲明一個EJB的本地主目錄的應(yīng)用。 
 /web-app>

相應(yīng)元素配置

1、Web應(yīng)用圖標(biāo):指出IDE和GUI工具用來表示W(wǎng)eb應(yīng)用的大圖標(biāo)和小圖標(biāo)

復(fù)制代碼 代碼如下:

icon> 
     small-icon>/images/app_small.gif/small-icon> 
     large-icon>/images/app_large.gif/large-icon> 
 /icon>


2、Web 應(yīng)用名稱:提供GUI工具可能會用來標(biāo)記這個特定的Web應(yīng)用的一個名稱
復(fù)制代碼 代碼如下:

display-name>Tomcat Example/display-name>    

3、Web 應(yīng)用描述:給出于此相關(guān)的說明性文本
復(fù)制代碼 代碼如下:

disciption>Tomcat Example servlets and JSP pages./disciption>

4、上下文參數(shù):聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。
復(fù)制代碼 代碼如下:

context-param>
     param-name>ContextParameter/para-name>
     param-value>test/param-value>
     description>It is a test parameter./description>
 /context-param>

在servlet里面可以通過 getServletContext().getInitParameter(“context/param”)得到
5、過濾器配置:將一個名字與一個實現(xiàn)javaxs.servlet.Filter接口的類相關(guān)聯(lián)。
復(fù)制代碼 代碼如下:

filter>
     filter-name>setCharacterEncoding/filter-name>
     filter-class>com.myTest.setCharacterEncodingFilter/filter-class>
     init-param>
         param-name>encoding/param-name>
         param-value>GB2312/param-value>
     /init-param>
 /filter>
 filter-mapping>
     filter-name>setCharacterEncoding/filter-name>
     url-pattern>/*/url-pattern>
 /filter-mapping>

6、監(jiān)聽器配置
復(fù)制代碼 代碼如下:

listener>
     listerner-class>listener.SessionListener/listener-class>
 /listener>

7、Servlet配置
基本配置
復(fù)制代碼 代碼如下:

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>

高級配置
復(fù)制代碼 代碼如下:

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>

元素說明
servlet>/servlet> 用來聲明一個servlet的數(shù)據(jù),主要有以下子元素:
servlet-name>/servlet-name> 指定servlet的名稱
servlet-class>/servlet-class> 指定servlet的類名稱
jsp-file>/jsp-file> 指定web站臺中的某個JSP網(wǎng)頁的完整路徑
init-param>/init-param> 用來定義參數(shù),可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數(shù)
load-on-startup>/load-on-startup>指定當(dāng)Web應(yīng)用啟動時,裝載Servlet的次序。
當(dāng)值為正數(shù)或零時:Servlet容器先加載數(shù)值小的servlet,再依次加載其他數(shù)值大的servlet.
當(dāng)值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它
servlet-mapping>/servlet-mapping> 用來定義servlet所對應(yīng)的URL,包含兩個子元素
servlet-name>/servlet-name> 指定servlet的名稱
url-pattern>/url-pattern> 指定servlet所對應(yīng)的URL
8、會話超時配置(單位為分鐘)
復(fù)制代碼 代碼如下:

 session-config>
     session-timeout>120/session-timeout>
 /session-config>

9、MIME類型配置
復(fù)制代碼 代碼如下:

 mime-mapping>
     extension>htm/extension>
     mime-type>text/html/mime-type>
 /mime-mapping>

10、指定歡迎文件頁配置
復(fù)制代碼 代碼如下:

 welcome-file-list>
     welcome-file>index.jsp/welcome-file>
     welcome-file>index.html/welcome-file>
     welcome-file>index.htm/welcome-file>
 /welcome-file-list>

11、配置錯誤頁面
一、 通過錯誤碼來配置error-page
復(fù)制代碼 代碼如下:

 error-page>
     error-code>404/error-code>
     location>/NotFound.jsp/location>
 /error-page>

上面配置了當(dāng)系統(tǒng)發(fā)生404錯誤時,跳轉(zhuǎn)到錯誤處理頁面NotFound.jsp。
二、通過異常的類型配置error-page
復(fù)制代碼 代碼如下:

 error-page>
     exception-type>java.lang.NullException/exception-type>
     location>/error.jsp/location>
 /error-page>

上面配置了當(dāng)系統(tǒng)發(fā)生java.lang.NullException(即空指針異常)時,跳轉(zhuǎn)到錯誤處理頁面error.jsp
12、TLD配置
復(fù)制代碼 代碼如下:

 taglib>
     taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib/taglib-uri>
     taglib-location>/WEB-INF/jsp/debug-taglib.tld/taglib-location>
 /taglib>

如果MyEclipse一直在報錯,應(yīng)該把taglib> 放到 jsp-config>中
view source
復(fù)制代碼 代碼如下:

 jsp-config>
     taglib>
         taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib/taglib-uri>
         taglib-location>/WEB-INF/pager-taglib.tld/taglib-location>
     /taglib>
 /jsp-config>

13、資源管理對象配置
復(fù)制代碼 代碼如下:

 resource-env-ref>
     resource-env-ref-name>jms/StockQueue/resource-env-ref-name>
 /resource-env-ref>

14、資源工廠配置
復(fù)制代碼 代碼如下:

 resource-ref>
     res-ref-name>mail/Session/res-ref-name>
     res-type>javax.mail.Session/res-type>
     res-auth>Container/res-auth>
 /resource-ref>

配置數(shù)據(jù)庫連接池就可在此配置:
復(fù)制代碼 代碼如下:

 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>

15、安全限制配置
復(fù)制代碼 代碼如下:

 security-constraint>
     display-name>Example Security Constraint/display-name>
     web-resource-collection>
         web-resource-name>Protected Area/web-resource-name>
         url-pattern>/jsp/security/protected/*/url-pattern>
         http-method>DELETE/http-method>
         http-method>GET/http-method>
         http-method>POST/http-method>
         http-method>PUT/http-method>
     /web-resource-collection>
     auth-constraint>
         role-name>tomcat/role-name>
         role-name>role1/role-name>
     /auth-constraint>
 /security-constraint>

16、登陸驗證配置
復(fù)制代碼 代碼如下:

 login-config>
     auth-method>FORM/auth-method>
     realm-name>Example-Based Authentiation Area/realm-name>
     form-login-config>
         form-login-page>/jsp/security/protected/login.jsp/form-login-page>
         form-error-page>/jsp/security/protected/error.jsp/form-error-page>
     /form-login-config>
 /login-config>

17、安全角色:security-role元素給出安全角色的一個列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。
分別地聲明角色可使高級IDE處理安全信息更為容易。
復(fù)制代碼 代碼如下:

 security-role>
     role-name>tomcat/role-name>
 /security-role>

18、Web環(huán)境參數(shù):env-entry元素聲明Web應(yīng)用的環(huán)境項
復(fù)制代碼 代碼如下:

 env-entry>
     env-entry-name>minExemptions/env-entry-name>
     env-entry-value>1/env-entry-value>
     env-entry-type>java.lang.Integer/env-entry-type>
 /env-entry>

19、EJB 聲明
復(fù)制代碼 代碼如下:

 ejb-ref>
     description>Example EJB reference/decription>
     ejb-ref-name>ejb/Account/ejb-ref-name>
     ejb-ref-type>Entity/ejb-ref-type>
     home>com.mycompany.mypackage.AccountHome/home>
     remote>com.mycompany.mypackage.Account/remote>
 /ejb-ref>

20、本地EJB聲明
復(fù)制代碼 代碼如下:

 ejb-local-ref>
     description>Example Loacal EJB reference/decription>
     ejb-ref-name>ejb/ProcessOrder/ejb-ref-name>
     ejb-ref-type>Session/ejb-ref-type>
     local-home>com.mycompany.mypackage.ProcessOrderHome/local-home>
     local>com.mycompany.mypackage.ProcessOrder/local>
 /ejb-local-ref>

21、配置DWR
復(fù)制代碼 代碼如下:

 servlet>
     servlet-name>dwr-invoker/servlet-name>
     servlet-class>uk.ltd.getahead.dwr.DWRServlet/servlet-class>
 /servlet>
 servlet-mapping>
     servlet-name>dwr-invoker/servlet-name>
     url-pattern>/dwr/*/url-pattern>
 /servlet-mapping>

22、配置Struts
復(fù)制代碼 代碼如下:

 display-name>Struts Blank Application/display-name>
 servlet>
     servlet-name>action/servlet-name>
     servlet-class>org.apache.struts.action.ActionServlet/servlet-class>
     init-param>
         param-name>detail/param-name>
         param-value>2/param-value>
     /init-param>
     init-param>
         param-name>debug/param-name>
         param-value>2/param-value>
     /init-param>
     init-param>
         param-name>config/param-name>
         param-value>/WEB-INF/struts-config.xml/param-value>
     /init-param>
     init-param>
         param-name>application/param-name>
         param-value>ApplicationResources/param-value>
     /init-param>
     load-on-startup>2/load-on-startup>
 /servlet>
 servlet-mapping>
     servlet-name>action/servlet-name>
     url-pattern>*.do/url-pattern>
 /servlet-mapping>
 welcome-file-list>
     welcome-file>index.jsp/welcome-file>
 /welcome-file-list>

 !-- Struts Tag Library Descriptors -->
 taglib>
     taglib-uri>struts-bean/taglib-uri>
     taglib-location>/WEB-INF/tld/struts-bean.tld/taglib-location>
 /taglib>
 taglib>
     taglib-uri>struts-html/taglib-uri>
     taglib-location>/WEB-INF/tld/struts-html.tld/taglib-location>
 /taglib>
 taglib>
     taglib-uri>struts-nested/taglib-uri>
     taglib-location>/WEB-INF/tld/struts-nested.tld/taglib-location>
 /taglib>
 taglib>
     taglib-uri>struts-logic/taglib-uri>
     taglib-location>/WEB-INF/tld/struts-logic.tld/taglib-location>
 /taglib>
 taglib>
     taglib-uri>struts-tiles/taglib-uri>
     taglib-location>/WEB-INF/tld/struts-tiles.tld/taglib-location>
 /taglib>

23、配置Spring(基本上都是在Struts中配置的)
復(fù)制代碼 代碼如下:

 !-- 指定spring配置文件位置 -->
 context-param>
     param-name>contextConfigLocation/param-name>
     param-value>
         !--加載多個spring配置文件 -->
         /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
     /param-value>
 /context-param>

 !-- 定義SPRING監(jiān)聽器,加載spring -->
 listener>
     listener-class>org.springframework.web.context.ContextLoaderListener/listener-class>
 /listener>

 listener>
     listener-class>org.springframework.web.context.request.RequestContextListener/listener-class>
 /listener>



您可能感興趣的文章:
  • web.xml中如何設(shè)置配置文件的加載路徑實例詳解

標(biāo)簽:肇慶 吳忠 昌都 梅州 臨沂 黔東 南充 白銀

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《關(guān)于JSP配置文件web.xml加載順序詳解》,本文關(guān)鍵詞  關(guān)于,JSP,配置文件,web.xml,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《關(guān)于JSP配置文件web.xml加載順序詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于關(guān)于JSP配置文件web.xml加載順序詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    宣汉县| 宁蒗| 东兰县| 龙泉市| 铜山县| 寻甸| 青海省| 太和县| 遵义市| 内黄县| 龙胜| 巢湖市| 迁西县| 合江县| 甘德县| 乐陵市| 谢通门县| 左贡县| 乌鲁木齐市| 丹寨县| 赫章县| 拉萨市| 改则县| 宁陕县| 开平市| 阳西县| 林口县| 鲜城| 彩票| 牟定县| 衡阳县| 如东县| 二手房| 丹巴县| 勃利县| 延川县| 镇平县| 宜兰县| 克什克腾旗| 稻城县| 凯里市|