web.xml 加载(解析)顺序

web.xml加载顺序html

应用服务器启动时web.xml加载过程,至于这些节点在xml文件中的先后顺序没有关系,不过有些应用服务器,我曾碰到过的 websphere就严格要求web.xml的节点顺序,不然部署不成功,因此仍是同意按照web.xml标准格式写
content-param --> listener --> filter --> servlet
java

一、启动WEB项目的时候,应用服务器会去读它的配置文件web.xml.读两个节点:<listener></listener> 和 <context-param></context-param>   

二、紧接着,容器建立一个ServletContext(上下文),这个WEB项目全部部分都将共享这个上下文.

三、容器将<context-param></context-param>转化为键值对,并交给ServletContext.

四、容器建立<listener></listener>中的类实例,即建立监听.

五、在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中得到: 
 ServletContext = ServletContextEvent.getServletContext();
 context-param的值 = ServletContext.getInitParameter("context-param的键");    

六、获得这个context-param的值以后,你就能够作一些操做了.注意,这个时候你的WEB项目尚未彻底启动完成.这个动做会比全部的Servlet都要早.换句话说,这个时候,你对<context-param>中的键值作的操做,将在你的WEB项目彻底启动以前被执行.若是想在项目启动以前就打开数据库,那么这里就能够在<context-param>中设置数据库的链接方式,在监听类中初始化数据库的链接,这个监听是本身写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.好比说数据库链接的关闭.
web


对于某类配置节而言,与它们出现的顺序是有关的
以 filter 为例,web.xml 中固然能够定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里必定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出如今 filter 以后,不然当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。
web 容器启动时初始化每一个 filter 时,是按照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。 
servlet 同 filter 相似,此处再也不赘述。
spring

好比filter 须要用到 bean ,但加载顺序是: 先加载filter 后加载spring,则filter中初始化操做中的bean为null;因此,若是过滤器中要使用到 bean,能够将spring 的加载 改为 Listener的方式 
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
sql

 

web.xml节点解析

根节点
一、 
<web-app></web-app>
数据库

经常使用节点介绍
二、 <context-param />  用来设定web站台的环境参数
 它包含两个子元素:
 <param-name></param-name> 用来指定参数的名称
 <param-value></param-value> 用来设定参数值
 在此设定的参数,能够在servlet中用 getServletContext().getInitParameter("my_param") 来取得
 例子:
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath*:/log4j.properties</param-value>
 </context-param>

三、 <listener /> 用来设定Listener接口
 它的主要子元素为
 <listener-class></listener-class> 定义Listener的类名称
 例子:
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

四、 <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>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
  </init-param>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>

五、 <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>DemoServlet</servlet-name>
  <servlet-class>com.test.DemoServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>DemoServlet</servlet-name>
  <url-pattern>/demoServlet</url-pattern>
 </servlet-mapping>

服务器

基本节点:

六、 <description/> 是对站台的描述
 例子:<description>传道、授业、解惑</description> 

七、 <display-name/> 定义站台的名称
 例子:<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. 
例子: 
<icon> 
 <small-icon>/images/small.gif</small-icon> 
 <large-icon>/images/large.gir</large-icon> 
</icon>

九、 <distributable/> 是指定该站台是否可分布式处理

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

十一、 <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>
app

十二、 <error-page>
 <error-page>
     <error-code>500</error-code>
     <location>/message.jsp</location>
 </error-page>
 <error-page>
     <error-code>400</error-code>
     <location>/message.jsp</location>
 </error-page>
 <error-page>
     <error-code>403</error-code>
     <location>/message.jsp</location>
 </error-page>
 <error-page>
     <error-code>404</error-code>
     <location>/message.jsp</location>
 </error-page>
 <error-page>
     <error-code>502</error-code>
     <location>/index.jsp</location>
 </error-page>

1三、
 <jsp-config/>
 <jsp-config>
 <taglib>
 <taglib-uri>/struts-tags</taglib-uri>
 <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/struts-dojo-tags</taglib-uri>
 <taglib-location>/WEB-INF/struts-dojo-tags.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/s</taglib-uri>
 <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
 </taglib>
 </jsp-config>

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

15、 <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
jsp

 好比,配置数据库链接池就可在此配置 <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>

相关文章
相关标签/搜索