web.xml 配置

元素 描述
<web-app> 顶级标签,包含所有下面的元素
<display-name> WEB 应用程序的名称
<description> WEB 应用程序的说明
<filter> Action 列表
<filter-mapping> URL 过滤器,对应到 Action 名称
<servlet> Servlet 列表及启动参数
<servlet-mapping> Servlet 映射,与 URL 进行匹配
<session-config> SESSION 配置,如过期分钟数
<welcome-file-list> 默认首页列表
<taglib> 标签库列表
<resource-ref> 配置资源引用
<security-constraint> 配置安全约束
<login-config> 配置登陆页面

示例:

<?xml version="1.0" encoding="GB2312"?>
<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>

<filter>

<!--
filter 配置Servlet过滤器
filter-name 定义过滤器的名字。当有多个过滤器时,不能同名
filter-class 指定实现这一过滤的类,这个类负责具体的过滤事务
-->
<filter>
<filter-name>SampleFilter</filter-name>
<filter-class>mypack.SampleFilter</filter-class>
</filter>
元素 描述
<filter-name> Action 名称
<filter-class> Action 类全称

<filter-mapping>

<!--
filter-mapping 设定过滤器负责过滤的URL
filter-name 过滤器名。这里的名字一定要和filter中的过滤器名匹配
url-pattern 指定过滤器负责过滤的URL
-->
<filter-mapping>
<filter-name>SampleFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
元素 描述
<filter-name> Action 名称
<filter-class> 对应的 URL 匹配

<servlet>

<!--
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-name> servlet 名称
<servlet-class> servlet 类全名
<init-param> 初始化参数
<param-name> 初始化参数名
<param-value> 初始化参数值
<load-on-startup> 指定是否自动初始化

<servlet-mapping>

<!--
配置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>
元素 描述
<servlet-name> servlet 名称
<url-pattern> 对应的 URL 匹配

<session-config>

<!--配置session session用来设定HttpSession的生命周期。单位(秒)-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
元素 描述
<session-timeout> 设置 SESSION 过期时间,分钟

<welcome-file-list>

<!--配置Wel0come0文件清单-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
元素 描述
<welcome-file> 首页地址

<taglib>

<!--
配置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>
元素 描述
<taglib-uri> 标签库别名
<taglib-location> 标签库文件路径

 

 

 

<!--
配置资源引用
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>