web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
??? version="2.4">
</web-app>
其它的元素都放在<web-app></web-app>之中。
<discription></discription> 是對站臺的描述
<display-name></display-name> 定義站臺的名稱
<distributable/> 是指定該站臺是否可分布式處理
<context-param></context-param> 用來設定web站臺的環境參數,它包含兩個子元素:
??? <param-name></param-name> 用來指定參數的名稱
??? <param-value></param-value> 用來設定參數值
比如:
<context-param>
??? <param-name>my_param</param-name>
??? <param-value>hello</param-value>
</context-param>
在此設定的參數,可以在servlet中用 getServletContext().getInitParameter("my_param") 來取得
<filter></filter> 是用來聲明filter的相關設定,它包含以下子元素:
??? <filter-name></filter-name> 這當然就是指定filter的名字
??? <filter-class></filter-class> 這是用來定義filter的類的名稱
??? <init-param></init-param> 用來定義參數,它有兩個子元素:
??? ??? <param-name></param-name> 用來指定參數的名稱
?? ? ?? <param-value></param-value> 用來設定參數值
比如:
<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></filter>同時使用的是<filter-mapping></filter-mapping> 用來定義filter所對應的URL,它有兩個子元素:
??? <filter-name></filter-name> 指定filter的名字
??? <url-pattern></url-pattern> 指定filter所對應的URL
比如:
<filter-mapping>
??? ??? <filter-name>setCharacterEncoding</filter-name>
??? ??? <url-pattern>/*</url-pattern>
</filter-mapping>
<listener></listener> 用來設定Listener接口,它的主要子元素為
??? <listener-class></listener-class> 定義Listener的類名稱
比如:
<listener>
??? <listener-class>com.myTest.ContextListener</listener-class>
</listener>
<servlet></servlet> 用來聲明一個servlet的數據,主要有以下子元素:
??? <servlet-name></servlet-name> 指定servlet的名稱
??? <servlet-class></servlet-class> 指定servlet的類名稱
??? <jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑
??? <init-param></init-param> 用來定義參數,和前面的<init-param>差不多
同樣,與<servlet></servlet>一起使用的是<servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素:
??? <servlet-name></servlet-name> 指定servlet的名稱
??? <url-pattern></url-pattern> 指定servlet所對應的URL
比如:
<servlet>
??????? <servlet-name>ShoppingServlet</servlet-name>
??????? <servlet-class>com.myTest.ShoppingServlet</servlet-class>
</servlet>
???
<servlet-mapping>
??????? <servlet-name>ShoppingServlet</servlet-name>
??????? <url-pattern>/shop/ShoppingServlet</url-pattern>
</servlet-mapping>
<session-config></session-config> 用來定義web站臺中的session參數,包含一個子元素:
??? <session-timeout></session-timeout> 用來定義這個web站臺所有session的有效期限,單位為分鐘
<mime-mapping></mime-mapping> 定義某一個擴展名和某一個MIME Type做對映,包含兩個子元素:
??? <extension></extension> 擴展名的名稱
??? <mime-type></mime-type> MIME格式
比如:
<mime-mapping>
??? <extension>doc</extension>
??? <mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
??? <extension>xls</extension>
??? <mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
<welcome-file-list></welcom-file-list> 用來定義首頁的列單,包含一個子元素:
??? <welcome-file></welcome-file> 指定首頁的文件名稱
比如:
<welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
??? <welcome-file>index.html</welcome-file>
</welcom-file-list>
<error-page></error-page> 用來處理錯誤代碼或異常的頁面,有三個子元素:
??? <error-code></error-code> 指定錯誤代碼
??? <exception-type></exception-type> 指定一個JAVA異常類型
??? <location></location> 指定在web站臺內的相關資源路徑
比如:
<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>
<taglib></taglib> 用來設定JSP網頁所用到的Tag Library路徑,有兩個子元素:
??? <taglib-uri></taglib-uri> 定義TLD文件的URI,在JSP網頁中用taglib指令便可取得該URI的TLD文件
??? <taglib-location></taglib-location> 指定TLD文件相對于web站臺的存放位置
比如:
<taglib>
? ??? <taglib-uri>myTaglib</taglib-uri>
? ??? <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<resource-ref></resource-ref> 定義利用JNDI取得站臺可利用的資源,有五個子元素:
??? <description></description> 資源說明
??? <rec-ref-name></rec-ref-name> 資源名稱
??? <res-type></res-type> 資源種類
??? <res-auth></res-auth> 資源經由Application或Container來許可
??? <res-sharing-scope></res-sharing-scope> 資源是否可以共享,有Shareable和Unshareable兩個值,默認為Shareable
比如,配置數據庫連接池就可在此配置:
<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>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
??? version="2.4">
</web-app>
其它的元素都放在<web-app></web-app>之中。
<discription></discription> 是對站臺的描述
<display-name></display-name> 定義站臺的名稱
<distributable/> 是指定該站臺是否可分布式處理
<context-param></context-param> 用來設定web站臺的環境參數,它包含兩個子元素:
??? <param-name></param-name> 用來指定參數的名稱
??? <param-value></param-value> 用來設定參數值
比如:
<context-param>
??? <param-name>my_param</param-name>
??? <param-value>hello</param-value>
</context-param>
在此設定的參數,可以在servlet中用 getServletContext().getInitParameter("my_param") 來取得
<filter></filter> 是用來聲明filter的相關設定,它包含以下子元素:
??? <filter-name></filter-name> 這當然就是指定filter的名字
??? <filter-class></filter-class> 這是用來定義filter的類的名稱
??? <init-param></init-param> 用來定義參數,它有兩個子元素:
??? ??? <param-name></param-name> 用來指定參數的名稱
?? ? ?? <param-value></param-value> 用來設定參數值
比如:
<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></filter>同時使用的是<filter-mapping></filter-mapping> 用來定義filter所對應的URL,它有兩個子元素:
??? <filter-name></filter-name> 指定filter的名字
??? <url-pattern></url-pattern> 指定filter所對應的URL
比如:
<filter-mapping>
??? ??? <filter-name>setCharacterEncoding</filter-name>
??? ??? <url-pattern>/*</url-pattern>
</filter-mapping>
<listener></listener> 用來設定Listener接口,它的主要子元素為
??? <listener-class></listener-class> 定義Listener的類名稱
比如:
<listener>
??? <listener-class>com.myTest.ContextListener</listener-class>
</listener>
<servlet></servlet> 用來聲明一個servlet的數據,主要有以下子元素:
??? <servlet-name></servlet-name> 指定servlet的名稱
??? <servlet-class></servlet-class> 指定servlet的類名稱
??? <jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑
??? <init-param></init-param> 用來定義參數,和前面的<init-param>差不多
同樣,與<servlet></servlet>一起使用的是<servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素:
??? <servlet-name></servlet-name> 指定servlet的名稱
??? <url-pattern></url-pattern> 指定servlet所對應的URL
比如:
<servlet>
??????? <servlet-name>ShoppingServlet</servlet-name>
??????? <servlet-class>com.myTest.ShoppingServlet</servlet-class>
</servlet>
???
<servlet-mapping>
??????? <servlet-name>ShoppingServlet</servlet-name>
??????? <url-pattern>/shop/ShoppingServlet</url-pattern>
</servlet-mapping>
<session-config></session-config> 用來定義web站臺中的session參數,包含一個子元素:
??? <session-timeout></session-timeout> 用來定義這個web站臺所有session的有效期限,單位為分鐘
<mime-mapping></mime-mapping> 定義某一個擴展名和某一個MIME Type做對映,包含兩個子元素:
??? <extension></extension> 擴展名的名稱
??? <mime-type></mime-type> MIME格式
比如:
<mime-mapping>
??? <extension>doc</extension>
??? <mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
??? <extension>xls</extension>
??? <mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
<welcome-file-list></welcom-file-list> 用來定義首頁的列單,包含一個子元素:
??? <welcome-file></welcome-file> 指定首頁的文件名稱
比如:
<welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
??? <welcome-file>index.html</welcome-file>
</welcom-file-list>
<error-page></error-page> 用來處理錯誤代碼或異常的頁面,有三個子元素:
??? <error-code></error-code> 指定錯誤代碼
??? <exception-type></exception-type> 指定一個JAVA異常類型
??? <location></location> 指定在web站臺內的相關資源路徑
比如:
<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>
<taglib></taglib> 用來設定JSP網頁所用到的Tag Library路徑,有兩個子元素:
??? <taglib-uri></taglib-uri> 定義TLD文件的URI,在JSP網頁中用taglib指令便可取得該URI的TLD文件
??? <taglib-location></taglib-location> 指定TLD文件相對于web站臺的存放位置
比如:
<taglib>
? ??? <taglib-uri>myTaglib</taglib-uri>
? ??? <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<resource-ref></resource-ref> 定義利用JNDI取得站臺可利用的資源,有五個子元素:
??? <description></description> 資源說明
??? <rec-ref-name></rec-ref-name> 資源名稱
??? <res-type></res-type> 資源種類
??? <res-auth></res-auth> 資源經由Application或Container來許可
??? <res-sharing-scope></res-sharing-scope> 資源是否可以共享,有Shareable和Unshareable兩個值,默認為Shareable
比如,配置數據庫連接池就可在此配置:
<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>