转自:
http://blog.csdn.net/liaoxiaohua1981/article/details/6759206
格式定义:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>contextConfigLocationValue></param-value>
</context-param>
作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。
param-name 设定上下文的参数名称。必须是唯一名称
param-value 设定的参数名称的值
初始化过程:
- 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
- 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
- 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
- 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
- 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
- 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。
-
由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener >> fileter >> servlet
如何使用
- 页面中
- ${initParam.contextConfigLocation}
- Servlet中
- String paramValue=getServletContext().getInitParameter("contextConfigLocation")
-
分享到:
相关推荐
了解web.xml的配置信息和加载顺序,有助于理解框架的流程,
部署描述文件web.xml详解(web-app_2_3.dtd),全面介绍: 1. icon元素 2. display-name元素 3. description元素 4. distributable元素 5. context-param元素 6. filter元素 7. filter-mapping元素 8. ...
用于 web.xml 配置详解。例如: <web-app> <display-name></display-name>定义了WEB应用的名字 <description></description> 声明WEB应用的描述信息 <context-param></context-param> context-param元素声明应用...
-- context-param 元素用来设定web站台的环境参数(context),它包含两个子元素:param-name和param-value.,如spring的典型配置 --> <context-param> <!-- 设定Context名称 --> <param-name>contextConfigLocation...
本篇文章是对web.xml中在Servlet中获取context-param和init-param内的参数进行了详细的分析介绍,需要的朋友参考下
<param-value>/WEB-INF/conf/logback.xml</param-value> </context-param> <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> </listener> 即可加载logback配置,使用...
<param-value>/WEB-INF/spring/*.bean.xml</param-value> </context-param> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> <listener> <listener-...
<param-value>/WEB-INF/log4j.properties</param-value> </context-param> <!-- Listener contextConfigLocation --> <listener-class>org.springframework.web.context.ContextLoaderListener...
一,添加struts2的支持 1.添加jar包 struts-2.3.4.1\apps\struts2-blank\WEB-INF\lib 2.添加配置文件 struts-2.3.4.1\apps\struts2-...3.在web.xml中添加context-param指定spring配置文件的位置,并通过监听器加载
<!--该元素声明应用范围内的初始化参数--> <context-param> <!--声明描述信息-->...<param-name></param-name> <!--用于指定上下文参数的值--> <param-value></param-value> </context-param>
Web服务器实例化ApplicationContext容器时,通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码: <context-param> <param-name>contextConfigLocation</param-name> <param-value> ...
Web.xml常用元素<web-app><display-name></display-name>定义了WEB应用的名字<description></description> 声明WEB应用的描述信息<context-param></context-param> context-param元素声明应用范围内的初始化参数。...
<param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/database.xml /WEB-INF/applicationContext.xml </param-value> </context-param> <listener-class> org.spring...
<param-value>classpath*:applicationContext-server.xml</param-value> </context-param> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> <servlet-...
<param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <!-- 这是spring 监听 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-...
数据库连接为在web.xml中的如下配置 项: <context-param> <param-name>WebVisitLogger-DbUrl</param-name> <param-value>jdbc:postgresql://localhost/visit_logger?user=postgres&password=postgres&charSet...
<!!-- web.xml文件 --> <?xml version="1.0" encoding="UTF-8"?...<param-value>/WEB-INF/acegi-config.xml</param-value> </context-param> <!-- Acegi 的 Filter Chain 代理 --> <filter> <filter-name>
数据库连接为在web.xml中的如下配置 项: <context-param> <param-name>WebVisitLogger-DbUrl</param-name> <param-value>jdbc:postgresql://localhost/visit_logger?user=postgres&password=postgres&charSet...
<param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Spring 容器启动监听器 --> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-...
-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> --> <param-value>classpath:beans.xml</param-value> </context-param> <filter-name>struts2...