web.xml加载顺序
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
一
1、啟動(dòng)一個(gè)WEB項(xiàng)目的時(shí)候,WEB容器會(huì)去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個(gè)結(jié)點(diǎn)。?
2、緊急著,容創(chuàng)建一個(gè)ServletContext(servlet上下文),這個(gè)web項(xiàng)目的所有部分都將共享這個(gè)上下文。?
3、容器將<context-param>轉(zhuǎn)換為鍵值對,并交給servletContext。?
4、容器創(chuàng)建<listener>中的類實(shí)例,創(chuàng)建監(jiān)聽器。?
二 ?Load-on-startup
Load-on-startup元素在web應(yīng)用啟動(dòng)的時(shí)候指定了servlet被加載的順序,它的值必須是一個(gè)整數(shù)。如果它的值是一個(gè)負(fù)整數(shù)或是這個(gè)元素不存在,那么容器會(huì)在該servlet被調(diào)用的時(shí)候,加載這個(gè)servlet。如果值是正整數(shù)或零,容器在配置的時(shí)候就加載并初始化這個(gè)servlet,容器必須保證值小的先被加載。如果值相等,容器可以自動(dòng)選擇先加載誰。?
在servlet的配置當(dāng)中,<load-on-startup>5</load-on-startup>的含義是:?
標(biāo)記容器是否在啟動(dòng)的時(shí)候就加載這個(gè)servlet。?
當(dāng)值為0或者大于0時(shí),表示容器在應(yīng)用啟動(dòng)時(shí)就加載這個(gè)servlet;?
當(dāng)是一個(gè)負(fù)數(shù)時(shí)或者沒有指定時(shí),則指示容器在該servlet被選擇時(shí)才加載。?
正數(shù)的值越小,啟動(dòng)該servlet的優(yōu)先級越高。?
三 ?加載順序
首先可以肯定的是,加載順序與它們在 web.xml 文件中的先后順序無關(guān)。即不會(huì)因?yàn)?filter 寫在 listener的前面而會(huì)先加載 filter。最終得出的結(jié)論是:ServletContext->?listener ->filter -> servlet
?????同時(shí)還存在著這樣一種配置節(jié):context-param,它用于向 ServletContext提供鍵值對,即應(yīng)用程序上下文信息。我們的 listener, filter 等在初始化時(shí)會(huì)用到這些上下文中的信息,那么context-param 配置節(jié)是不是應(yīng)該寫在 listener 配置節(jié)前呢?實(shí)際上 context-param配置節(jié)可寫在任意位置,因此真正的加載順序?yàn)?#xff1a;context-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 的加載順序是:ServletContext->?context-param ->listener -> filter -> servlet,而同個(gè)類型之間的實(shí)際程序調(diào)用的時(shí)候的順序是根據(jù)對應(yīng)的 mapping 的順序進(jìn)行調(diào)用的。
?
四?web.xml文件詳解?
?
我將自己知道的web.xml的元素整理了一下:
web.xml首先是肯定要包含它的schema.
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
???xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
???xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
???version="2.4">
</web-app>
其它的元素都放在<web-app></web-app>之中。
<discription></discription>是對站臺(tái)的描述
<display-name></display-name>定義站臺(tái)的名稱
<distributable/> 是指定該站臺(tái)是否可分布式處理
<context-param></context-param>用來設(shè)定web站臺(tái)的環(huán)境參數(shù),它包含兩個(gè)子元素:
???<param-name></param-name>用來指定參數(shù)的名稱
???<param-value></param-value>用來設(shè)定參數(shù)值
比如:
<context-param>
???<param-name>my_param</param-name>
???<param-value>hello</param-value>
</context-param>
在此設(shè)定的參數(shù),可以在servlet中用getServletContext().getInitParameter("my_param") 來取得
<filter></filter>是用來聲明filter的相關(guān)設(shè)定,它包含以下子元素:
???<filter-name></filter-name>這當(dāng)然就是指定filter的名字
???<filter-class></filter-class>這是用來定義filter的類的名稱
???<init-param></init-param>用來定義參數(shù),它有兩個(gè)子元素:
???????<param-name></param-name>用來指定參數(shù)的名稱
???????<param-value></param-value>用來設(shè)定參數(shù)值
比如:
<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></filter>同時(shí)使用的是<filter-mapping></filter-mapping>用來定義filter所對應(yīng)的URL,它有兩個(gè)子元素:
???<filter-name></filter-name>指定filter的名字
???<url-pattern></url-pattern>指定filter所對應(yīng)的URL
比如:
<filter-mapping>
???????<filter-name>setCharacterEncoding</filter-name>
???????<url-pattern>/*</url-pattern>
</filter-mapping>
<listener></listener>用來設(shè)定Listener接口,它的主要子元素為
???<listener-class></listener-class>定義Listener的類名稱
比如:
<listener>
???<listener-class>com.myTest.ContextListener</listener-class>?
</listener>
<servlet></servlet>用來聲明一個(gè)servlet的數(shù)據(jù),主要有以下子元素:
???<servlet-name></servlet-name>指定servlet的名稱
???<servlet-class></servlet-class>指定servlet的類名稱
???<jsp-file></jsp-file>指定web站臺(tái)中的某個(gè)JSP網(wǎng)頁的完整路徑
???<init-param></init-param>用來定義參數(shù),和前面的<init-param>差不多
同樣,與<servlet></servlet>一起使用的是<servlet-mapping></servlet-mapping>用來定義servlet所對應(yīng)的URL,包含兩個(gè)子元素:
???<servlet-name></servlet-name>指定servlet的名稱
???<url-pattern></url-pattern>指定servlet所對應(yīng)的URL
比如:
<servlet>
???????<servlet-name>ShoppingServlet</servlet-name>
???????<servlet-class>com.myTest.ShoppingServlet</servlet-class>
</servlet>
????
<servlet-mapping>
???????<servlet-name>ShoppingServlet</servlet-name>
???????<url-pattern>/shop/ShoppingServlet</url-pattern>
</servlet-mapping>
<session-config></session-config>用來定義web站臺(tái)中的session參數(shù),包含一個(gè)子元素:
???<session-timeout></session-timeout>用來定義這個(gè)web站臺(tái)所有session的有效期限,單位為?????????????????????????????????????????????????????????????分鐘
<mime-mapping></mime-mapping>定義某一個(gè)擴(kuò)展名和某一個(gè)MIMEType做對映,包含兩個(gè)???????????????????????????????????????????????????????子元素:
???<extension></extension>擴(kuò)展名的名稱
???<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>
<welcome-file-list></welcom-file-list>用來定義首頁的列單,包含一個(gè)子元素:
???<welcome-file></welcome-file>指定首頁的文件名稱
比如:
<welcome-file-list>
???<welcome-file>index.jsp</welcome-file>
???<welcome-file>index.html</welcome-file>
</welcom-file-list>
<error-page></error-page>用來處理錯(cuò)誤代碼或異常的頁面,有三個(gè)子元素:
???<error-code></error-code>指定錯(cuò)誤代碼
???<exception-type></exception-type>指定一個(gè)JAVA異常類型
???<location></location>指定在web站臺(tái)內(nèi)的相關(guān)資源路徑
比如:
<error-page>
???<error-code>404</error-code>
???<location>/error404.jsp</location>
</error-page>
<error-page>
???<exception-type>java.lang.Exception</exception-type>
???<location>/exception.jsp</location>
</error-page>
<taglib></taglib>用來設(shè)定JSP網(wǎng)頁所用到的Tag Library路徑,有兩個(gè)子元素:
???<taglib-uri></taglib-uri>定義TLD文件的URI,在JSP網(wǎng)頁中用taglib指令便可取得該URI的?????????????????????????????????????????????TLD文件
???<taglib-location></taglib-location>指定TLD文件相對于web站臺(tái)的存放位置
比如:
<taglib>
?????<taglib-uri>myTaglib</taglib-uri>
?????<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<resource-ref></resource-ref>定義利用JNDI取得站臺(tái)可利用的資源,有五個(gè)子元素:
???<description></description>資源說明
???<rec-ref-name></rec-ref-name>資源名稱
???<res-type></res-type>資源種類
???<res-auth></res-auth>資源經(jīng)由Application或Container來許可
???<res-sharing-scope></res-sharing-scope>資源是否可以共享,有Shareable和Unshareable兩個(gè)????????????????????????????????????????????????????????????????????值,默認(rèn)為Shareable
比如,配置數(shù)據(jù)庫連接池就可在此配置:
<resource-ref>
???????<description>JNDI JDBC DataSource ofshop</description>
???????<res-ref-name>jdbc/sample_db</res-ref-name>
???????<res-type>javax.sql.DataSource</res-type>
???????<res-auth>Container</res-auth>
</resource-ref>
<jsp-config>包括<taglib>和<jsp-property-group> 兩個(gè)子元素。
????
???其中<taglib>元素在JSP1.2時(shí)就已經(jīng)存在;而<jsp-property-group>是JSP2.0 新增的元素。
???<jsp-property-group>元素主要有八個(gè)子元素,它們分別為:
????
???1.<description>:設(shè)定的說明;
???2.<display-name>:設(shè)定名稱;
???3.<url-pattern>:設(shè)定值所影響的范圍,如:/CH2 或/*.jsp;
???4.<el-ignored>:若為true,表示不支持EL語法;
???5.<scripting-invalid>:若為true,表示不支持<%scripting %>語法;
???6.<page-encoding>:設(shè)定JSP 網(wǎng)頁的編碼;
???7.<include-prelude>:設(shè)置JSP網(wǎng)頁的抬頭,擴(kuò)展名為.jspf;
???8.<include-coda>:設(shè)置JSP網(wǎng)頁的結(jié)尾,擴(kuò)展名為.jspf。
一個(gè)簡單的<jsp-config>元素完整配置:
<jsp-config>
???<taglib>
???????<taglib-uri>Taglib</taglib-uri>
???????<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
???</taglib>
???<jsp-property-group>
???????<description>Special property groupfor JSP Configuration JSPexample.</description>
???????<display-name>JSPConfiguration</display-name>
???????<url-pattern>/jsp/*</url-pattern>
???????<el-ignored>true</el-ignored>
???????<page-encoding>GB2312</page-encoding>
???????<scripting-invalid>true</scripting-invalid>
???????<include-prelude>/include/prelude.jspf</include-prelude>
???????<include-coda>/include/coda.jspf</include-coda>
???</jsp-property-group>
</jsp-config>??
配置web.xml來限制對某些servlet的請求
有時(shí)我們只希望通過認(rèn)證的用戶才能請求某些servlet的話,就可以在web.xml中來進(jìn)行相應(yīng)的配置,來達(dá)到此目的。
這就要用到<security-constraint></security-constraint>元素。
對于tomcat,中web.xml使用security-constraint元素需要在位于<Tomcat-installation-directory>/conf/tomcat-users.xml的XML文件中創(chuàng)建用戶名和密碼。比如下面的這個(gè)tomcat-users.xml文件:
<?xml version='1.0'encoding='utf-8'?>
<tomcat-users>
?<rolerolename="tomcat"/>
?<rolerolename="manager"/>
?<rolerolename="admin"/>
?<user username="tomcat"password="tomcat" roles="tomcat"/>
?<user username="both"password="tomcat" roles="tomcat,manager"/>
?<user username="admin"password="admin" roles="admin"/>
</tomcat-users>
此XML片段包括一個(gè)tomcat-users根元素,它包含一個(gè)或多個(gè)role和user元素。
然后在Web應(yīng)用程序的web.xml中創(chuàng)建security-constraint、login-config和security-role元素。
<security-constraint>
?????<web-resource-collection>
?????????<web-resource-name>HelloServlet</web-resource-name>
?????????<url-pattern>/HelloServlet</url-pattern>
?????????<http-method>GET</http-method>
?????????<http-method>POST</http-method>
?????</web-resource-collection>
?????<auth-constraint>
?????????<description>This applies only to the"tomcat" securityrole</description>
?????????<role-name>admin</role-name>
?????</auth-constraint>
?????<user-data-constraint>
?????????<transport-guarantee>NONE</transport-guarantee>
?????</user-data-constraint>
?</security-constraint>
??
?<login-config>
?????<auth-method>BASIC</auth-method>
?</login-config>
?<security-role>
?????<role-name>admin</role-name>
?</security-role>
其中security-constraint元素包含一個(gè)或多個(gè)web-resource-collection元素,它是描述Web應(yīng)用程序中的哪些web資源受到指定安全限制的保護(hù)。http-method元素指定安全限制覆蓋的HTTP方法。上面的例子中,當(dāng)我們對/HelloServlet的GET或POST請求時(shí)將觸發(fā)配置的安全機(jī)制。
auth-constraint元素用于描述允許訪問Web組件的安全角色。此例中安全角色的例子有tomcat、manager、admin。而只有當(dāng)作為admin角色的用戶才可以訪問HelloServlet。
Web應(yīng)用程序通過login-config元素來認(rèn)證用戶,并確認(rèn)該用戶是否為正確的角色。
longin-config包含的transport-guarantee子元素用來指定認(rèn)證方法,BASIC是一種常見的Web認(rèn)證方式,瀏覽器給用戶提示一個(gè)對話框,要求輸入用戶名和密碼,隨后Tomcat將給出的用戶名和密碼與tomcat-users.xml中的用戶名和密碼進(jìn)行比較,然后使用前面的security-constraint配置來確定用戶是否可訪問受保護(hù)的servlet。
(除BASIC外,還可以是FORM、CLIENT-CERT、DIGEST等)
其實(shí)這種認(rèn)證方法實(shí)際上有兩個(gè)步驟:
1、檢查提供的用戶名和密碼是否正確。
2、判斷用戶是否映射到特定的安全角色。例如,用戶可能提供了正確的用戶名和密碼,但沒有映射到特定的安全角色,也將被禁止訪問特定的Web資源。
五相應(yīng)元素配置????
1、Web應(yīng)用圖標(biāo):指出IDE和GUI工具用來表示W(wǎng)eb應(yīng)用的大圖標(biāo)和小圖標(biāo)????
<icon>????
<small-icon>/images/app_small.gif</small-icon>????
<large-icon>/images/app_large.gif</large-icon>????
</icon>????
2、Web應(yīng)用名稱:提供GUI工具可能會(huì)用來標(biāo)記這個(gè)特定的Web應(yīng)用的一個(gè)名稱????
<display-name>TomcatExample</display-name>????
3、Web 應(yīng)用描述:給出于此相關(guān)的說明性文本????
<disciption>Tomcat Example servletsand JSPpages.</disciption>????
4、上下文參數(shù):聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。????
<context-param>????
???<param-name>ContextParameter</para-name>????
???<param-value>test</param-value>????
???<description>It is a testparameter.</description>????
</context-param>????
在servlet里面可以通過getServletContext().getInitParameter("context/param")得到????
5、過濾器配置:將一個(gè)名字與一個(gè)實(shí)現(xiàn)javaxs.servlet.Filter接口的類相關(guān)聯(lián)。????
<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)聽器配置????
<listener>????
?????<listerner-class>listener.SessionListener</listener-class>????
</listener>????
7、Servlet配置????
??基本配置????
??<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>????
??高級配置????
??<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 foranonymousaccess</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>用來聲明一個(gè)servlet的數(shù)據(jù),主要有以下子元素:????
????<servlet-name></servlet-name>指定servlet的名稱????
????<servlet-class></servlet-class>指定servlet的類名稱????
????<jsp-file></jsp-file>指定web站臺(tái)中的某個(gè)JSP網(wǎng)頁的完整路徑????
????<init-param></init-param>用來定義參數(shù),可有多個(gè)init-param。在servlet類中通過getInitParamenter(Stringname)方法訪問初始化參數(shù)????
????<load-on-startup></load-on-startup>指定當(dāng)Web應(yīng)用啟動(dòng)時(shí),裝載Servlet的次序。????
????????????????????????????????當(dāng)值為正數(shù)或零時(shí):Servlet容器先加載數(shù)值小的servlet,再依次加載其他數(shù)值大的servlet.????
????????????????????????????????當(dāng)值為負(fù)或未定義:Servlet容器將在Web客戶首次訪問這個(gè)servlet時(shí)加載它????
????<servlet-mapping></servlet-mapping>用來定義servlet所對應(yīng)的URL,包含兩個(gè)子元素????
??????<servlet-name></servlet-name>指定servlet的名稱????
??????<url-pattern></url-pattern>指定servlet所對應(yīng)的URL????
8、會(huì)話超時(shí)配置(單位為分鐘)????
??<session-config>????
?????<session-timeout>120</session-timeout>????
??</session-config>????
9、MIME類型配置????
??<mime-mapping>????
?????<extension>htm</extension>????
?????<mime-type>text/html</mime-type>????
??</mime-mapping>????
10、指定歡迎文件頁配置????
??<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、配置錯(cuò)誤頁面????
一、通過錯(cuò)誤碼來配置error-page????
??<error-page>????
?????<error-code>404</error-code>????
?????<location>/NotFound.jsp</location>????
??</error-page>????
上面配置了當(dāng)系統(tǒng)發(fā)生404錯(cuò)誤時(shí),跳轉(zhuǎn)到錯(cuò)誤處理頁面NotFound.jsp。????
二、通過異常的類型配置error-page????
??<error-page>????
??????<exception-type>java.lang.NullException</exception-type>????
??????<location>/error.jsp</location>????
??</error-page>????
上面配置了當(dāng)系統(tǒng)發(fā)生java.lang.NullException(即空指針異常)時(shí),跳轉(zhuǎn)到錯(cuò)誤處理頁面error.jsp????
12、TLD配置????
??<taglib>????
??????<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>????
??????<taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>????
??</taglib>????
??如果MyEclipse一直在報(bào)錯(cuò),應(yīng)該把<taglib> 放到<jsp-config>中????
??<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、資源管理對象配置????
??<resource-env-ref>????
??????<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>????
??</resource-env-ref>????
14、資源工廠配置????
??<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ù)庫連接池就可在此配置:????
??<resource-ref>????
??????<description>JNDI JDBC DataSource ofshop</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、安全限制配置????
??<security-constraint>????
?????<display-name>Example SecurityConstraint</display-name>????
?????<web-resource-collection>????
????????<web-resource-name>ProtectedArea</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、登陸驗(yàn)證配置????
??<login-config>????
????<auth-method>FORM</auth-method>????
????<realm-name>Example-BasedAuthentiationArea</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元素給出安全角色的一個(gè)列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。????
???分別地聲明角色可使高級IDE處理安全信息更為容易。????
<security-role>????
????<role-name>tomcat</role-name>????
</security-role>????
18、Web環(huán)境參數(shù):env-entry元素聲明Web應(yīng)用的環(huán)境項(xiàng)????
<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聲明????
<ejb-ref>????
????<description>Example EJBreference</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聲明????
<ejb-local-ref>????
????<description>Example Loacal EJBreference</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????
<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????
???<display-name>Struts BlankApplication</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中配置的)????
??<!--指定spring配置文件位置-->????
??<context-param>????
?????<param-name>contextConfigLocation</param-name>????
?????<param-value>????
??????<!--加載多個(gè)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 文件中一般包括 servlet, spring, filter,listenr的配置。那么他們是按照一個(gè)什么順序加載呢?
加載順序會(huì)影響對spring bean 的調(diào)用。
???比如filter需要用到 bean ,但是加載順序是 先加載filter后加載spring,則filter中初始化操作中的bean為null;
首先可以肯定 加載順序與他們在web.xml 文件中的先后順序無關(guān)。
web.xml 中 listener 和 serverlet 的加載順序?yàn)?先 listener 后serverlet
最終得出結(jié)果:先 listener >> filter>> servlet>>?spring
?所以,如果過濾器中要使用到 bean,可以將spring 的加載 改成Listener的方式
<listener>
???????<listener-class>
???????????org.springframework.web.context.ContextLoaderListener
???????</listener-class>
???</listener>
?搞定!
?關(guān)于他們的內(nèi)部執(zhí)行順序,也需要注意,如下面文章中遇到的問題
web.xml的filter執(zhí)行順序?qū)е碌膩y碼,切記!
2008-05-02 01:38
發(fā)現(xiàn)引起bug的原因是web.xml的下面幾行:
???<filter-mapping>
???????<filter-name>SecurityFilter</filter-name>
???????<url-pattern>*.do</url-pattern>
???</filter-mapping>
???<filter-mapping>
???????<filter-name>CharacterEncoding</filter-name>
???????<url-pattern>*.do</url-pattern>
???</filter-mapping>
???<filter-mapping>
???????<filter-name>CharacterEncoding</filter-name>
???????<url-pattern>*.jsp</url-pattern>
???</filter-mapping>
根據(jù)servlet2.3規(guī)范filter執(zhí)行是按照web.xml配置的filter-mapping先后順序進(jìn)行執(zhí)行,所以上面的配置會(huì)導(dǎo)致遇見*.do的url請求,先進(jìn)行SecurityFilter的過濾器處理,這時(shí)候沒有做編碼處理,已經(jīng)是亂碼,到下面的filter處理時(shí)已經(jīng)時(shí)亂碼,再做編碼處理已經(jīng)沒有用處。
修正方式,調(diào)整filter-mapping順序,如下:
???<filter-mapping>
???????<filter-name>CharacterEncoding</filter-name>
???????<url-pattern>*.do</url-pattern>
???</filter-mapping>
???<filter-mapping>
???????<filter-name>CharacterEncoding</filter-name>
???????<url-pattern>*.jsp</url-pattern>
???</filter-mapping>
???<filter-mapping>
???????<filter-name>SecurityFilter</filter-name>
???????<url-pattern>*.do</url-pattern>
???</filter-mapping>
轉(zhuǎn)載于:https://my.oschina.net/wanyuxiang000/blog/191763
總結(jié)
以上是生活随笔為你收集整理的web.xml加载顺序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS清浮动处理(Clear与BFC)
- 下一篇: using 语句中使用的类型必须可隐式转