`

Web.xml配置之context-param

 
阅读更多
转自: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中&lt;/context-param&gt;等配置的作用

    了解web.xml的配置信息和加载顺序,有助于理解框架的流程,

    web.xml详解(web-app_2_3.dtd)

    部署描述文件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.xml 配置详解。例如: &lt;web-app&gt; &lt;display-name&gt;&lt;/display-name&gt;定义了WEB应用的名字 &lt;description&gt;&lt;/description&gt; 声明WEB应用的描述信息 &lt;context-param&gt;&lt;/context-param&gt; context-param元素声明应用...

    JAVA web.xml配置详解

    -- context-param 元素用来设定web站台的环境参数(context),它包含两个子元素:param-name和param-value.,如spring的典型配置 --&gt; &lt;context-param&gt; &lt;!-- 设定Context名称 --&gt; &lt;param-name&gt;contextConfigLocation...

    解析web.xml中在Servlet中获取context-param和init-param内的参数

    本篇文章是对web.xml中在Servlet中获取context-param和init-param内的参数进行了详细的分析介绍,需要的朋友参考下

    logback-ext-spring

    &lt;param-value&gt;/WEB-INF/conf/logback.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener-class&gt;ch.qos.logback.ext.spring.web.LogbackConfigListener&lt;/listener-class&gt; &lt;/listener&gt; 即可加载logback配置,使用...

    spring和hibernate整合

    &lt;param-value&gt;/WEB-INF/spring/*.bean.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;listener&gt; &lt;listener-...

    ssh框架的搭建

    &lt;param-value&gt;/WEB-INF/log4j.properties&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Listener contextConfigLocation --&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener...

    shiro资料一览

    一,添加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配置文件的位置,并通过监听器加载

    web.xml文件标签详解

    &lt;!--该元素声明应用范围内的初始化参数--&gt; &lt;context-param&gt; &lt;!--声明描述信息--&gt;...&lt;param-name&gt;&lt;/param-name&gt; &lt;!--用于指定上下文参数的值--&gt; &lt;param-value&gt;&lt;/param-value&gt; &lt;/context-param&gt;

    基于java的企业级应用开发:Spring的核心容器.ppt

    Web服务器实例化ApplicationContext容器时,通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码: &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; ...

    Web配置详解

    Web.xml常用元素&lt;web-app&gt;&lt;display-name&gt;&lt;/display-name&gt;定义了WEB应用的名字&lt;description&gt;&lt;/description&gt; 声明WEB应用的描述信息&lt;context-param&gt;&lt;/context-param&gt; context-param元素声明应用范围内的初始化参数。...

    Spring MVC 框架应用实例

    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/database.xml /WEB-INF/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener-class&gt; org.spring...

    CXF WebService整合Spring示例工程代码demo

    &lt;param-value&gt;classpath*:applicationContext-server.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener-class&gt;org.springframework.web.util.IntrospectorCleanupListener&lt;/listener-class&gt; &lt;servlet-...

    DOS命令使用方法(超全).

    &lt;param-value&gt;classpath*:applicationContext-*.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- 这是spring 监听 --&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-...

    网站访问记录器 0.1.1 发布_程序

    数据库连接为在web.xml中的如下配置 项: &lt;context-param&gt; &lt;param-name&gt;WebVisitLogger-DbUrl&lt;/param-name&gt; &lt;param-value&gt;jdbc:postgresql://localhost/visit_logger?user=postgres&password=postgres&charSet...

    一个简单的Acegi入门实例

    &lt;!!-- web.xml文件 --&gt; &lt;?xml version="1.0" encoding="UTF-8"?...&lt;param-value&gt;/WEB-INF/acegi-config.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Acegi 的 Filter Chain 代理 --&gt; &lt;filter&gt; &lt;filter-name&gt;

    网站访问记录器 0.1.1 发布_数据库

    数据库连接为在web.xml中的如下配置 项: &lt;context-param&gt; &lt;param-name&gt;WebVisitLogger-DbUrl&lt;/param-name&gt; &lt;param-value&gt;jdbc:postgresql://localhost/visit_logger?user=postgres&password=postgres&charSet...

    springweb3.0MVC注解(附实例)

    &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Spring 容器启动监听器 --&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-...

    生活轨迹SSH服务端

    -- &lt;param-value&gt;/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml&lt;/param-value&gt; --&gt; &lt;param-value&gt;classpath:beans.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;filter-name&gt;struts2...

Global site tag (gtag.js) - Google Analytics