BaoYaEr

tomcat web.xml配置

web.xml元素介绍

我将自己知道的web.xml的元素整理了一下:

web.xml首先是肯定要包含它的schema.

<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>


<jsp-config> 包括<taglib> 和<jsp-property-group> 两个子元素。
    
    其中<taglib>元素在JSP 1.2时就已经存在;而<jsp-property-group>是JSP 2.0 新增的元素。
    <jsp-property-group>元素主要有八个子元素,它们分别为:
    
    1.<description>:设定的说明;
    2.<display-name>:设定名称;
    3.<url-pattern>:设定值所影响的范围,如:/CH2 或 /*.jsp;
    4.<el-ignored>:若为true,表示不支持EL 语法;
    5.<scripting-invalid>:若为true,表示不支持<% scripting %>语法;
    6.<page-encoding>:设定JSP 网页的编码;
    7.<include-prelude>:设置JSP 网页的抬头,扩展名为.jspf;
    8.<include-coda>:设置JSP 网页的结尾,扩展名为.jspf。


一个简单的<jsp-config>元素完整配置:

<jsp-config>
    <taglib>
        <taglib-uri>Taglib</taglib-uri>
        <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
    </taglib>
    <jsp-property-group>
        <description>Special property group for JSP Configuration JSP example.</description>
        <display-name>JSPConfiguration</display-name>
        <url-pattern>/jsp/* </url-pattern>
        <el-ignored>true</el-ignored>
        <page-encoding>GB2312</page-encoding>
        <scripting-invalid>true</scripting-invalid>
        <include-prelude>/include/prelude.jspf</include-prelude>
        <include-coda>/include/coda.jspf</include-coda>
    </jsp-property-group>
</jsp-config>



配置web.xml来限制对某些servlet的请求

有时我们只希望通过认证的用户才能请求某些servlet的话,就可以在web.xml中来进行相应的配置,来达到此目的。

这就要用到<security-constraint></security-constraint>元素。
对于tomcat,中web.xml使用security-constraint元素需要在位于<Tomcat-installation-directory>/conf/tomcat-users.xml的XML文件中创建用户名和密码。比如下面的这个tomcat-users.xml文件:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,manager"/>
  <user username="admin" password="admin" roles="admin"/>
</tomcat-users>

此XML片段包括一个tomcat-users根元素,它包含一个或多个role和user元素。


然后在Web应用程序的web.xml中创建security-constraint、login-config和security-role元素。

<security-constraint>
      <web-resource-collection>
          <web-resource-name>HelloServlet</web-resource-name>
          <url-pattern>/HelloServlet</url-pattern>
          <http-method>GET</http-method>
          <http-method>POST</http-method>
      </web-resource-collection>
      <auth-constraint>
          <description>This applies only to the "tomcat" security role</description>
          <role-name>admin</role-name>
      </auth-constraint>
      <user-data-constraint>
          <transport-guarantee>NONE</transport-guarantee>
      </user-data-constraint>
  </security-constraint>
 
  <login-config>
      <auth-method>BASIC</auth-method>
  </login-config>
  <security-role>
      <role-name>admin</role-name>
  </security-role>

其中security-constraint元素包含一个或多个web-resource-collection元素,它是描述Web应用程序中的哪些web资源受到指定安全限制的保护。http-method元素指定安全限制覆盖的HTTP方法。上面的例子中,当我们对/HelloServlet的GET或POST请求时将触发配置的安全机制。
auth-constraint元素用于描述允许访问Web组件的安全角色。此例中安全角色的例子有tomcat、manager、admin。而只有当作为admin角色的用户才可以访问HelloServlet。

Web应用程序通过login-config元素来认证用户,并确认该用户是否为正确的角色。
longin-config包含的transport-guarantee子元素用来指定认证方法,BASIC是一种常见的Web认证方式,浏览器给用户提示一个对话框,要求输入用户名和密码,随后Tomcat将给出的用户名和密码与tomcat-users.xml中的用户名和密码进行比较,然后使用前面的security-constraint
配置来确定用户是否可访问受保护的servlet。

(除BASIC外,还可以是FORM、CLIENT-CERT、DIGEST等)

其实这种认证方法实际上有两个步骤:
1、检查提供的用户名和密码是否正确。
2、判断用户是否映射到特定的安全角色。例如,用户可能提供了正确的用户名和密码,但没有映射到特定的安全角色,也将被禁止访问特定的Web资源。



通过此次整理,对web.xml中元素有了更清楚的了解,以后继续努力...



发表于 2007-03-30 10:52 大田斗 阅读(30826) 评论(2)  编辑  收藏 所属分类: Strtus
 
评论
# 总结的不错
总结的不错
alaric 评论于 2012-05-12 21:21  回复  更多评论    
# re: tomcat web.xml配置
说的非常详细,收藏了
马甲 评论于 2013-08-23 10:23  回复  更多评论    
 
新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问   管理
相关文章:
  • struts中dispathAction
  • Java实现随机验证码功能实例
  • struts的ActionMessage以及html:errors不用资源文件
  • 分页pager-taglib使用指南
  • ServletContextListener || HttpSessionListener 的用法
  • Struts标签库
  • Struts结合Velocity开发web应用
  • 使用struts-menu制作树型菜单实例
  • Struts filter使用学习笔记
  • struts 的log4j配置
 
 
<2025年9月>
日一二三四五六
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

 导航

  • BlogJava
  • 首页
  • 发新随笔
  • 发新文章
  • 联系
  • 聚合
  • 管理

 统计

  • 随笔: 32
  • 文章: 427
  • 评论: 144
  • 引用: 0

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿(5)

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔档案

  • 2008年12月 (1)
  • 2008年4月 (2)
  • 2008年2月 (1)
  • 2008年1月 (1)
  • 2007年12月 (3)
  • 2007年11月 (1)
  • 2007年10月 (3)
  • 2007年7月 (2)
  • 2007年6月 (1)
  • 2007年4月 (2)
  • 2007年3月 (3)
  • 2007年2月 (5)
  • 2007年1月 (3)
  • 2006年12月 (4)

文章分类

  • axis(6) (rss)
  • eclipse(7) (rss)
  • Hibernate(30) (rss)
  • html/js/css(107) (rss)
  • java(106) (rss)
  • linux(7) (rss)
  • Lucene(7) (rss)
  • spring(36) (rss)
  • Spring CLOUd(1) (rss)
  • Strtus(30) (rss)
  • 其它(48) (rss)
  • 开源opensource(48) (rss)
  • 数据库DateBase(30) (rss)
  • 设计模式(12) (rss)

文章档案

  • 2018年8月 (1)
  • 2012年5月 (1)
  • 2012年4月 (2)
  • 2011年7月 (6)
  • 2010年3月 (1)
  • 2010年2月 (1)
  • 2010年1月 (3)
  • 2009年12月 (1)
  • 2009年10月 (1)
  • 2009年8月 (3)
  • 2009年3月 (1)
  • 2009年2月 (1)
  • 2008年12月 (3)
  • 2008年11月 (10)
  • 2008年10月 (3)
  • 2008年9月 (2)
  • 2008年8月 (2)
  • 2008年7月 (4)
  • 2008年6月 (13)
  • 2008年5月 (15)
  • 2008年4月 (9)
  • 2008年3月 (10)
  • 2008年1月 (18)
  • 2007年12月 (33)
  • 2007年11月 (6)
  • 2007年10月 (18)
  • 2007年9月 (10)
  • 2007年8月 (18)
  • 2007年7月 (15)
  • 2007年6月 (25)
  • 2007年5月 (19)
  • 2007年4月 (26)
  • 2007年3月 (38)
  • 2007年2月 (33)
  • 2007年1月 (27)
  • 2006年12月 (27)
  • 2006年11月 (12)

java

  • Ajax特效网站
  • cndiy nio
  • GRO
  • Hani Suleiman's blog
  • Java之路
  • java论坛
  • J道
  • mule
  • mule 入门
  • oksonic(动画教程)
  • 一路由你
  • 中国eclipse
  • 八进制
  • 在线源码
  • 多线程实战
  • 天火
  • 小米的blogjava
  • 幻境伯克----jface/swt
  • 很全的博克-强
  • 每日一得
  • 满江红
  • 邢红瑞
  • 飞翔
  • 鸟诗选(js)
  • 鸟食轩 (dhtml)

工具

  • apache中文手册
  • extjs学习
  • iconFindre
  • java 安全
  • javaresearch
  • java技巧网
  • js之王
  • matrix(study)
  • prototype api
  • spring中文
  • 北京IT企业速查
  • 在线流程图工具
  • 雅虎翻译

朋友

  • Happyshow
  • hibernate异常
  • skywalker
  • sunshow
  • xf
  • 亚光
  • 同云博客
  • 小弟鹏
  • 张玉磊
  • 昕
  • 李阳
  • 黄鸣

搜索

  •  

积分与排名

  • 积分 - 1105886
  • 排名 - 28

最新评论

  • 1. re: hibernate.cfg.xml配置
  • 好全啊 .. 棒棒哒 ~ !
  • --junqinag.yang
  • 2. re: Quartz任务调度快速入门
  • 我现在来看还是觉得不错
  • --小任
  • 3. re: js中this的总结
  • 评论内容较长,点击标题查看
  • --pam
  • 4. re: Quartz任务调度快速入门
  • 楼主辛苦
  • --yd
  • 5. re: Quartz任务调度快速入门
  • 顶了,内容写的很好
  • --sen

阅读排行榜

  • 1. 网页不缓存(3568)
  • 2. Form嵌套引起的问题 (2877)
  • 3. 解决IE下CSS背景图片闪烁的Bug(2461)
  • 4. Spring AOP的动态载入原理(2420)
  • 5. 如何制作漂亮的Excel表格(2059)

评论排行榜

  • 1. 北京户口--吃官司(5)
  • 2. 开始→运行→输入的命令集锦(3)
  • 3. 让网页上的所有图片动起来(2)
  • 4. Dom4j 编码问题彻底解决 (1)
  • 5. 心情不爽(1)

Powered by: 博客园
模板提供:沪江博客
Copyright ©2025 大田斗