1 <web-app> 2 3 <!--定義了WEB應用的名字--> 4 <display-name></display-name> 5 6 <!--聲明WEB應用的描述信息--> 7 <description></description> 8 9 <!--context-param元素聲明應用范圍內的初始化參數(shù)-->10 <context-param></context-param>11 12 <!--過濾器元素將一個名字與一個實現(xiàn)javax.servlet.Filter接口的類相關聯(lián)-->13 <filter></filter>14 15 <!--一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯(lián)-->16 <filter-mapping></filter-mapping>17 18 <!--servlet API的版本2.3增加了對事件監(jiān)聽程序的支持,事件監(jiān)聽程序在建立、修改和刪除會話或servlet環(huán)境時得到通知。19 Listener元素指出事件監(jiān)聽程序類-->20 <listener></listener>21 22 <!--在向servlet或JSP頁面制定初始化參數(shù)或定制URL時,必須首先命名servlet或JSP頁面。23 Servlet元素就是用來完成此項任務的-->24 <servlet></servlet>25 26 <!--服務器一般為servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。27 但是,常常會更改這個URL,以便servlet可以訪問初始化參數(shù)或更容易地處理相對URL。28 在更改缺省URL時,使用servlet-mapping元素-->29 <servlet-mapping></servlet-mapping>30 31 <!--如果某個會話在一定時間內未被訪問,服務器可以拋棄它以節(jié)省內存??赏ㄟ^使用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狀態(tài)代碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面-->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元素聯(lián)合使用-->55 <security-constraint></security-constraint>56 57 <!--指定服務器應該怎樣給試圖訪問受保護頁面的用戶授權。它與sercurity-constraint元素聯(lián)合使用-->58 <login-config></login-config>59 60 <!--給出安全角色的一個列表,這些角色將出現(xiàn)在servlet元素內的security-role-ref元素的role-name子元素中。61 分別地聲明角色可使高級IDE處理安全信息更為容易-->62 <security-role></security-role>63 64 <!--聲明Web應用的環(huán)境項-->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工具用來表示W(wǎng)eb應用的大圖標和小圖標
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應用的一個名稱
<display-name>Tomcat Example</display-name>
3.Web 應用描述:給出于此相關的說明性文本
<desciption>Tomcat Example servlets and JSP pages.</desciption>
4.上下文參數(shù):聲明應用范圍內的初始化參數(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.過濾器配置:將一個名字與一個實現(xiàn)javaxs.servlet.Filter接口的類相關聯(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>指定當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 <!--配置了當系統(tǒng)發(fā)生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp-->2 <error-page>3 <error-code>404</error-code>4 <location>/NotFound.jsp</location>5 </error-page>
(2).通過異常的類型配置error-page
1 <!--配置了當系統(tǒng)發(fā)生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>
如果開發(fā)工具一直在報錯,應該把<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>
配置數(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.登陸驗證配置
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元素給出安全角色的一個列表,這些角色將出現(xiàn)在servlet元素內的security-role-ref元素的role-name子元素中。
分別地聲明角色可使高級IDE處理安全信息更為容易。
1 <security-role>2 <role-name>tomcat</role-name>3 </security-role>
18.Web環(huán)境參數(shù):env-entry元素聲明Web應用的環(huán)境項
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中元素的配置以及作用了,歡迎提出異議和不適當?shù)牡胤焦餐瑢W習。