web.xml配置文件

来源:互联网 发布:美工数字字体图片 编辑:程序博客网 时间:2024/05/21 08:28
网上已经有很多同学对web.xml进行了详细的解读,我这边主要是将自己在网上浏览的文章进行汇总和梳理,便于理解和学习~


一般的web工程都会用到web.xml,web.xml主要用来配置,可以方便的开发web工程。Web.xml主要用来配置Filter、Listener、Servlet等。但是要说明的是web.xml并不是必要的,一个web工程可以没有web.xml文件。

web.xml的加载顺序
当要启动某个java项目时,容器如(tomcat)会第一步加载项目中的web.xml文件,通过其中的各种配置来启动项目,只有其中配置的各项均无误时,项目才能正确启动web.xml有多项标签,在其加载的过程中顺序依次为:context-param > listener > fileter > servlet(同类多个节点以出现顺序依次加载)并且这些元素可以配置在文件中的任意位置。
1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。 
2、紧接着,创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。 
3、容器将<context-param>转换为键值对,并交给servletContext。 
4、容器创建<listener>中的类实例,创建监听器。 
5、容器会读取,根据指定的路径来实例化过滤器
6、项目初始化完成
7、在发起第一次请求时,servlet节点才会被加载实例化
web.xml常用标签

1.<description>,<display-name>,<icon>
<description>项目描述</discription>  对项目做出描述.
<display-name>项目名称</display-name> 定义项目的名称.
<icon> icon元素包含small-icon和large-icon两个子元素.用来指定web站台中小图标和大图标的路径.
<small-icon>/路径/**.gif</small-icon>小图标大小为16 X 16 pixel,但是图象文件必须为GIF或JPEG格式,扩展名必须为:.gif或 .jpg.
<large-icon>/路径/**.jpg</large-icon> 大图标大小为32 X 32 pixel,但是图象文件必须为GIF或JPEG的格式,扩展名必须为; gif 或jpg.
<display-name>Test</display-name>    <description>This is a Examples</description>    <icon>       <small-icon>/images/small.gif</small-icon>         <large-icon>/images/large.gir</large-icon>    </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>     <param-name>param_name</param-name>     <param-value>param_value</param-value>  </context-param>  
3.<filter>
filter元素用于指定Web容器中的过滤器。在请求和响应对象被servlet处理之前或之后,可以使用过滤器对这两个对象进行操作。利用下一节介绍 的filter-mapping元素,过滤器被映射到一个servlet或一个URL模式。这个过滤器的filter元素和filter-mapping 元素必须具有相同的名称。
 filter元素用来声明filter的相关设定.filter元素除了下面介绍的的子元素之外,还包括之前介绍过的<icon>,<display-name>,<description>,<init-param>,其用途一样.
init-param元素与context-param 元素具有相同的元素描述符。
filter-name元素用来定义过滤器的名称,该名称在整个应用中都必须是惟一的。
filter-class元素指定过滤 器类的完全限定的名称。
  <filter>    <filter-name>encodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>      <param-name>encoding</param-name>      <param-value>UTF-8</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>encodingFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>
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
<servlet-name>Servlet的名称<servlet-name> 定义servlet的名称.
<dispatcher>REQUEST|INCLUDE|FORWARD|ERROR</disaptcher> 设定Filter对应的请求方式,有RQUEST,INCLUDE,FORWAR,ERROR四种,默认为REQUEST.
  <filter>    <filter-name>encodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>      <param-name>encoding</param-name>      <param-value>UTF-8</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>encodingFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>
5.<servlet>
   在web.xml中完成的一个最常见的任务是对servlet或JSP页面给出名称和定制的URL。用servlet元素分配名称,使用servlet-mapping元素将定制的URL与刚分配的名称相关联。
 <servlet>    <servlet-name>scan</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring/spring-mvc.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>scan</servlet-name>    <url-pattern>/*</url-pattern>  </servlet-mapping>
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.
<servlet-mapping>     <servlet-name>LoginChecker</servlet-name>     <url-pattern>/LoginChecker</url-pattern>  </servlet-mapping>  
 7.<listener>
listener元素用来注册一个监听器类,可以在Web应用中包含该类。使用listener元素,可以收到事件什么时候发生以及用什么作为响应的通知。
listener元素用来定义Listener接口,它的主要子元素为<listener-class>
<listen-class>Listener的类名称</listener-class> 定义Listener的类名称.例
<listener>      <listener-class><span style="font-family: FangSong_GB2312;font-size:18px; white-space: pre;">com.foo.hello</span></listener-class>  </listener>  
8.<session-cofing>
session-config包含一个子元素session-timeout.定义web应用中的session参数.
<session-timeout>分钟</session-timeout> 定义这个web应用所有session的有效期限.单位为分钟.
<session-config>     <session-timeout>20</session-timeout>  </session-config>  
9.<mime-mapping>
mime-mapping包含两个子元素extension和mime-type.定义某一个扩展名和某一MIME Type做对映.
<extension>扩展名名称</extension> 扩展名称
<mime-type>MIME格式</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>  <mime-mapping>     <extension>ppt</extesnion>     <mime-type>application/vnd.ms-powerpoint</mime-type>  </mime-mapping> 
10.<welcome-file-list>
welcome-file-list包含一个子元素welcome-file.用来定义首页列单.
<welcome-file>用来指定首页文件名称</welcome-flie>
welcome-file用来指定首页文件名称.我们可以用<welcome-file>指定几个首页,而服务器会依照设定的顺序来找首页.
<welcome-file-list>      <welcome-file>index.jsp</welcome-file>      <welcome-file>index.htm</welcome-file>  </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应用内的相关资源路径
<error-page>     <error-code>404</error-code>     <location>/error404.jsp</location>  </error-page>  <error-page>     <exception-type>java.lang.Exception</exception-type>     <location>/except.jsp</location>  </error-page>  

0 0