欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tomcat web.xml配置

程序员文章站 2024-01-06 21:02:46
...

引用 http://blog.csdn.net/doupei2006/article/details/7939502

 

web.xml中关于Servlet、Filter、Listener的配置

 

分类: Java 2459人阅读 评论(1) 收藏 举报

(一)web.xml不同元素的加载顺序

加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。

web.xml 的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的

 

(二)web.xml文件详解

(2.1)

首先是schema

 

  1. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  
  4.     version="2.4">  
  5.   
  6. </web-app>  
<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></distributable>是指定该站台是否可分布式处理

(2.2)

如下方式设置web站点的环境参数

 

  1. <context-param>  
  2.     <param-name>my_param</param-name>  
  3.     <param-value>hello</param-value>  
  4. </context-param>  
<context-param>
    <param-name>my_param</param-name>
    <param-value>hello</param-value>
</context-param>

 

另外有如下方式设置环境变量

  1. <env-entry>      
  2.      <env-entry-name>minExemptions</env-entry-name>      
  3.      <env-entry-value>1</env-entry-value>      
  4.      <env-entry-type>java.lang.Integer</env-entry-type>      
  5. </env-entry>    
<env-entry>    
     <env-entry-name>minExemptions</env-entry-name>    
     <env-entry-value>1</env-entry-value>    
     <env-entry-type>java.lang.Integer</env-entry-type>    
</env-entry>  

 

 

在设置该参数my_param后,在servlet中可以用getServletContext().getInitParameter("my_param")来获得

 

(2.3)Filter过滤器

如下方式设置Filter,有元素<filter-name>、<filter-class>、<init-param>

 

  1. <filter>  
  2.         <filter-name>setCharacterEncoding</filter-name>  
  3.         <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>  
  4.         <init-param>  
  5.             <param-name>encoding</param-name>  
  6.             <param-value>GB2312</param-value>  
  7.         </init-param>  
  8. </filter>  
<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-mapping>要和<filter>配套使用设置如下出现在<filter>之后

 

 

  1. <filter-mapping>  
  2.         <filter-name>setCharacterEncoding</filter-name>  
  3.         <url-pattern>/*</url-pattern>  
  4. </filter-mapping>  
<filter-mapping>
        <filter-name>setCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>


(2.4)Listener监听器

 

如下方式设置设定Listener接口(主要有子元素<listener-class></listener-class>定义Listener的类名称

 

  1. <listener>  
  2.     <listener-class>com.myTest.ContextListener</listener-class>   
  3. </listener>  
<listener>
    <listener-class>com.myTest.ContextListener</listener-class> 
</listener>


(2.5)Servlet

 

如下方式定义servlet和servlet-mapping

 

  1. <servlet>  
  2.         <servlet-name>ShoppingServlet</servlet-name>   <!--指定servlet名称-->  
  3.         <servlet-class>com.myTest.ShoppingServlet</servlet-class>  <!--指定servlet的类名(实现)-->  
  4. </servlet>  
  5.       
  6. <servlet-mapping>  
  7.         <servlet-name>ShoppingServlet</servlet-name>  
  8.         <url-pattern>/shop/ShoppingServlet</url-pattern> <!--指定servlet所对应的url-->  
  9. </servlet-mapping>  
<servlet>
        <servlet-name>ShoppingServlet</servlet-name>   <!--指定servlet名称-->
        <servlet-class>com.myTest.ShoppingServlet</servlet-class>  <!--指定servlet的类名(实现)-->
</servlet>
    
<servlet-mapping>
        <servlet-name>ShoppingServlet</servlet-name>
        <url-pattern>/shop/ShoppingServlet</url-pattern> <!--指定servlet所对应的url-->
</servlet-mapping>

 

(2.6)定义session参数

<session-config></session-config> 用来定义web站台中的session参数,包含一个子元素:
 <session-timeout></session-timeout> 用来定义这个web站台所有session的有效期限,单位为( 分钟)

 

(2.7)定义某一个扩展名和某一个MIME Type做对映

 

  1. <mime-mapping>  
  2.     <extension>doc</extension>  <!--扩展的名称-->  
  3.     <mime-type>application/vnd.ms-word</mime-type>   <!--MIME格式-->  
  4. </mime-mapping>   
  5. <mime-mapping>  
  6.     <extension>xls</extension>  
  7.     <mime-type>application/vnd.ms-excel</mime-type>  
  8. </mime-mapping>  
<mime-mapping>
    <extension>doc</extension>  <!--扩展的名称-->
    <mime-type>application/vnd.ms-word</mime-type>   <!--MIME格式-->
</mime-mapping> 
<mime-mapping>
    <extension>xls</extension>
    <mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>


(2.8)定义首页列表(定义首页的文件名称(可有多个))

 

 

  1. <welcome-file-list>  
  2.     <welcome-file>index.jsp</welcome-file>    
  3.     <welcome-file>index.html</welcome-file>  
  4. </welcom-file-list>  
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>  
    <welcome-file>index.html</welcome-file>
</welcom-file-list>


(2.9)用来处理错误代码或异常的页面

 

3个子元素:<error-code></error-code> 指定错误代码
                      <exception-type></exception-type> 指定一个JAVA异常类型
                      <location></location> 指定在web站台内的相关资源路径

 

  1. <error-page>  
  2.     <error-code>404</error-code>  
  3.     <location>/error404.jsp</location>  
  4. </error-page>  
  5. <error-page>  
  6.     <exception-type>java.lang.Exception</exception-type>  
  7.     <location>/exception.jsp</location>  
  8. </error-page>  
<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>


(2.10)设定JSP网页所用到的Tag Library路径

 

2个子元素:<taglib-uri></taglib-uri> 定义TLD文件的URI,在JSP网页中用taglib指令便可取得该URI的TLD文件
                      <taglib-location></taglib-location> 指定TLD文件相对于web站台的存放位置

 

  1. <taglib>  
  2.       <taglib-uri>myTaglib</taglib-uri>  
  3.       <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>  
  4. </taglib>  
<taglib>
      <taglib-uri>myTaglib</taglib-uri>
      <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>

 

 

(2.11)定义利用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


可在此配置数据库连接

 

  1. <resource-ref>  
  2.         <description>JNDI JDBC DataSource of shop</description>  
  3.         <res-ref-name>jdbc/sample_db</res-ref-name>  
  4.         <res-type>javax.sql.DataSource</res-type>  
  5.         <res-auth>Container</res-auth>  
  6. </resource-ref>  
<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>

 

(2.12)<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。

例如:

 

  1. <jsp-config>  
  2.     <taglib>  
  3.         <taglib-uri>Taglib</taglib-uri>  
  4.         <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>  
  5.     </taglib>  
  6.     <jsp-property-group>  
  7.         <description>Special property group for JSP Configuration JSP example.</description>  
  8.         <display-name>JSPConfiguration</display-name>  
  9.         <url-pattern>/jsp/* </url-pattern>  
  10.         <el-ignored>true</el-ignored>  
  11.         <page-encoding>GB2312</page-encoding>  
  12.         <scripting-invalid>true</scripting-invalid>  
  13.         <include-prelude>/include/prelude.jspf</include-prelude>  
  14.         <include-coda>/include/coda.jspf</include-coda>  
  15.     </jsp-property-group>  
  16. </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中来进行相应的配置,来达到此目的。

步骤一:修改tomcat服务器的/conf/tomcat-users.xml,设置用户名和密码

以下是tomcat-users.xml的片段

  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <tomcat-users>  
  3.   <role rolename="tomcat"/>  
  4.   <role rolename="manager"/>  
  5.   <role rolename="admin"/>  
  6.   <user username="tomcat" password="tomcat" roles="tomcat"/>  
  7.   <user username="both" password="tomcat" roles="tomcat,manager"/>  
  8.   <user username="admin" password="admin" roles="admin"/>  
  9. </tomcat-users>  
<?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>


步骤二:修改web应用的web.xml,创建security-constraint、login-config和security-role元素

 

  1. <security-constraint>  
  2.       <web-resource-collection>  
  3.           <web-resource-name>HelloServlet</web-resource-name>  
  4.           <url-pattern>/HelloServlet</url-pattern>  
  5.           <http-method>GET</http-method>  
  6.           <http-method>POST</http-method>  
  7.       </web-resource-collection>  
  8.       <auth-constraint>  
  9.           <description>This applies only to the "tomcat" security role</description>  
  10.           <span style="color: rgb(255, 102, 102);"><role-name>admin</role-name></span>  
  11.       </auth-constraint>  
  12.       <user-data-constraint>  
  13.           <transport-guarantee>NONE</transport-guarantee>  
  14.       </user-data-constraint>  
  15.   </security-constraint>  
  16.     
  17.   <login-config>  
  18.       <auth-method>BASIC</auth-method>  
  19.   </login-config>  
  20.   <security-role>  
  21.       <span style="color: rgb(255, 102, 102);"><role-name>admin</role-name></span>  
  22.   </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等)

 

步骤三:在访问步骤二中的应用时,浏览器会要求输入用户名和密码,输入tomcat配置文件中设置的用户名、密码即可

 

验证总的步骤

1、检查提供的用户名和密码是否正确。
2、判断用户是否映射到特定的安全角色。例如,用户可能提供了正确的用户名和密码,但没有映射到特定的安全角色,也将被禁止访问特定的Web资源。
3、访问,输入用户名和密码

 

 

(四)配置EJB

EJB声明

  1. <ejb-ref>      
  2.      <description>Example EJB reference</decription>      
  3.      <ejb-ref-name>ejb/Account</ejb-ref-name>      
  4.      <ejb-ref-type>Entity</ejb-ref-type>      
  5.      <home>com.mycompany.mypackage.AccountHome</home>      
  6.      <remote>com.mycompany.mypackage.Account</remote>      
  7. </ejb-ref>      
<ejb-ref>    
     <description>Example EJB reference</decription>    
     <ejb-ref-name>ejb/Account</ejb-ref-name>    
     <ejb-ref-type>Entity</ejb-ref-type>    
     <home>com.mycompany.mypackage.AccountHome</home>    
     <remote>com.mycompany.mypackage.Account</remote>    
</ejb-ref>    


本地EJB声明

 

  1. <ejb-local-ref>      
  2.      <description>Example Loacal EJB reference</decription>      
  3.      <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>      
  4.      <ejb-ref-type>Session</ejb-ref-type>      
  5.      <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>      
  6.      <local>com.mycompany.mypackage.ProcessOrder</local>      
  7. </ejb-local-ref>  
<ejb-local-ref>    
     <description>Example Loacal EJB reference</decription>    
     <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>    
     <ejb-ref-type>Session</ejb-ref-type>    
     <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>    
     <local>com.mycompany.mypackage.ProcessOrder</local>    
</ejb-local-ref>


(五)配置DWR

 

  1. <servlet>      
  2.       <servlet-name>dwr-invoker</servlet-name>      
  3.       <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>      
  4. </servlet>      
  5. <servlet-mapping>      
  6.       <servlet-name>dwr-invoker</servlet-name>      
  7.       <url-pattern>/dwr/*</url-pattern>      
  8. </servlet-mapping>      
<servlet>    
      <servlet-name>dwr-invoker</servlet-name>    
      <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>    
</servlet>    
<servlet-mapping>    
      <servlet-name>dwr-invoker</servlet-name>    
      <url-pattern>/dwr/*</url-pattern>    
</servlet-mapping>    


(六)配置Struts

 

例如:

  1. <display-name>Struts Blank Application</display-name>      
  2.    <servlet>      
  3.        <servlet-name>action</servlet-name>      
  4.        <servlet-class>      
  5.            org.apache.struts.action.ActionServlet      
  6.        </servlet-class>      
  7.        <init-param>      
  8.            <param-name>detail</param-name>      
  9.            <param-value>2</param-value>      
  10.        </init-param>      
  11.        <init-param>      
  12.            <param-name>debug</param-name>      
  13.            <param-value>2</param-value>      
  14.        </init-param>      
  15.        <init-param>      
  16.            <param-name>config</param-name>      
  17.            <param-value>/WEB-INF/struts-config.xml</param-value>      
  18.        </init-param>      
  19.        <init-param>      
  20.            <param-name>application</param-name>      
  21.            <param-value>ApplicationResources</param-value>      
  22.        </init-param>      
  23.        <load-on-startup>2</load-on-startup>      
  24.    </servlet>      
  25.    <servlet-mapping>      
  26.        <servlet-name>action</servlet-name>      
  27.        <url-pattern>*.do</url-pattern>      
  28.    </servlet-mapping>      
  29.    <welcome-file-list>      
  30.        <welcome-file>index.jsp</welcome-file>      
  31.    </welcome-file-list>      
  32.   
  33.    <!-- Struts Tag Library Descriptors -->      
  34.    <taglib>      
  35.        <taglib-uri>struts-bean</taglib-uri>      
  36.        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>      
  37.    </taglib>      
  38.    <taglib>      
  39.        <taglib-uri>struts-html</taglib-uri>      
  40.        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>      
  41.    </taglib>      
  42.    <taglib>      
  43.    <taglib-uri>struts-nested</taglib-uri>      
  44.    <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>      
  45.    </taglib>      
  46.    <taglib>      
  47.        <taglib-uri>struts-logic</taglib-uri>      
  48.        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>      
  49.    </taglib>      
  50.    <taglib>      
  51.        <taglib-uri>struts-tiles</taglib-uri>      
  52.        <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>      
  53.    </taglib>      
 <display-name>Struts Blank Application</display-name>    
    <servlet>    
        <servlet-name>action</servlet-name>    
        <servlet-class>    
            org.apache.struts.action.ActionServlet    
        </servlet-class>    
        <init-param>    
            <param-name>detail</param-name>    
            <param-value>2</param-value>    
        </init-param>    
        <init-param>    
            <param-name>debug</param-name>    
            <param-value>2</param-value>    
        </init-param>    
        <init-param>    
            <param-name>config</param-name>    
            <param-value>/WEB-INF/struts-config.xml</param-value>    
        </init-param>    
        <init-param>    
            <param-name>application</param-name>    
            <param-value>ApplicationResources</param-value>    
        </init-param>    
        <load-on-startup>2</load-on-startup>    
    </servlet>    
    <servlet-mapping>    
        <servlet-name>action</servlet-name>    
        <url-pattern>*.do</url-pattern>    
    </servlet-mapping>    
    <welcome-file-list>    
        <welcome-file>index.jsp</welcome-file>    
    </welcome-file-list>    

    <!-- Struts Tag Library Descriptors -->    
    <taglib>    
        <taglib-uri>struts-bean</taglib-uri>    
        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>    
    </taglib>    
    <taglib>    
        <taglib-uri>struts-html</taglib-uri>    
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>    
    </taglib>    
    <taglib>    
    <taglib-uri>struts-nested</taglib-uri>    
    <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>    
    </taglib>    
    <taglib>    
        <taglib-uri>struts-logic</taglib-uri>    
        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>    
    </taglib>    
    <taglib>    
        <taglib-uri>struts-tiles</taglib-uri>    
        <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>    
    </taglib>    


(七)配置Spring(基本上都包含在Struts配置汇中)

 

  1.  <!-- 指定spring配置文件位置 -->      
  2.    <context-param>      
  3.       <param-name>contextConfigLocation</param-name>      
  4.       <param-value>      
  5.        <!--加载多个spring配置文件 -->      
  6.         /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml      
  7.       </param-value>      
  8.    </context-param>      
  9.   
  10.    <!-- 定义SPRING监听器,加载spring -->      
  11.   
  12. <listener>      
  13.      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      
  14. </listener>      
  15.   
  16. <listener>      
  17.      <listener-class>      
  18.        org.springframework.web.context.request.RequestContextListener      
  19.      </listener-class>      
  20. </listener>   
 <!-- 指定spring配置文件位置 -->    
   <context-param>    
      <param-name>contextConfigLocation</param-name>    
      <param-value>    
       <!--加载多个spring配置文件 -->    
        /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml    
      </param-value>    
   </context-param>    

   <!-- 定义SPRING监听器,加载spring -->    

<listener>    
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
</listener>    

<listener>    
     <listener-class>    
       org.springframework.web.context.request.RequestContextListener    
     </listener-class>    
</listener> 



 

 

 

 

 

 

 

 

 

相关标签: tomcat web.xml

上一篇:

下一篇: