Tomcat的web.xml详解

参考地址地址:https://blog.csdn.net/m751075306/article/details/9452893

                        https://blog.csdn.net/wangxiaotongfan/article/details/51318951

                        http://www.cnblogs.com/hafiz/p/5715523.html

    web.xml用来初始化工程配置信息,比如filter,listener,servlet,servlet-mapping,启动加载级别等等

   web.xml的模式文件是由Sun 公司定义的,每个web.xml文件的根元素为<web-app>中,必须标明这个web.xml使用的是哪个模式文件。

   web.xml的根元素定义如下所示:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3. xmlns="http://java.sun.com/xml/ns/javaee"  
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7. </web-app>  

     下面就来介绍一下web.xml中常用的标签及其功能

1.<description>,<display-name>,<icon>

<description>项目描述</discription>  对项目做出描述.

<display-name>项目名称</display-name> 定义项目的名称.

    <icon> icon元素包含small-icon和large-icon两个子元素.用来指定web站台中小图标和大图标的路径.

        <small-icon>/路径/smallicon.gif</small-icon>small-icon元素应指向web站台中某个小图标的路径,大小为16 X 16 pixel,但是图象文件必须为GIF或JPEG格式,扩展名必须为:.gif或 .jpg.

        <large-icon>/路径/largeicon-jpg</large-icon> large-icon元素应指向web站台中某个大图表路径,大小为32 X 32 pixel,但是图象文件必须为GIF或JPEG的格式,扩展名必须为; gif 或jpg.

    例如:

[html]  view plain  copy
  1. <display-name>Develop Example</display-name>    
  2. <description>JSP 2.0 Tech Book's Examples</description>   
  3.  <icon>    
  4.    <small-icon>/images/small.gif</small-icon>      
  5.    <large-icon>/images/large.gir</large-icon>   
  6.  </icon>  
2.<context-param>
    <context-param>元素含有一对参数名和参数值,用作应用的servlet上下文初始化参数。参数名在整个Web应用中必须是惟一的。
    context-param 元素用来设定web应用的环境参数(context),它包含两个子元素: param-name和param-value. 
        <param-name>参数名称</param-name> 设定Context名称 
        <param-value>值</param-value> 设定Context名称的值 </context-param> 
    例如:

[html]  view plain  copy
  1. <context-param>  
  2.    <param-name>param_name</param-name>  
  3.    <param-value>param_value</param-value>  
  4. </context-param>  
    此所设定的参数,在JSP网页中可以使用下列方法来取得:${initParam.param_name}
    若在Servlet可以使用下列方法来获得:
        String param_name=getServletContext().getInitParamter("param_name");

3.<filter>

filter元素用于指定Web容器中的过滤器。

在请求和响应对象被servlet处理之前或之后,可以使用过滤器对这两个对象进行操作。

利用下一节介绍 的filter-mapping元素,过滤器被映射到一个servlet或一个URL模式。

这个过滤器的filter元素和filter-mapping 元素必须具有相同的名称。

    filter元素用来声明filter的相关设定.filter元素除了下面介绍的的子元素之外,还包括之前介绍过的<icon>,<display-name>,<description>,<init-param>,其用途一样.
    下面介绍filter-name,filter-class和init-param元素
        init-param元素与context-param 元素具有相同的元素描述符。
        filter-name元素用来定义过滤器的名称,该名称在整个应用中都必须是惟一的。
        filter-class元素指定过滤 器类的完全限定的名称。


    <filter-name>Filter的名称</filter-name> 定义Filter的名称.
    <filter-class>Filter的类名称</filter-class> 定义Filter的类名称.例如:com.foo.hello
    例如:
[html]  view plain  copy
  1. <filter>  
  2.   <filter-name>setCharacterEncoding</filter-name>  
  3.   <filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>  
  4.   <init-param>  
  5.      <param-name>encoding</param-name>  
  6.      <param-value>GB2312</param-value>  
  7.   </init-param>  
  8. </filter>  

4.<filter-mapping>

    filter-mapping元素用来声明Web应用中的过滤器映射。过滤器可被映射到一个servlet或一个URL模式。将过滤器映射到一个 servlet中会造成过滤器作用于servlet上。将过滤器映射到一个URL模式中则可以将过滤器应用于任何资源,只要该资源的URL与URL模式匹配。过滤是按照部署描述符的filter-mapping元素出现的顺序执行的。

    filter-mapping 元素的两个主要子元素filter-name和url-pattern.用来定义Filter所对应的URL。还有servlet-name和dispatcher子元素,不是很常用。

        <filter-name>Filter的名称</filter-name>  定义Filter的名称.

        <url-pattern>URL</url-pattern> Filter所对应的RUL.例如:<url-pattern>/Filter/Hello</url-pattern>
        <servlet-name>Servlet的名称<servlet-name> 定义servlet的名称.
        <dispatcher>REQUEST|INCLUDE|FORWARD|ERROR</disaptcher> 设定Filter对应的请求方式,有RQUEST,INCLUDE,FORWAR,ERROR四种,默认为REQUEST.

例如:

[html]  view plain  copy
  1. <filter-mapping>  
  2.    <filter-name>GZIPEncoding</filter-name>  
  3.    <url-pattern>/*</url-pattern>  
  4. </filter-mapping>  

完整的filter配置例子如下:

[html]  view plain  copy
  1. <filter>  
  2.     <filter-name>struts2</filter-name>  
  3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.     <filter-name>struts2</filter-name>  
  7.     <url-pattern>/*</url-pattern>  
  8. </filter-mapping>  

5.<servlet>

   在web.xml中完成的一个最常见的任务是对servlet或JSP页面给出名称和定制的URL。用servlet元素分配名称,使用servlet-mapping元素将定制的URL与刚分配的名称相关联。

例如:
[html]  view plain  copy
  1. <servlet>   
  2.   <servlet-name>Test</servlet-name>   
  3.   <servlet-class>com.moreservlets.TestServlet</servlet-class>   
  4. </servlet>   
    这表示位于WEB-INF/classes/com/moreservlets/TestServlet的servlet已经得到了注册名Test。

6.<servlet-mapping>

    servlet-mapping元素包含两个子元素servlet-name和url-pattern.用来定义servlet所对应URL.
        <servlet-name>Servlet的名称</servlet-name> 定义Servlet的名称.
        <url-pattern>Servlet URL</url-pattern> 定义Servlet所对应的RUL.例如:<url-pattern>/Servlet/Hello</url-pattern>
例如:
[html]  view plain  copy
  1. <servlet-mapping>  
  2.    <servlet-name>LoginChecker</servlet-name>  
  3.    <url-pattern>/LoginChecker</url-pattern>  
  4. </servlet-mapping>  
完整的servlet配置如下所示:
[html]  view plain  copy
  1. <servlet>  
  2.   <servlet-name>ServletName</servlet-name>     
  3.   <servlet-class>xxxpackage.xxxServlet</servlet-class>   <!--Servlet的类-->  
  4.   <init-param>                                     <!--初始化一个变量,可看成全局变量,可省略-->  
  5.     <param-name>参数名称</param-name>              <!--变量名称-->  
  6.     <param-value>参数值</param-value>              <!--变量值-->  
  7.   </init-param>  
  8. </servlet>  
  9. <servlet-mapping>  
  10.   <servlet-name>ServletName</servlet-name>                 
  11.   <url-pattern>/aaa/xxx</url-pattern>                   <!--映射的url路径 -->  
  12. </servlet-mapping>  
    在地址栏中输入http://localhost:8080/web-App/aaa/xxx就可以访问了。

7.<listener>

    listener元素用来注册一个监听器类,可以在Web应用中包含该类。使用listener元素,可以收到事件什么时候发生以及用什么作为响应的通知。
    listener元素用来定义Listener接口,它的主要子元素为<listener-class>
        <listen-class>Listener的类名称</listener-class> 定义Listener的类名称.例如: com.foo.hello
    例如:
[html]  view plain  copy
  1. <listener>  
  2.     <listener-class><span style="font-family:'FangSong_GB2312';font-size:18px;white-space:pre;">com.foo.hello</span></listener-class>  
  3. </listener>  

8.<session-cofing>

    session-config包含一个子元素session-timeout.定义web应用中的session参数.
        <session-timeout>分钟</session-timeout> 定义这个web应用所有session的有效期限.单位为分钟.
    例如:
[html]  view plain  copy
  1. <session-config>  
  2.    <session-timeout>20</session-timeout>  
  3. </session-config>  

9.<mime-mapping>

    mime-mapping包含两个子元素extension和mime-type.定义某一个扩展名和某一MIME Type做对映.
        <extension>扩展名名称</extension> 扩展名称
        <mime-type>MIME格式</mime-type> MIME格式.
    例如:
[html]  view plain  copy
  1. <mime-mapping>  
  2.    <extension>doc</extension>  
  3.    <mime-type>application/vnd.ms-word</mime-type>  
  4. </mime-mapping>  
  5. <mime-mapping>  
  6.    <extension>xls</extension>  
  7.    <mime-type>application/vnd.ms-excel</mime-type>  
  8. </mime-mapping>  
  9. <mime-mapping>  
  10.    <extension>ppt</extesnion>  
  11.    <mime-type>application/vnd.ms-powerpoint</mime-type>  
  12. </mime-mapping>  

10.<welcome-file-list>

    welcome-file-list包含一个子元素welcome-file.用来定义首页列单.
        <welcome-file>用来指定首页文件名称</welcome-flie>
        welcome-file用来指定首页文件名称.我们可以用<welcome-file>指定几个首页,而服务器会依照设定的顺序来找首页.
    例如:
[html]  view plain  copy
  1. <welcome-file-list>  
  2.     <welcome-file>index.jsp</welcome-file>  
  3.     <welcome-file>index.htm</welcome-file>  
  4. </welcome-file-list>  

11.<error-page>

    error-page元素包含三个子元素error-code,exception-type和location.
    将错误代码(Error Code)或异常(Exception)的种类对应到web应用资源路径.
        <error-code>错误代码</error-code> HTTP Error code,例如: 404
        <exception-type>Exception</exception-type> 一个完整名称的Java异常类型
        <location>/路径</location>  在web应用内的相关资源路径
    例如:
[html]  view plain  copy
  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>/except.jsp</location>  
  8. </error-page>  

12.<jsp-config>

    jsp-config元素主要用来设定JSP的相关配置,<jsp:config>包括<taglib>和<jsp-property-group>两个子元素.其中<taglib>元素在JSP 1.2时就已经存在了;而<jsp-property-group>是JSP 2.0新增的元素.

    <taglib>
    taglib元素包含两个子元素taglib-uri和taglib-location.用来设定JSP网页用到的Tag Library路径.
        <taglib-uri>URI</taglib-uri> taglib-uri定义TLD文件的URI,JSP网页的taglib指令可以经由这个URI存取到TLD文件.
        <taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction> TLD文件对应Web应用的存放位置.

    <jsp-property-group>
    jsp-property-group元素包含8个元素,分别为:
        <description>Description</descrition>    此设定的说明
        <display-name>Name</display-name>        此设定的名称
        <url-pattern>URL</url-pattern>           设定值所影响的范围,如:/CH2 或者/*.jsp
        <el-ignored>true|false</el-ignored>      若为true,表示不支持EL语法.
        <scripting-invalid>true|false</scripting-invalid>    若为true表示不支持<%scription%>语法.
        <page-encoding>encoding</page-encoding>  设定JSP网页的编码
        <include-prelude>.jspf</include-prelude> 设置JSP网页的抬头,扩展名为.jspf
        <include-coda>.jspf</include-coda>       设置JSP网页的结尾,扩展名为.jspf

    例如:
[html]  view plain  copy
  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>  
  8.           Special property group for JSP Configuration JSP example.  
  9.         </description>  
  10.         <display-name>JSPConfiguration</display-name>  
  11.         <uri-pattern>/*</uri-pattern>  
  12.         <el-ignored>true</el-ignored>  
  13.         <page-encoding>GB2312</page-encoding>  
  14.         <scripting-inivalid>true</scripting-inivalid>  
  15.    ............  
  16.     </jsp-property-group>  
  17. </jsp-config>  

12.<resource-ref>

resource-ref元素包括五个子元素description,res-ref-name,res-type,res-auth,res-sharing-scope.利用JNDI取得应用可利用资源.
    <description>说明</description> 资源说明
    <rec-ref-name>资源名称</rec-ref-name> 资源名称
    <res-type>资源种类</res-type> 资源种类
    <res-auth>Application|Container</res-auth> 资源由Application或Container来许可
    <res-sharing-scope>Shareable|Unshareable</res-sharing-scope> 资源是否可以共享.默认值为 Shareable
例如:
[html]  view plain  copy
  1. <resource-ref>  
  2.    <description>JNDI JDBC DataSource of JSPBook</description>  
  3.    <res-ref-name>jdbc/sample_db</res-ref-name>  
  4.    <res-type>javax.sql.DataSoruce</res-type>  
  5.    <res-auth>Container</res-auth>  
  6. </resource-ref>  

一、web.xml配置文件常用元素及其意义预览

 1 <web-app>
 2 
 3      <!--定义了WEB应用的名字-->
 4      <display-name></display-name>
 5 
 6      <!--声明WEB应用的描述信息-->
 7      <description></description>
 8 
 9      <!--context-param元素声明应用范围内的初始化参数-->
10      <context-param></context-param>
11 
12      <!--过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联-->
13      <filter></filter>
14 
15      <!--一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联-->
16      <filter-mapping></filter-mapping>
17 
18      <!--servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。
19          Listener元素指出事件监听程序类-->
20      <listener></listener>
21 
22      <!--在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。
23          Servlet元素就是用来完成此项任务的-->
24      <servlet></servlet>
25 
26      <!--服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。
27          但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。
28          在更改缺省URL时,使用servlet-mapping元素-->
29      <servlet-mapping></servlet-mapping>
30 
31      <!--如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。可通过使用HttpSession的
32          setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值-->
33      <session-config></session-config>
34 
35      <!--如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证-->
36      <mime-mapping></mime-mapping>
37 
38      <!--指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件-->
39      <welcome-file-list></welcome-file-list>
40 
41      <!--在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面-->
42      <error-page></error-page>
43 
44      <!--对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,
45          而不用编辑使用这些文件的JSP页面-->
46      <taglib></taglib>
47 
48      <!--声明与资源相关的一个管理对象-->
49      <resource-env-ref></resource-env-ref>
50 
51      <!--声明一个资源工厂使用的外部资源-->
52      <resource-ref></resource-ref>
53 
54      <!--制定应该保护的URL。它与login-config元素联合使用-->
55      <security-constraint></security-constraint>
56 
57      <!--指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用-->
58      <login-config></login-config>
59 
60      <!--给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。
61          分别地声明角色可使高级IDE处理安全信息更为容易-->
62      <security-role></security-role>
63 
64      <!--声明Web应用的环境项-->
65      <env-entry></env-entry>
66 
67      <!--声明一个EJB的主目录的引用-->
68      <ejb-ref></ejb-ref>
69 
70      <!--声明一个EJB的本地主目录的应用-->
71      <ejb-local-ref></ejb-local-ref>
72 
73  </web-app> 

二、各个配置元素详解

1.Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标

1 <icon>  
2      <small-icon>/images/app_small.gif</small-icon>  
3      <large-icon>/images/app_large.gif</large-icon>  
4  </icon>

 2.Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称

<display-name>Tomcat Example</display-name>

 3.Web 应用描述:给出于此相关的说明性文本

<desciption>Tomcat Example servlets and JSP pages.</desciption>

 4.上下文参数:声明应用范围内的初始化参数

1 <context-param>
2      <param-name>参数名</para-name>
3      <param-value>参数值</param-value>
4      <description>参数描述</description>
5  </context-param>

在servlet里面可以通过 getServletContext().getInitParameter(“context/param”)得到

 5.过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联

 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>
 9  <filter-mapping>
10      <filter-name>setCharacterEncoding</filter-name>
11      <url-pattern>/*</url-pattern>
12  </filter-mapping>

 6.监听器配置

1 <listener>
2      <listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3  </listener>

 7.Servlet配置

 1 <servlet>
 2    <servlet-name>servlet名称</servlet-name>
 3    <servlet-class>servlet类全路径</servlet-class>
 4    <init-param>
 5        <param-name>参数名</param-name>
 6        <param-value>参数值</param-value>
 7    </init-param>
 8    <run-as>
 9        <description>Security role for anonymous access</description>
10        <role-name>tomcat</role-name>
11    </run-as>
12   <load-on-startup>指定当Web应用启动时,装载Servlet的次序</load-on-startup>
13 </servlet>
14 <servlet-mapping>
15   <servlet-name>servlet名称</servlet-name>
16   <url-pattern>映射路径</url-pattern>
17 </servlet-mapping>

 8.会话超时配置(单位为分钟)

1 <session-config>
2      <session-timeout>120</session-timeout>
3  </session-config>

 9.MIME类型配置

1 <mime-mapping>
2      <extension>htm</extension>
3      <mime-type>text/html</mime-type>
4  </mime-mapping>

 10.指定欢迎文件页配置

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

 11.配置错误页面

  (1).通过错误码来配置error-page

1 <!--配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp-->
2 <error-page>
3       <error-code>404</error-code>
4       <location>/NotFound.jsp</location>
5  </error-page>

  (2).通过异常的类型配置error-page

1 <!--配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp-->
2 <error-page>
3       <exception-type>java.lang.NullException</exception-type>
4       <location>/error.jsp</location>
5 </error-page>

 12.TLD配置

1 <taglib>
2      <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
3      <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
4  </taglib>

如果开发工具一直在报错,应该把<taglib> 放到 <jsp-config>中

1 <jsp-config>
2      <taglib>
3          <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
4          <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
5      </taglib>
6  </jsp-config>

 13.资源管理对象配置

1 <resource-env-ref>
2      <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
3  </resource-env-ref>

 14.资源工厂配置

1 <resource-ref>
2      <res-ref-name>mail/Session</res-ref-name>
3      <res-type>javax.mail.Session</res-type>
4      <res-auth>Container</res-auth>
5 </resource-ref>

  配置数据库连接池就可在此配置

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>

 15.安全限制配置

 1 <security-constraint>
 2      <display-name>Example Security Constraint</display-name>
 3      <web-resource-collection>
 4          <web-resource-name>Protected Area</web-resource-name>
 5          <url-pattern>/jsp/security/protected/*</url-pattern>
 6          <http-method>DELETE</http-method>
 7          <http-method>GET</http-method>
 8          <http-method>POST</http-method>
 9          <http-method>PUT</http-method>
10      </web-resource-collection>
11      <auth-constraint>
12          <role-name>tomcat</role-name>
13          <role-name>role1</role-name>
14      </auth-constraint>
15 </security-constraint>

 16.登陆验证配置

1  <login-config>
2      <auth-method>FORM</auth-method>
3      <realm-name>Example-Based Authentiation Area</realm-name>
4      <form-login-config>
5          <form-login-page>/jsp/security/protected/login.jsp</form-login-page>
6          <form-error-page>/jsp/security/protected/error.jsp</form-error-page>
7      </form-login-config>
8  </login-config>

 17.安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。

分别地声明角色可使高级IDE处理安全信息更为容易。

1 <security-role>
2      <role-name>tomcat</role-name>
3  </security-role>

 18.Web环境参数:env-entry元素声明Web应用的环境项

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>

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

 20.本地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>

web.xml文件详解

Web.xml常用元素

<web-app>    
<display-name></display-name>定义了WEB应用的名字    

<description></description> 声明WEB应用的描述信息    

<context-param></context-param> context-param元素声明应用范围内的初始化参数。    

<filter></filter> 过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。    
<filter-mapping></filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。 

<listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知.Listener元素指出事件监听程序类。    

<servlet></servlet> 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。    

<servlet-mapping></servlet-mapping> 服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。    

但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时,使用servlet-mapping元素。    

<session-config></session-config> 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。    

可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。    

<mime-mapping></mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。

<welcome-file-list></welcome-file-list> 指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。  

<error-page></error-page> 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。    

<taglib></taglib> 对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,而不用编辑使用这些文件的JSP页面。  

<resource-env-ref></resource-env-ref>声明与资源相关的一个管理对象。 

<resource-ref></resource-ref> 声明一个资源工厂使用的外部资源。

<security-constraint></security-constraint> 制定应该保护的URL。它与login-config元素联合使用   

<login-config></login-config> 指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用。  

<security-role></security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。  

<env-entry></env-entry>声明Web应用的环境项。 

<ejb-ref></ejb-ref>声明一个EJB的主目录的引用。 

< ejb-local-ref></ ejb-local-ref>声明一个EJB的本地主目录的应用。   

</web-app>    

相应元素配置

1、Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标

<icon>    
<small-icon>/images/app_small.gif</small-icon>    
<large-icon>/images/app_large.gif</large-icon>    
</icon>

2、Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称

<display-name>Tomcat Example</display-name>

3、Web 应用描述: 给出于此相关的说明性文本

<disciption>Tomcat Example servlets and JSP pages.</disciption>

4、上下文参数:声明应用范围内的初始化参数。

<context-param>    
    <param-name>ContextParameter</para-name>    
    <param-value>test</param-value>    
    <description>It is a test parameter.</description>    
</context-param>    

在servlet里面可以通过getServletContext().getInitParameter(“context/param”)得到

5、过滤器配置:将一个名字与一个实现javaxs.servlet.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-name>setCharacterEncoding</filter-name>    
        <url-pattern>/*</url-pattern>    
</filter-mapping>    

6、监听器配置

<listener>    
      <listerner-class>listener.SessionListener</listener-class>    
</listener>    

7、Servlet配置 
基本配置

   <servlet>    
      <servlet-name>snoop</servlet-name>    
      <servlet-class>SnoopServlet</servlet-class>    
   </servlet>    
   <servlet-mapping>    
      <servlet-name>snoop</servlet-name>    
      <url-pattern>/snoop</url-pattern>    
   </servlet-mapping>    

高级配置

   <servlet>    
      <servlet-name>snoop</servlet-name>    
      <servlet-class>SnoopServlet</servlet-class>    
      <init-param>    
         <param-name>foo</param-name>    
         <param-value>bar</param-value>    
      </init-param>    
      <run-as>    
         <description>Security role for anonymous access</description>    
         <role-name>tomcat</role-name>    
      </run-as>    
   </servlet>    
   <servlet-mapping>    
      <servlet-name>snoop</servlet-name>    
      <url-pattern>/snoop</url-pattern>    
   </servlet-mapping>    

元素说明

     <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类中通过getInitParamenter(String name)方法访问初始化参数    
     <load-on-startup></load-on-startup>指定当Web应用启动时,装载Servlet的次序。    
                                 当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.    
                                 当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它    
     <servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素    
       <servlet-name></servlet-name> 指定servlet的名称    
       <url-pattern></url-pattern> 指定servlet所对应的URL

8、会话超时配置(单位为分钟)

   <session-config>    
      <session-timeout>120</session-timeout>    
   </session-config>

9、MIME类型配置

   <mime-mapping>    
      <extension>htm</extension>    
      <mime-type>text/html</mime-type>    
   </mime-mapping>

10、指定欢迎文件页配置

   <welcome-file-list>    
      <welcome-file>index.jsp</welcome-file>    
      <welcome-file>index.html</welcome-file>    
      <welcome-file>index.htm</welcome-file>    
   </welcome-file-list>  

11、配置错误页面 
一、 通过错误码来配置error-page

   <error-page>    
      <error-code>404</error-code>    
      <location>/NotFound.jsp</location>    
   </error-page>  
  • 了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。 

二、通过异常的类型配置error-page

   <error-page>    
       <exception-type>java.lang.NullException</exception-type>    
       <location>/error.jsp</location>    
   </error-page> 

上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp 
12、TLD配置

   <taglib>    
       <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>    
       <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>    
   </taglib> 

如果MyEclipse一直在报错,应该把 放到 中

   <jsp-config>    
      <taglib>    
          <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>    
          <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>    
      </taglib>    
   </jsp-config>    

13、资源管理对象配置

   <resource-env-ref>    
       <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>    
   </resource-env-ref>  

14、资源工厂配置

   <resource-ref>    
       <res-ref-name>mail/Session</res-ref-name>    
       <res-type>javax.mail.Session</res-type>    
       <res-auth>Container</res-auth>    
   </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>    

15、安全限制配置

   <security-constraint>    
      <display-name>Example Security Constraint</display-name>    
      <web-resource-collection>    
         <web-resource-name>Protected Area</web-resource-name>    
         <url-pattern>/jsp/security/protected/*</url-pattern>    
         <http-method>DELETE</http-method>    
         <http-method>GET</http-method>    
         <http-method>POST</http-method>    
         <http-method>PUT</http-method>    
      </web-resource-collection>    
      <auth-constraint>    
        <role-name>tomcat</role-name>    
        <role-name>role1</role-name>    
      </auth-constraint>    
   </security-constraint> 

16、登陆验证配置

   <login-config>    
     <auth-method>FORM</auth-method>    
     <realm-name>Example-Based Authentiation Area</realm-name>    
     <form-login-config>    
        <form-login-page>/jsp/security/protected/login.jsp</form-login-page>    
        <form-error-page>/jsp/security/protected/error.jsp</form-error-page>    
     </form-login-config>    
   </login-config>   

17、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。 
分别地声明角色可使高级IDE处理安全信息更为容易。

<security-role>    
     <role-name>tomcat</role-name>    
</security-role> 

18、Web环境参数:env-entry元素声明Web应用的环境项

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

19、EJB 声明

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

20、本地EJB声明

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

21、配置DWR

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

22、配置Struts

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

23、配置Spring(基本上都是在Struts中配置的)

   <!-- 指定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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值