关键字: tomcat, web.xml说明, web.xml 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" "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 配置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>