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

ass>

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

时间: 2024-08-03 11:31:09

web.xml加载顺序的相关文章

web.xml加载顺序详解

web.xml加载顺序 1.先加载<context-param>标签 2.创建servletContext容器 3.把<context-parame>标签中数据转化成键值树交给servletContext容器 4.创建Listener实例 5.加载filter(过滤器) 6.加载Interceptor(拦截器) 7.加载servlet 注:filter加载顺序:根据web.xml中<filter-mapper>来决定 servlet一样如此 1.自定义Listener,

web.xml加载顺序与web.xml常用节点解析

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

JAVA Web.xml 加载顺序

web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param> 2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文. 3.容器将<context-param></context-param>转化为键值对,

【未整理】web.xml加载顺序.RP

一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文. 3.容器将<context-param>转换为键值对,并交给servletContext. 4.容器创建<listener>中的类实例,创建监听器. 二  Load-on-startup Lo

WEB容器启动——web.xml加载详解

最近在看spring的源码,关于web.xml文件在容器(Tomcat.JBOSS等)启动时加载顺序问题很混乱,通过搜集资料,得出以下的结论: 1.加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:context-param -> listener -> filter -> servlet; 容器首先会去它的配置文件web.xml读取两个节点:  <context-param&

web.xml加载

Web项目的很多信息都配置在web.xml文件中,而且项目启动时,容器(下文以Tomcat为例)会先读配置文件,那web.xml文件中的那么多内容是按照什么顺序加载的呢? 这个读取顺序跟文件内容的上下文顺序基本没有关系,也就是说这些配置信息的读取顺序是一定的,跟他写在哪个位置没关系. (1)因为无论是Listener,Filter,Servlet都可能在初始化的时候用到ServletContext中的内容,而这个内容来自<context-param></context-param>

SpringMVC(十七):Web.xml加载流程源码分析

之前章节讲解了web.xml如何使用编码的方式替换掉,但是一直没有写web.xml是如何被加载的相关细节,觉得十分有必要写一篇文章类梳理下. 待完成... 参考 <SpringMVC初始化流程> <Spring 4.x源码分析-BeanWrapper> <第三章 DispatcherServlet详解 ——跟开涛学SpringMVC> <SpringMvc之DispatcherServlet详解> <Spring MVC入口Servlet详解(Http

C# web 页面加载顺序--转载

ASP.NET页面生命周期描述 下面是ASP.NET页面初始的过程: 1. Page_Init(); 2. Load ViewState; 3. Load Postback data; 4. Page_Load(); 5. Handle control events; 6. Page_PreRender(); 7. Page_Render(); 8. Unload event; 9. Dispose method called;  下面对其中的一些过程作下描述: 1. Page_Init();

spring 的配置文件 是上个web.xml 加载spring容器中的/WEB-INF/application.xml 文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/sc