web.xml 不是web工程必須的。
web.xml文件用來配置那些東西:歡迎頁,servlet,filter等。
web.xml文件中定義了多少種標簽元素,web.xml 中就可以出現它的模式文件所定義的標簽元素。每個web.xml文件的根元素<web-app>中,都必須標明這個web.xml使用的是哪個模式文件,如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://Java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
</web-app>
web.xml中標簽的元素的種類會越來越多,但有些是不常用的,我們只需要記住一些常用的就可以了。
列出常用的標簽元素記這些元素的功能:
1、指定歡迎頁,指定多個,會先從第一個開始找
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index1.jsp</welcome-file>
</welcome-file-list>
對於tomcat來說,當你只指定一個web的根名,沒有指定具體頁面,去訪問時一個web時,如果web.xml文件中配置了歡迎頁,那么就返回指定的那個頁面作為歡迎頁,而在文中沒有web.xml文件,或雖然有web.xml,但web.xml也沒指定歡迎頁的情況下,它默認先查找index.html文件,如果找到了,就把index.html作為歡迎頁還回給瀏覽器。如果沒找到index.html,tomcat就去找index.jsp。找到index.jsp就把它作為歡迎頁面返回。而如果index.html和index.jsp都沒找到,又沒有用web.xml文件指定歡迎頁面,那此時tomcat就不知道該返回哪個文件了,它就顯示The requested resource (/XXX) is not available的頁面。其中XXX表示web的根名。但如果你指定了具體頁面,是可以正常訪問的。
2、錯誤頁面,可以是錯誤編碼,也可以是異常類型
<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>
3.上下文配置,param-name設定上下文的參數名稱。必須是唯一名稱。param-value:設定上下文參數名稱的值
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:**/applicationContext.xml</param-value>
</context-param>