Tomcat中web.xml文件的詳細說明
<?xml version="1.0" encoding="GB2312"?> <!--
Web.xml依次定議了如下元素:
<web-app>
<display-name></display-name> 定義了WEB應用的名字
<description></description> 聲明WEB應用的描述信息
<filter></filter>
<filter-mapping></filter-mapping>
<servlet></servlet>
<servlet-mapping></servlet-mapping>
<session-config></session-config>
<welcome-file-list></welcome-file-list>
<taglib></taglib>
<resource-ref></resource-ref>
<security-constraint></security-constraint>
<login-config></login-config>
</web-app>
在web.xml中元素定義的先后順序不能顛倒,否則Tomcat服務器可能會拋出SAXParseException.
-->
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN" "
<web-app>
<display-name>Sample Application</display-name>
<description>
This is a Sample Application
</description>
<!--
filter 配置Servlet過濾器
filter-name 定義過濾器的名字。當有多個過濾器時,不能同名
filter-class 指定實現這一過濾的類,這個類負責具體的過濾事務
-->
<filter>
<filter-name>SampleFilter</filter-name>
<filter-class>mypack.SampleFilter</filter-class>
</filter>
<!--
filter-mapping 設定過濾器負責過濾的URL
filter-name 過濾器名。這里的名字一定要和filter中的過濾器名匹配
url-pattern 指定過濾器負責過濾的URL
-->
<filter-mapping>
<filter-name>SampleFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!--
servlet 配置Servlet.
servlet-name 定義Servlet的名字
servlet-class 指定實現這個servlet的類
init-param 定義Servlet的初始化參數和參數值,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數
load-on-startup 指定當Web應用啟動時,裝載Servlet的次序。
當值為正數或零時:Servlet容器先加載數值小的servlet,再依次加載其他數值大的servlet.
當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它
-->
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>mypack.SampleServlet</servlet-class>
<init-param>
<param-name>initParam1</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--
配置servlet映射(下面代碼為SampleServlet指定的相對URL為"/sample":
servlet-name 指定servlet的名字,這里的名字應該和<Servlet>元素中定義的名字匹配。
url-pattern 指定訪問這個servlet的URL。只需給出相對路徑。
-->
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/sample</url-pattern>
</servlet-mapping>
<!--配置session session用來設定HttpSession的生命周期。單位(秒)-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!--配置Wel0come0文件清單-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<!--
配置Tag Library
taglib-uri 設定Tag Library的唯一標識符,在Web應用中將根據這一標識符來引用Tag Library
taglib-location 指定和Tag Library對應的TLD文件的位置
-->
<taglib>
<taglib-uri>/mytaglib</taglib-uri>
<taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
</taglib>
<!--
配置資源引用
description 對所引用的資源的說明
res-ref-name 指定所引用資源的JNDI名字
res-type 指定所引用資源的類名字
res-auth 指定管理所引用資源的Manager,它有兩個可選值:
Container:由容器來創建和管理resource
Application:同WEB應用來創建和管理Resource
-->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/sampleDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!--
配置安全約束(以下代碼指定當用戶訪問該WEB應用下的所有資源時,必須具備guest角色)
web-resource-collection 聲明受保護的WEB資源
auth-constraint 聲明可以訪問受保護資源的角色,可以包含多個<role-name>子元素
web-resource-name 標識受保護的WEB資源
url-pattern 指定受保護的URL路徑
-->
<Security-constraint>
<web-resource-collection>
<web-resource-name>sample appliction</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>guest</role-name>
</auth-constraint>
</Security-constraint>
<!--
配置安全驗證登錄界面:指定當WEB客戶訪問受保護的WEB資源時,系統彈出的登錄對話框的類型。
auth-method 指定驗證方法,它有三個可選值:BASIC(基本驗證)、DIGEST(摘要驗證)、FORM(表單驗證)
realm-name 設定安全域的名稱
form-login-config 當驗證方法為FORM時,配置驗證網頁和出錯網頁
form-login-page 當驗證方法為FORM時,設定驗證網頁
form-error-page 當驗證方法為FORM時,設定出錯網頁
-->
<login-config>
<auth-method>FORM</auth-method>
<realm-name>
Tomcat Server Configuration form-Based Authentication Area
</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
<!--配置對安全驗證角色的引用-->
<security-role>
<description>
The role that is required to log into the sample application
</description>
<role-name>guest</role-name>
</security-role>
</web-app>