小菜毛毛技術(shù)分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
          http://zhxing.javaeye.com/blog/399668
          在項(xiàng)目中總會遇到一些關(guān)于加載的優(yōu)先級問題,近期也同樣遇到過類似的,所以自己查找資料總結(jié)了下,下面有些是轉(zhuǎn)載其他人的,畢竟人家寫的不錯(cuò),自己也就不重復(fù)造輪子了,只是略加點(diǎn)了自己的修飾。

                  首先可以肯定的是,加載順序與它們在 web.xml 文件中的先后順序無關(guān)。即不會因?yàn)?filter 寫在 listener 的前面而會先加載 filter。最終得出的結(jié)論是:listener -> filter -> servlet

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

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

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

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

          web.xml文件詳解

          Xml代碼
          1. Web.xml常用元素   
          2. <web-app>   
          3. <display-name></display-name>定義了WEB應(yīng)用的名字   
          4. <description></description> 聲明WEB應(yīng)用的描述信息   
          5.   
          6. <context-param></context-param> context-param元素聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。   
          7. <filter></filter> 過濾器元素將一個(gè)名字與一個(gè)實(shí)現(xiàn)javax.servlet.Filter接口的類相關(guān)聯(lián)。   
          8. <filter-mapping></filter-mapping> 一旦命名了一個(gè)過濾器,就要利用filter-mapping元素把它與一個(gè)或多個(gè)servlet或JSP頁面相關(guān)聯(lián)。   
          9. <listener></listener>servlet API的版本2.3增加了對事件監(jiān)聽程序的支持,事件監(jiān)聽程序在建立、修改和刪除會話或servlet環(huán)境時(shí)得到通知。   
          10.                      Listener元素指出事件監(jiān)聽程序類。   
          11. <servlet></servlet> 在向servlet或JSP頁面制定初始化參數(shù)或定制URL時(shí),必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項(xiàng)任務(wù)的。   
          12. <servlet-mapping></servlet-mapping> 服務(wù)器一般為servlet提供一個(gè)缺省的URL:http://host/webAppPrefix/servlet/ServletName。   
          13.               但是,常常會更改這個(gè)URL,以便servlet可以訪問初始化參數(shù)或更容易地處理相對URL。在更改缺省URL時(shí),使用servlet-mapping元素。   
          14.   
          15. <session-config></session-config> 如果某個(gè)會話在一定時(shí)間內(nèi)未被訪問,服務(wù)器可以拋棄它以節(jié)省內(nèi)存。   
          16.           可通過使用HttpSession的setMaxInactiveInterval方法明確設(shè)置單個(gè)會話對象的超時(shí)值,或者可利用session-config元素制定缺省超時(shí)值。   
          17.   
          18. <mime-mapping></mime-mapping>如果Web應(yīng)用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。   
          19. <welcome-file-list></welcome-file-list> 指示服務(wù)器在收到引用一個(gè)目錄名而不是文件名的URL時(shí),使用哪個(gè)文件。   
          20. <error-page></error-page> 在返回特定HTTP狀態(tài)代碼時(shí),或者特定類型的異常被拋出時(shí),能夠制定將要顯示的頁面。   
          21. <taglib></taglib> 對標(biāo)記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置,   
          22.                   而不用編輯使用這些文件的JSP頁面。   
          23. <resource-env-ref></resource-env-ref>聲明與資源相關(guān)的一個(gè)管理對象。   
          24. <resource-ref></resource-ref> 聲明一個(gè)資源工廠使用的外部資源。   
          25. <security-constraint></security-constraint> 制定應(yīng)該保護(hù)的URL。它與login-config元素聯(lián)合使用   
          26. <login-config></login-config> 指定服務(wù)器應(yīng)該怎樣給試圖訪問受保護(hù)頁面的用戶授權(quán)。它與sercurity-constraint元素聯(lián)合使用。   
          27. <security-role></security-role>給出安全角色的一個(gè)列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素   
          28.                    的role-name子元素中。分別地聲明角色可使高級IDE處理安全信息更為容易。   
          29. <env-entry></env-entry>聲明Web應(yīng)用的環(huán)境項(xiàng)。   
          30. <ejb-ref></ejb-ref>聲明一個(gè)EJB的主目錄的引用。   
          31. < ejb-local-ref></ ejb-local-ref>聲明一個(gè)EJB的本地主目錄的應(yīng)用。   
          32. </web-app>   
          33.   
          34.   
          35. 相應(yīng)元素配置   
          36.   
          37. 1、Web應(yīng)用圖標(biāo):指出IDE和GUI工具用來表示W(wǎng)eb應(yīng)用的大圖標(biāo)和小圖標(biāo)   
          38. <icon>   
          39. <small-icon>/images/app_small.gif</small-icon>   
          40. <large-icon>/images/app_large.gif</large-icon>   
          41. </icon>   
          42. 2、Web 應(yīng)用名稱:提供GUI工具可能會用來標(biāo)記這個(gè)特定的Web應(yīng)用的一個(gè)名稱   
          43. <display-name>Tomcat Example</display-name>   
          44. 3、Web 應(yīng)用描述: 給出于此相關(guān)的說明性文本   
          45. <disciption>Tomcat Example servlets and JSP pages.</disciption>   
          46. 4、上下文參數(shù):聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。   
          47.   <context-param>   
          48.     <param-name>ContextParameter</para-name>   
          49.     <param-value>test</param-value>   
          50.     <description>It is a test parameter.</description>   
          51.   </context-param>   
          52.   在servlet里面可以通過getServletContext().getInitParameter("context/param")得到   
          53.   
          54. 5、過濾器配置:將一個(gè)名字與一個(gè)實(shí)現(xiàn)javaxs.servlet.Filter接口的類相關(guān)聯(lián)。   
          55.   <filter>   
          56.         <filter-name>setCharacterEncoding</filter-name>   
          57.         <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>   
          58.         <init-param>   
          59.             <param-name>encoding</param-name>   
          60.             <param-value>GB2312</param-value>   
          61.         </init-param>   
          62.   </filter>   
          63.   <filter-mapping>   
          64.         <filter-name>setCharacterEncoding</filter-name>   
          65.         <url-pattern>/*</url-pattern>   
          66.   </filter-mapping>   
          67. 6、監(jiān)聽器配置   
          68.   <listener>   
          69.       <listerner-class>listener.SessionListener</listener-class>   
          70.   </listener>   
          71. 7、Servlet配置   
          72.    基本配置   
          73.    <servlet>   
          74.       <servlet-name>snoop</servlet-name>   
          75.       <servlet-class>SnoopServlet</servlet-class>   
          76.    </servlet>   
          77.    <servlet-mapping>   
          78.       <servlet-name>snoop</servlet-name>   
          79.       <url-pattern>/snoop</url-pattern>   
          80.    </servlet-mapping>   
          81.    高級配置   
          82.    <servlet>   
          83.       <servlet-name>snoop</servlet-name>   
          84.       <servlet-class>SnoopServlet</servlet-class>   
          85.       <init-param>   
          86.          <param-name>foo</param-name>   
          87.          <param-value>bar</param-value>   
          88.       </init-param>   
          89.       <run-as>   
          90.          <description>Security role for anonymous access</description>   
          91.          <role-name>tomcat</role-name>   
          92.       </run-as>   
          93.    </servlet>   
          94.    <servlet-mapping>   
          95.       <servlet-name>snoop</servlet-name>   
          96.       <url-pattern>/snoop</url-pattern>   
          97.    </servlet-mapping>   
          98.    元素說明   
          99.      <servlet></servlet> 用來聲明一個(gè)servlet的數(shù)據(jù),主要有以下子元素:   
          100.      <servlet-name></servlet-name> 指定servlet的名稱   
          101.      <servlet-class></servlet-class> 指定servlet的類名稱   
          102.      <jsp-file></jsp-file> 指定web站臺中的某個(gè)JSP網(wǎng)頁的完整路徑   
          103.      <init-param></init-param> 用來定義參數(shù),可有多個(gè)init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數(shù)   
          104.      <load-on-startup></load-on-startup>指定當(dāng)Web應(yīng)用啟動(dòng)時(shí),裝載Servlet的次序。   
          105.                                  當(dāng)值為正數(shù)或零時(shí):Servlet容器先加載數(shù)值小的servlet,再依次加載其他數(shù)值大的servlet.   
          106.                                  當(dāng)值為負(fù)或未定義:Servlet容器將在Web客戶首次訪問這個(gè)servlet時(shí)加載它   
          107.      <servlet-mapping></servlet-mapping> 用來定義servlet所對應(yīng)的URL,包含兩個(gè)子元素   
          108.        <servlet-name></servlet-name> 指定servlet的名稱   
          109.        <url-pattern></url-pattern> 指定servlet所對應(yīng)的URL   
          110. 8、會話超時(shí)配置(單位為分鐘)   
          111.    <session-config>   
          112.       <session-timeout>120</session-timeout>   
          113.    </session-config>   
          114. 9、MIME類型配置   
          115.    <mime-mapping>   
          116.       <extension>htm</extension>   
          117.       <mime-type>text/html</mime-type>   
          118.    </mime-mapping>   
          119. 10、指定歡迎文件頁配置   
          120.    <welcome-file-list>   
          121.       <welcome-file>index.jsp</welcome-file>   
          122.       <welcome-file>index.html</welcome-file>   
          123.       <welcome-file>index.htm</welcome-file>   
          124.    </welcome-file-list>   
          125. 11、配置錯(cuò)誤頁面   
          126.   一、 通過錯(cuò)誤碼來配置error-page   
          127.    <error-page>   
          128.       <error-code>404</error-code>   
          129.       <location>/NotFound.jsp</location>   
          130.    </error-page>   
          131.   上面配置了當(dāng)系統(tǒng)發(fā)生404錯(cuò)誤時(shí),跳轉(zhuǎn)到錯(cuò)誤處理頁面NotFound.jsp。   
          132. 二、通過異常的類型配置error-page   
          133.    <error-page>   
          134.        <exception-type>java.lang.NullException</exception-type>   
          135.        <location>/error.jsp</location>   
          136.    </error-page>   
          137.   上面配置了當(dāng)系統(tǒng)發(fā)生java.lang.NullException(即空指針異常)時(shí),跳轉(zhuǎn)到錯(cuò)誤處理頁面error.jsp   
          138. 12、TLD配置   
          139.    <taglib>   
          140.        <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>   
          141.        <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>   
          142.    </taglib>   
          143.    如果MyEclipse一直在報(bào)錯(cuò),應(yīng)該把<taglib> 放到 <jsp-config>中   
          144.    <jsp-config>   
          145.       <taglib>   
          146.           <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>   
          147.           <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>   
          148.       </taglib>   
          149.    </jsp-config>   
          150. 13、資源管理對象配置   
          151.    <resource-env-ref>   
          152.        <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>   
          153.    </resource-env-ref>   
          154. 14、資源工廠配置   
          155.    <resource-ref>   
          156.        <res-ref-name>mail/Session</res-ref-name>   
          157.        <res-type>javax.mail.Session</res-type>   
          158.        <res-auth>Container</res-auth>   
          159.    </resource-ref>   
          160.    配置數(shù)據(jù)庫連接池就可在此配置:   
          161.    <resource-ref>   
          162.        <description>JNDI JDBC DataSource of shop</description>   
          163.        <res-ref-name>jdbc/sample_db</res-ref-name>   
          164.        <res-type>javax.sql.DataSource</res-type>   
          165.        <res-auth>Container</res-auth>   
          166.    </resource-ref>   
          167. 15、安全限制配置   
          168.    <security-constraint>   
          169.       <display-name>Example Security Constraint</display-name>   
          170.       <web-resource-collection>   
          171.          <web-resource-name>Protected Area</web-resource-name>   
          172.          <url-pattern>/jsp/security/protected/*</url-pattern>   
          173.          <http-method>DELETE</http-method>   
          174.          <http-method>GET</http-method>   
          175.          <http-method>POST</http-method>   
          176.          <http-method>PUT</http-method>   
          177.       </web-resource-collection>   
          178.       <auth-constraint>   
          179.         <role-name>tomcat</role-name>   
          180.         <role-name>role1</role-name>   
          181.       </auth-constraint>   
          182.    </security-constraint>   
          183. 16、登陸驗(yàn)證配置   
          184.    <login-config>   
          185.      <auth-method>FORM</auth-method>   
          186.      <realm-name>Example-Based Authentiation Area</realm-name>   
          187.      <form-login-config>   
          188.         <form-login-page>/jsp/security/protected/login.jsp</form-login-page>   
          189.         <form-error-page>/jsp/security/protected/error.jsp</form-error-page>   
          190.      </form-login-config>   
          191.    </login-config>   
          192. 17、安全角色:security-role元素給出安全角色的一個(gè)列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。   
          193.     分別地聲明角色可使高級IDE處理安全信息更為容易。   
          194.   <security-role>   
          195.      <role-name>tomcat</role-name>   
          196.   </security-role>   
          197. 18、Web環(huán)境參數(shù):env-entry元素聲明Web應(yīng)用的環(huán)境項(xiàng)   
          198.   <env-entry>   
          199.      <env-entry-name>minExemptions</env-entry-name>   
          200.      <env-entry-value>1</env-entry-value>   
          201.      <env-entry-type>java.lang.Integer</env-entry-type>   
          202.   </env-entry>   
          203. 19、EJB 聲明   
          204.   <ejb-ref>   
          205.      <description>Example EJB reference</decription>   
          206.      <ejb-ref-name>ejb/Account</ejb-ref-name>   
          207.      <ejb-ref-type>Entity</ejb-ref-type>   
          208.      <home>com.mycompany.mypackage.AccountHome</home>   
          209.      <remote>com.mycompany.mypackage.Account</remote>   
          210.   </ejb-ref>   
          211. 20、本地EJB聲明   
          212.   <ejb-local-ref>   
          213.      <description>Example Loacal EJB reference</decription>   
          214.      <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>   
          215.      <ejb-ref-type>Session</ejb-ref-type>   
          216.      <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>   
          217.      <local>com.mycompany.mypackage.ProcessOrder</local>   
          218.   </ejb-local-ref>   
          219. 21、配置DWR   
          220.   <servlet>   
          221.       <servlet-name>dwr-invoker</servlet-name>   
          222.       <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>   
          223.   </servlet>   
          224.   <servlet-mapping>   
          225.       <servlet-name>dwr-invoker</servlet-name>   
          226.       <url-pattern>/dwr/*</url-pattern>   
          227.   </servlet-mapping>   
          228. 22、配置Struts   
          229.     <display-name>Struts Blank Application</display-name>   
          230.     <servlet>   
          231.         <servlet-name>action</servlet-name>   
          232.         <servlet-class>   
          233.             org.apache.struts.action.ActionServlet   
          234.         </servlet-class>   
          235.         <init-param>   
          236.             <param-name>detail</param-name>   
          237.             <param-value>2</param-value>   
          238.         </init-param>   
          239.         <init-param>   
          240.             <param-name>debug</param-name>   
          241.             <param-value>2</param-value>   
          242.         </init-param>   
          243.         <init-param>   
          244.             <param-name>config</param-name>   
          245.             <param-value>/WEB-INF/struts-config.xml</param-value>   
          246.         </init-param>   
          247.         <init-param>   
          248.             <param-name>application</param-name>   
          249.             <param-value>ApplicationResources</param-value>   
          250.         </init-param>   
          251.         <load-on-startup>2</load-on-startup>   
          252.     </servlet>   
          253.     <servlet-mapping>   
          254.         <servlet-name>action</servlet-name>   
          255.         <url-pattern>*.do</url-pattern>   
          256.     </servlet-mapping>   
          257.     <welcome-file-list>   
          258.         <welcome-file>index.jsp</welcome-file>   
          259.     </welcome-file-list>   
          260.   
          261.     <!-- Struts Tag Library Descriptors -->   
          262.     <taglib>   
          263.         <taglib-uri>struts-bean</taglib-uri>   
          264.         <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>   
          265.     </taglib>   
          266.     <taglib>   
          267.         <taglib-uri>struts-html</taglib-uri>   
          268.         <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>   
          269.     </taglib>   
          270.     <taglib>   
          271.     <taglib-uri>struts-nested</taglib-uri>   
          272.     <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>   
          273.     </taglib>   
          274.     <taglib>   
          275.         <taglib-uri>struts-logic</taglib-uri>   
          276.         <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>   
          277.     </taglib>   
          278.     <taglib>   
          279.         <taglib-uri>struts-tiles</taglib-uri>   
          280.         <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>   
          281.     </taglib>   
          282. 23、配置Spring(基本上都是在Struts中配置的)   
          283.   
          284.    <!-- 指定spring配置文件位置 -->   
          285.    <context-param>   
          286.       <param-name>contextConfigLocation</param-name>   
          287.       <param-value>   
          288.        <!--加載多個(gè)spring配置文件 -->   
          289.         /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml   
          290.       </param-value>   
          291.    </context-param>   
          292.   
          293.    <!-- 定義SPRING監(jiān)聽器,加載spring -->   
          294.   
          295.   <listener>   
          296.      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
          297.   </listener>   
          298.   
          299.   <listener>   
          300.      <listener-class>   
          301.        org.springframework.web.context.request.RequestContextListener   
          302.      </listener-class>   
          303.   </listener>   

          posted on 2010-09-30 16:33 小菜毛毛 閱讀(1651) 評論(0)  編輯  收藏 所屬分類: java定時(shí)器servlet
          主站蜘蛛池模板: 忻城县| 怀集县| 伊川县| 云阳县| 中方县| 大关县| 天全县| 墨玉县| 清新县| 横山县| 绥阳县| 泸定县| 惠安县| 腾冲县| 四会市| 南江县| 云龙县| 崇仁县| 定安县| 淮北市| 分宜县| 汉川市| 临澧县| 洮南市| 岱山县| 九寨沟县| 德格县| 广丰县| 柳林县| 兰考县| 蒙山县| 齐齐哈尔市| 福州市| 申扎县| 张家港市| 会东县| 平乡县| 寿宁县| 融水| 什邡市| 昆山市|