web.xml 的加載順序是:
context-param -> listener -> filter -> servlet
web.xml文件詳解
Web.xml常用元素
<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>
過(guò)濾器元素將一個(gè)名字與一個(gè)實(shí)現(xiàn)*javax.servlet.Filter
接口的類相關(guān)聯(lián)。
<filter-mapping></filter-mapping>
一旦命名了一個(gè)過(guò)濾器,就要利用filter-mapping元素把它與一個(gè)或多個(gè)servlet或JSP頁(yè)面相關(guān)聯(lián)涮母。 -
<listener></listener>
servlet API的版本2.3增加了對(duì)事件監(jiān)聽(tīng)程序的支持段审,事件監(jiān)聽(tīng)程序在建立抑钟、修改和刪除會(huì)話或servlet環(huán)境時(shí)得到通知。Listener元素指出事件監(jiān)聽(tīng)程序類尽棕。 -
<servlet></servlet>
在向servlet或JSP頁(yè)面制定初始化參數(shù)或定制URL時(shí)喳挑,必須首先命名servlet或JSP頁(yè)面。Servlet元素就是用來(lái)完成此項(xiàng)任務(wù)的滔悉。 -
<servlet-mapping></servlet-mapping>
服務(wù)器一般為servlet提供一個(gè)缺省的URL:http://host/webAppPrefix/servlet/ServletName 但是伊诵,常常會(huì)更改這個(gè)URL,以便servlet可以訪問(wèn)初始化參數(shù)或更容易地處理相對(duì)URL回官。在更改缺省URL時(shí)曹宴,使用servlet-mapping元素。 -
<session-config></session-config>
如果某個(gè)會(huì)話在一定時(shí)間內(nèi)未被訪問(wèn)歉提,服務(wù)器可以拋棄它以節(jié)省內(nèi)存笛坦。可通過(guò)使用HttpSession的setMaxInactiveInterval方法明確設(shè)置單個(gè)會(huì)話對(duì)象的超時(shí)值苔巨,或者可利用session-config元素制定缺省超時(shí)值版扩。 -
<mime-mapping></mime-mapping>
如果Web應(yīng)用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型侄泽,則mime-mapping元素提供這種保證礁芦。 -
<welcome-file-list></welcome-file-list>
指示服務(wù)器在收到引用一個(gè)目錄名而不是文件名的URL時(shí),使用哪個(gè)文件。 -
<error-page></error-page>
在返回特定HTTP狀態(tài)代碼時(shí)柿扣,或者特定類型的異常被拋出時(shí)肖方,能夠制定將要顯示的頁(yè)面。 -
<taglib></taglib>
對(duì)標(biāo)記庫(kù)描述符文件(Tag Libraryu Descriptor file)指定別名未状。此功能使你能夠更改TLD文件的位置俯画,而不用編輯使用這些文件的JSP頁(yè)面。 -
<resource-env-ref></resource-env-ref>
聲明與資源相關(guān)的一個(gè)管理對(duì)象司草。 -
<resource-ref></resource-ref>
聲明一個(gè)資源工廠使用的外部資源艰垂。 -
<security-constraint></security-constraint>
制定應(yīng)該保護(hù)的URL。它與login-config元素聯(lián)合使用 -
<security-role></security-role>
給出安全角色的一個(gè)列表翻伺,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中材泄。分別地聲明角色可使高級(jí)IDE處理安全信息更為容易。 -
<env-entry></env-entry>
聲明Web應(yīng)用的環(huán)境項(xiàng)吨岭。 -
<ejb-ref></ejb-ref>
聲明一個(gè)EJB的主目錄的引用拉宗。 -
<ejb-local-ref></ejb-local-ref>
聲明一個(gè)EJB的本地主目錄的應(yīng)用。
</web-app>
相應(yīng)元素配置
- Web應(yīng)用圖標(biāo):指出IDE和GUI工具用來(lái)表示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>
-
Web 應(yīng)用名稱:提供GUI工具可能會(huì)用來(lái)標(biāo)記這個(gè)特定的Web應(yīng)用的一個(gè)名稱
<display-name>Tomcat Example</display-name>
-
Web 應(yīng)用描述: 給出于此相關(guān)的說(shuō)明性文本
<disciption>Tomcat Example servlets and JSP pages.</disciption>
- 上下文參數(shù):聲明應(yīng)用范圍內(nèi)的初始化參數(shù)辣辫。
<context-param>
<param-name>ContextParameter</para-name>
<param-value>test</param-value>
<description>It is a test parameter.</description>
</context-param>
在servlet里面可以通過(guò)getServletContext().getInitParameter("context/param")
得到
- 過(guò)濾器配置:將一個(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>
- 監(jiān)聽(tīng)器配置
<listener>
<listerner-class>listener.SessionListener</listener-class>
</listener>
-
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>
高級(jí)配置
<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>
元素說(shuō)明
<servlet></servlet> 用來(lái)聲明一個(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)頁(yè)的完整路徑
<init-param></init-param> 用來(lái)定義參數(shù)急灭,可有多個(gè)init-param姐浮。在servlet類中通過(guò)getInitParamenter(String name)方法訪問(wèn)初始化參數(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客戶首次訪問(wèn)這個(gè)servlet時(shí)加載它
<servlet-mapping></servlet-mapping> 用來(lái)定義servlet所對(duì)應(yīng)的URL,包含兩個(gè)子元素
<servlet-name></servlet-name> 指定servlet的名稱
<url-pattern></url-pattern> 指定servlet所對(duì)應(yīng)的URL
- 會(huì)話超時(shí)配置(單位為分鐘)
<session-config>
<session-timeout>120</session-timeout>
</session-config>
- MIME類型配置
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
- 指定歡迎文件頁(yè)配置
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
配置錯(cuò)誤頁(yè)面
通過(guò)錯(cuò)誤碼來(lái)配置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ò)誤處理頁(yè)面NotFound.jsp蛋逾。
- 通過(guò)異常的類型配置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ò)誤處理頁(yè)面error.jsp
- 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>
- 資源管理對(duì)象配置
<resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>
- 資源工廠配置
<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ù)庫(kù)連接池就可在此配置:
<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>
- 安全限制配置
<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>
- 登陸驗(yàn)證配置
<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>
-
安全角色:security-role元素給出安全角色的一個(gè)列表窗悯,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中区匣。
分別地聲明角色可使高級(jí)IDE處理安全信息更為容易。
<security-role>
<role-name>tomcat</role-name>
</security-role>
- 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>
- EJB 聲明
<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>
- 本地EJB聲明
<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>
- 配置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>
- 配置Struts
<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>
- 配置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)聽(tīng)器蒋院,加載spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>