web.xml加载顺序

web.xml加载顺序

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

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

2、紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.

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

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

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

6、得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.如果想在项目启动之前就打开数据库,那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接,这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.


对于某类配置节而言,与它们出现的顺序是有关的
以 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 类似,此处不再赘述。

比如filter 需要用到 bean ,但加载顺序是: 先加载filter 后加载spring,则filter中初始化操作中的bean为null;所以,如果过滤器中要使用到 bean,可以将spring 的加载 改成 Listener的方式 
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 

web.xml节点解析

根节点

1、 
<web-app></web-app>

常用节点介绍
2、 <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>

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

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

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

基本节点:

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

7、 <display-name/> 定义站台的名称
 例子:<display-name>我的站点</display-name>

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

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

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

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

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值