一、web.xml配置文件常用元素及其意義預(yù)覽1 <web-app> 2 3 <!--定義了WEB應(yīng)用的名字--> 4 <display-name></display-name> 5 6 <!--聲明WEB應(yīng)用的描述信息--> 7 <description></description> 8 9 <!--context-param元素聲明應(yīng)用范圍內(nèi)的初始化參數(shù)--> 10 <context-param></context-param> 11 12 <!--過濾器元素將一個(gè)名字與一個(gè)實(shí)現(xiàn)javax.servlet.Filter接口的類相關(guān)聯(lián)--> 13 <filter></filter> 14 15 <!--一旦命名了一個(gè)過濾器,就要利用filter-mapping元素把它與一個(gè)或多個(gè)servlet或JSP頁面相關(guān)聯(lián)--> 16 <filter-mapping></filter-mapping> 17 18 <!--servlet API的版本2.3增加了對(duì)事件監(jiān)聽程序的支持,事件監(jiān)聽程序在建立、修改和刪除會(huì)話或servlet環(huán)境時(shí)得到通知。 19 Listener元素指出事件監(jiān)聽程序類--> 20 <listener></listener> 21 22 <!--在向servlet或JSP頁面制定初始化參數(shù)或定制URL時(shí),必須首先命名servlet或JSP頁面。 23 Servlet元素就是用來完成此項(xiàng)任務(wù)的--> 24 <servlet></servlet> 25 26 <!--服務(wù)器一般為servlet提供一個(gè)缺省的URL:http://host/webAppPrefix/servlet/ServletName。 27 但是,常常會(huì)更改這個(gè)URL,以便servlet可以訪問初始化參數(shù)或更容易地處理相對(duì)URL。 28 在更改缺省URL時(shí),使用servlet-mapping元素--> 29 <servlet-mapping></servlet-mapping> 30 31 <!--如果某個(gè)會(huì)話在一定時(shí)間內(nèi)未被訪問,服務(wù)器可以拋棄它以節(jié)省內(nèi)存??赏ㄟ^使用HttpSession的 32 setMaxInactiveInterval方法明確設(shè)置單個(gè)會(huì)話對(duì)象的超時(shí)值,或者可利用session-config元素制定缺省超時(shí)值--> 33 <session-config></session-config> 34 35 <!--如果Web應(yīng)用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證--> 36 <mime-mapping></mime-mapping> 37 38 <!--指示服務(wù)器在收到引用一個(gè)目錄名而不是文件名的URL時(shí),使用哪個(gè)文件--> 39 <welcome-file-list></welcome-file-list> 40 41 <!--在返回特定HTTP狀態(tài)代碼時(shí),或者特定類型的異常被拋出時(shí),能夠制定將要顯示的頁面--> 42 <error-page></error-page> 43 44 <!--對(duì)標(biāo)記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置, 45 而不用編輯使用這些文件的JSP頁面--> 46 <taglib></taglib> 47 48 <!--聲明與資源相關(guān)的一個(gè)管理對(duì)象--> 49 <resource-env-ref></resource-env-ref> 50 51 <!--聲明一個(gè)資源工廠使用的外部資源--> 52 <resource-ref></resource-ref> 53 54 <!--制定應(yīng)該保護(hù)的URL。它與login-config元素聯(lián)合使用--> 55 <security-constraint></security-constraint> 56 57 <!--指定服務(wù)器應(yīng)該怎樣給試圖訪問受保護(hù)頁面的用戶授權(quán)。它與sercurity-constraint元素聯(lián)合使用--> 58 <login-config></login-config> 59 60 <!--給出安全角色的一個(gè)列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。 61 分別地聲明角色可使高級(jí)IDE處理安全信息更為容易--> 62 <security-role></security-role> 63 64 <!--聲明Web應(yīng)用的環(huán)境項(xiàng)--> 65 <env-entry></env-entry> 66 67 <!--聲明一個(gè)EJB的主目錄的引用--> 68 <ejb-ref></ejb-ref> 69 70 <!--聲明一個(gè)EJB的本地主目錄的應(yīng)用--> 71 <ejb-local-ref></ejb-local-ref> 72 73 </web-app> 二、各個(gè)配置元素詳解1.Web應(yīng)用圖標(biāo):指出IDE和GUI工具用來表示W(wǎng)eb應(yīng)用的大圖標(biāo)和小圖標(biāo) 1 <icon> 2 <small-icon>/images/app_small.gif</small-icon> 3 <large-icon>/images/app_large.gif</large-icon> 4 </icon>
2.Web 應(yīng)用名稱:提供GUI工具可能會(huì)用來標(biāo)記這個(gè)特定的Web應(yīng)用的一個(gè)名稱 <display-name>Tomcat Example</display-name>
3.Web 應(yīng)用描述:給出于此相關(guān)的說明性文本 <desciption>Tomcat Example servlets and JSP pages.</desciption>
4.上下文參數(shù):聲明應(yīng)用范圍內(nèi)的初始化參數(shù) 1 <context-param> 2 <param-name>參數(shù)名</para-name> 3 <param-value>參數(shù)值</param-value> 4 <description>參數(shù)描述</description> 5 </context-param> 在servlet里面可以通過 getServletContext().getInitParameter(“context/param”)得到
5.過濾器配置:將一個(gè)名字與一個(gè)實(shí)現(xiàn)javaxs.servlet.Filter接口的類相關(guān)聯(lián) 1 <filter> 2 <filter-name>setCharacterEncoding</filter-name> 3 <filter-class>com.myTest.setCharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>GB2312</param-value> 7 </init-param> 8 </filter> 9 <filter-mapping> 10 <filter-name>setCharacterEncoding</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping>
6.監(jiān)聽器配置 1 <listener> 2 <listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class> 3 </listener>
7.Servlet配置 1 <servlet> 2 <servlet-name>servlet名稱</servlet-name> 3 <servlet-class>servlet類全路徑</servlet-class> 4 <init-param> 5 <param-name>參數(shù)名</param-name> 6 <param-value>參數(shù)值</param-value> 7 </init-param> 8 <run-as> 9 <description>Security role for anonymous access</description> 10 <role-name>tomcat</role-name> 11 </run-as> 12 <load-on-startup>指定當(dāng)Web應(yīng)用啟動(dòng)時(shí),裝載Servlet的次序</load-on-startup> 13 </servlet> 14 <servlet-mapping> 15 <servlet-name>servlet名稱</servlet-name> 16 <url-pattern>映射路徑</url-pattern> 17 </servlet-mapping>
8.會(huì)話超時(shí)配置(單位為分鐘) 1 <session-config> 2 <session-timeout>120</session-timeout> 3 </session-config>
9.MIME類型配置 1 <mime-mapping> 2 <extension>htm</extension> 3 <mime-type>text/html</mime-type> 4 </mime-mapping>
10.指定歡迎文件頁配置 1 <welcome-file-list> 2 <welcome-file>index.jsp</welcome-file> 3 <welcome-file>index.html</welcome-file> 4 <welcome-file>index.htm</welcome-file> 5 </welcome-file-list>
11.配置錯(cuò)誤頁面 (1).通過錯(cuò)誤碼來配置error-page 1 <!--配置了當(dāng)系統(tǒng)發(fā)生404錯(cuò)誤時(shí),跳轉(zhuǎn)到錯(cuò)誤處理頁面NotFound.jsp--> 2 <error-page> 3 <error-code>404</error-code> 4 <location>/NotFound.jsp</location> 5 </error-page> (2).通過異常的類型配置error-page 1 <!--配置了當(dāng)系統(tǒng)發(fā)生java.lang.NullException(即空指針異常)時(shí),跳轉(zhuǎn)到錯(cuò)誤處理頁面error.jsp--> 2 <error-page> 3 <exception-type>java.lang.NullException</exception-type> 4 <location>/error.jsp</location> 5 </error-page>
12.TLD配置 1 <taglib> 2 <taglib-uri>http://jakarta./tomcat/debug-taglib</taglib-uri> 3 <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location> 4 </taglib> 如果開發(fā)工具一直在報(bào)錯(cuò),應(yīng)該把<taglib> 放到 <jsp-config>中 1 <jsp-config> 2 <taglib> 3 <taglib-uri>http://jakarta./tomcat/debug-taglib</taglib-uri> 4 <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location> 5 </taglib> 6 </jsp-config>
13.資源管理對(duì)象配置 1 <resource-env-ref> 2 <resource-env-ref-name>jms/StockQueue</resource-env-ref-name> 3 </resource-env-ref>
14.資源工廠配置 1 <resource-ref> 2 <res-ref-name>mail/Session</res-ref-name> 3 <res-type>javax.mail.Session</res-type> 4 <res-auth>Container</res-auth> 5 </resource-ref> 配置數(shù)據(jù)庫連接池就可在此配置 1 <resource-ref> 2 <description>JNDI JDBC DataSource of shop</description> 3 <res-ref-name>jdbc/sample_db</res-ref-name> 4 <res-type>javax.sql.DataSource</res-type> 5 <res-auth>Container</res-auth> 6 </resource-ref>
15.安全限制配置 1 <security-constraint> 2 <display-name>Example Security Constraint</display-name> 3 <web-resource-collection> 4 <web-resource-name>Protected Area</web-resource-name> 5 <url-pattern>/jsp/security/protected/*</url-pattern> 6 <http-method>DELETE</http-method> 7 <http-method>GET</http-method> 8 <http-method>POST</http-method> 9 <http-method>PUT</http-method> 10 </web-resource-collection> 11 <auth-constraint> 12 <role-name>tomcat</role-name> 13 <role-name>role1</role-name> 14 </auth-constraint> 15 </security-constraint>
16.登陸驗(yàn)證配置 1 <login-config> 2 <auth-method>FORM</auth-method> 3 <realm-name>Example-Based Authentiation Area</realm-name> 4 <form-login-config> 5 <form-login-page>/jsp/security/protected/login.jsp</form-login-page> 6 <form-error-page>/jsp/security/protected/error.jsp</form-error-page> 7 </form-login-config> 8 </login-config>
17.安全角色:security-role元素給出安全角色的一個(gè)列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。 1 <security-role> 2 <role-name>tomcat</role-name> 3 </security-role>
18.Web環(huán)境參數(shù):env-entry元素聲明Web應(yīng)用的環(huán)境項(xiàng) 1 <env-entry> 2 <env-entry-name>minExemptions</env-entry-name> 3 <env-entry-value>1</env-entry-value> 4 <env-entry-type>java.lang.Integer</env-entry-type> 5 </env-entry>
19.EJB 聲明 1 <ejb-ref> 2 <description>Example EJB reference</decription> 3 <ejb-ref-name>ejb/Account</ejb-ref-name> 4 <ejb-ref-type>Entity</ejb-ref-type> 5 <home>com.mycompany.mypackage.AccountHome</home> 6 <remote>com.mycompany.mypackage.Account</remote> 7 </ejb-ref>
20.本地EJB聲明 1 <ejb-local-ref> 2 <description>Example Loacal EJB reference</decription> 3 <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name> 4 <ejb-ref-type>Session</ejb-ref-type> 5 <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home> 6 <local>com.mycompany.mypackage.ProcessOrder</local> 7 </ejb-local-ref>
三、總結(jié)以上就是常用的web.xml中元素的配置以及作用了,歡迎提出異議和不適當(dāng)?shù)牡胤焦餐瑢W(xué)習(xí)。 |
|
|