1 <web-app> 2 3 <!--定義了WEB應用的名字--> 4 <display-name></display-name> 5 6 <!--聲明WEB應用的描述信息--> 7 <description></description> 8 9 <!--context-param元素聲明應用範圍內的初始化參數--> 10 <context-param></context-param> 11 12 <!--過濾器元素將一個名字與一個實現javax.servlet.Filter接口的類相關聯--> 13 <filter></filter> 14 15 <!--一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯--> 16 <filter-mapping></filter-mapping> 17 18 <!--servlet API的版本2.3增長了對事件監聽程序的支持,事件監聽程序在創建、修改和刪除會話或servlet環境時獲得通知。 19 Listener元素指出事件監聽程序類--> 20 <listener></listener> 21 22 <!--在向servlet或JSP頁面制定初始化參數或定製URL時,必須首先命名servlet或JSP頁面。 23 Servlet元素就是用來完成此項任務的--> 24 <servlet></servlet> 25 26 <!--服務器通常爲servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。 27 可是,經常會更改這個URL,以便servlet能夠訪問初始化參數或更容易地處理相對URL。 28 在更改缺省URL時,使用servlet-mapping元素--> 29 <servlet-mapping></servlet-mapping> 30 31 <!--若是某個會話在必定時間內未被訪問,服務器能夠拋棄它以節省內存。可經過使用HttpSession的 32 setMaxInactiveInterval方法明確設置單個會話對象的超時值,或者可利用session-config元素制定缺省超時值--> 33 <session-config></session-config> 34 35 <!--若是Web應用具備想到特殊的文件,但願能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證--> 36 <mime-mapping></mime-mapping> 37 38 <!--指示服務器在收到引用一個目錄名而不是文件名的URL時,使用哪一個文件--> 39 <welcome-file-list></welcome-file-list> 40 41 <!--在返回特定HTTP狀態代碼時,或者特定類型的異常被拋出時,可以制定將要顯示的頁面--> 42 <error-page></error-page> 43 44 <!--對標記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你可以更改TLD文件的位置, 45 而不用編輯使用這些文件的JSP頁面--> 46 <taglib></taglib> 47 48 <!--聲明與資源相關的一個管理對象--> 49 <resource-env-ref></resource-env-ref> 50 51 <!--聲明一個資源工廠使用的外部資源--> 52 <resource-ref></resource-ref> 53 54 <!--制定應該保護的URL。它與login-config元素聯合使用--> 55 <security-constraint></security-constraint> 56 57 <!--指定服務器應該怎樣給試圖訪問受保護頁面的用戶受權。它與sercurity-constraint元素聯合使用--> 58 <login-config></login-config> 59 60 <!--給出安全角色的一個列表,這些角色將出如今servlet元素內的security-role-ref元素的role-name子元素中。 61 分別地聲明角色可以使高級IDE處理安全信息更爲容易--> 62 <security-role></security-role> 63 64 <!--聲明Web應用的環境項--> 65 <env-entry></env-entry> 66 67 <!--聲明一個EJB的主目錄的引用--> 68 <ejb-ref></ejb-ref> 69 70 <!--聲明一個EJB的本地主目錄的應用--> 71 <ejb-local-ref></ejb-local-ref> 72 73 </web-app>
1.Web應用圖標:指出IDE和GUI工具用來表示Web應用的大圖標和小圖標html
1 <icon> 2 <small-icon>/images/app_small.gif</small-icon> 3 <large-icon>/images/app_large.gif</large-icon> 4 </icon>
2.Web 應用名稱:提供GUI工具可能會用來標記這個特定的Web應用的一個名稱java
<display-name>Tomcat Example</display-name>
3.Web 應用描述:給出於此相關的說明性文本web
<desciption>Tomcat Example servlets and JSP pages.</desciption>
4.上下文參數:聲明應用範圍內的初始化參數spring
1 <context-param> 2 <param-name>參數名</para-name> 3 <param-value>參數值</param-value> 4 <description>參數描述</description> 5 </context-param>
在servlet裏面能夠經過 getServletContext().getInitParameter(「context/param」)獲得sql
5.過濾器配置:將一個名字與一個實現javaxs.servlet.Filter接口的類相關聯數據庫
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.監聽器配置apache
1 <listener> 2 <listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class> 3 </listener>
7.Servlet配置tomcat
1 <servlet> 2 <servlet-name>servlet名稱</servlet-name> 3 <servlet-class>servlet類全路徑</servlet-class> 4 <init-param> 5 <param-name>參數名</param-name> 6 <param-value>參數值</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>指定當Web應用啓動時,裝載Servlet的次序</load-on-startup> 13 </servlet> 14 <servlet-mapping> 15 <servlet-name>servlet名稱</servlet-name> 16 <url-pattern>映射路徑</url-pattern> 17 </servlet-mapping>
8.會話超時配置(單位爲分鐘)安全
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.配置錯誤頁面
(1).經過錯誤碼來配置error-page
1 <!--配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp--> 2 <error-page> 3 <error-code>404</error-code> 4 <location>/NotFound.jsp</location> 5 </error-page>
(2).經過異常的類型配置error-page
1 <!--配置了當系統發生java.lang.NullException(即空指針異常)時,跳轉到錯誤處理頁面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.apache.org/tomcat/debug-taglib</taglib-uri> 3 <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location> 4 </taglib>
若是開發工具一直在報錯,應該把<taglib> 放到 <jsp-config>中
1 <jsp-config> 2 <taglib> 3 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri> 4 <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location> 5 </taglib> 6 </jsp-config>
13.資源管理對象配置
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>
配置數據庫鏈接池就可在此配置
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.登錄驗證配置
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元素給出安全角色的一個列表,這些角色將出如今servlet元素內的security-role-ref元素的role-name子元素中。
分別地聲明角色可以使高級IDE處理安全信息更爲容易。
1 <security-role> 2 <role-name>tomcat</role-name> 3 </security-role>
18.Web環境參數:env-entry元素聲明Web應用的環境項
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>
以上就是經常使用的web.xml中元素的配置以及做用了,歡迎提出異議和不適當的地方共同窗習。
爲了方便你們在移動端也能看到我分享的博文,現已註冊我的微信公衆號,掃描下方二維碼便可,歡迎你們關注,有時間會及時分享相關技術博文。
感謝您花時間閱讀此篇文章,若是您以爲這篇文章你學到了東西也是爲了犒勞下博主的碼字不易不妨打賞一下吧,讓樓主能喝上一杯咖啡,在此謝過了!
若是您以爲閱讀本文對您有幫助,請點一下「推薦」按鈕,您的「推薦」將是我最大的寫做動力!另外您也能夠選擇【關注我】,能夠很方便找到我!
本文版權歸做者和博客園共有,來源網址:http://www.cnblogs.com/hafiz 歡迎各位轉載,可是未經做者本人贊成,轉載文章以後必須在文章頁面明顯位置給出做者和原文鏈接,不然保留追究法律責任的權利!