<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Sample Application</display-name> <description>This is a sample application</description> <filter> <!--過濾器名,可以隨便取,當web應用中有多個過濾器時不允許重名.--> <filter-name>SampleFilter</filter-name> <!--具體的過濾器的類的完整的包名+類名。注意:不能寫錯了。否則容器不能正確的實例化過濾器--> <filter-class>mypack.SampleFilter</filter-class> <init-param> <!-- 參數名 --> <param-name>initParam1</param-name> <!-- 參數值 --> <param-value>2</param-value> </init-param> </filter> <!-- Define the SampleFilter Mapping --> <filter-mapping> <!--過濾器名,注意要和上面的<filter-name>裏的名字一樣。--> <filter-name>SampleFilter</filter-name> <!-- 指定過濾器負責過濾的URL。這裏指定了*.jsp表示在訪問任何一個jsp頁面時都會先使用mypack.SampleFilter過濾器進行過濾。如果寫成login.jsp.則只有在訪問login.jsp時纔會調用該過濾器進行過濾。--> <url-pattern>*.jsp</url-pattern> </filter-mapping> <servlet> <!-- Servlet名字,可以隨便取,有多個Servlet時不允許重名--> <servlet-name>SampleServlet</servlet-name> <!--指定實現這個Servlet的類。完整的包名+類名--> <servlet-class>mypack.SampleServlet</servlet-class> <!--定義Servlet的初始化參數(包括參數名和參數值)一個<servlet>元素裏可以有多個<init-param>元素。在Servlet類中通過ServletConfig類的來訪問這些參數。 --> <init-param> <!-- 參數名 --> <param-name>initParam1</param-name> <!-- 參數值 --> <param-value>2</param-value> </init-param> <!--指定當前Web應用啓動時裝載Servlet的次序。當這個數>=0時,容器會按數值從小到大依次加載。如果數值<0或沒有指定,容器將載Web客戶首次訪問這個Servlet時加載。--> <load-on-startup>1</load-on-startup> </servlet> <!-- Define the SampleServlet Mapping --> <servlet-mapping> <!--必須和<servlet>裏的<servlet-name>內容一樣--> <servlet-name>SampleServlet</servlet-name> <!--指定訪問這個Servlet的URL。這裏給出的是對於整個Web應用的相對URL路徑。--> <url-pattern>/sample</url-pattern> </servlet-mapping> <session-config> <!--設 定HttpSession的生命週期。這裏以分鐘計算。下面的設定指明Session在最長不活動時間爲10分鐘。過了這個時間,Servlet容器將它 作爲無效處理。注意這裏和程序裏指定的計數單位不同,程序裏是以秒爲單位。<session-config>只有<session- timeout>這個元素--> <session-timeout>10</session-timeout> </session-config> <!-- 在 用戶訪問Web應用時,如果僅給出Web應用的根訪問URL,沒有指定具體的文件名,容器會調用<weblcome-file- list> 元素裏指定的文件清單。<welcome-file-list>裏允許有多個<welcome-file>元 素,每個元素代表一個文件。容器會先找第一文文件件是否存在,如果存在這把這個文件返回個客戶,不再進行其他文件的查找。如果不存在則找第二個文件,依次 類推。如果所有文件都不存在,則跑出404錯誤--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list> <!-- 設置Web應用引用的自定義標籤庫。下面的代碼定義了一個/mytaglib標籤庫,它對應的TLD文件爲/WEB-INF/mytaglib.tld --> <taglib> <taglib-uri>/mytaglib</taglib-uri> <taglib-location>/WEB-INF/mytaglib.tld</taglib-location> </taglib> <!-- 如果Web應用訪問了由Servlet容器管理的某個JNDI Resource必須在這裏聲明對JNDI Resource的引用 --> <resource-ref> <!-- 對應用資源的說明 --> <description>DB Connection</description> <!-- 指定所引用資源的JNDI名字 --> <res-ref-name>jdbc/sampleDb</res-ref-name> <!-- 指定所引用資源的類名字 --> <res-type>javax.sql.DataSource</res-type> <!-- 指定管理所引用資源的Manager, 它有兩個可選值:Container和Application.Container表示由容器來創建和管理Resource,Application表示由Web應用來管理和創建Resource --> <res-auth>Container</res-auth> </resource-ref> <security-constraint> <web-resource-collection> <!-- 這個名字是必須的,由工具使用,別的地方不使用 --> <web-resource-name>my application</web-resource-name> <!-- 指定要受約束的資源,至少有一個。可以有多個. --> <url-pattern>/*</url-pattern> <!-- 描 述了度可與URL模式指定的資源哪些方法是受約束的,如果沒有<http-method>元素,表示任何角色的人都無法訪問任何http的方 法 。這裏放置了GET方法,表示只有GET方法是受約束的。其他任何角色的人可以訪問POST和其他的方法。但不能訪問GET方法。--> <http-method>GET</http-method> </web-resource-collection> <!-- 如果沒有<auth-constraint>表示所有角色都能訪問GET方法,如果是<auth-constraint/>表示任何角色都不能訪問GET方法 --> <auth-constraint> <!-- 可選的。表示哪些角色能夠在指定的資源上調用受約束的方法。這裏表示只有擁有Admin和Member角色的人能夠訪問GET方法 <security-role>>裏的<role-name>值一樣 --> <role-name>Admin</role-name> <role-name>Member</role-name> </auth-constraint> </security-constraint> <!-- 將指定的角色映射到web.xml裏 --> <security-role> <description>The role that is required to log into the my Application </description> <!-- 以下的角色和tomcat-users.xml裏的<tomcat-users>裏的<role rolename=""/>裏的rolename屬性值對應 --> <role-name>Guest</role-name> </security-role> <!-- 如果要想進行認證,必須有<login-config>--> <!-- ....... --> </web-app>
配置文件附件左下角