`
javaeyetodj
  • 浏览: 426199 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Web.xml配置详解之context-param

 
阅读更多

转自 http://blog.csdn.net/liaoxiaohua1981/article/details/6759206

 

  •  格式定义:
[html] view plaincopy
 
  1. <context-param>  
  2. <param-name>contextConfigLocation</param-name>  
  3. <param-value>contextConfigLocationValue></param-value>  
  4. </context-param>  

作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。

param-name 设定上下文的参数名称。必须是唯一名称

param-value 设定的参数名称的值

  • 初始化过程:
    1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
    2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
    3. 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
    4. 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
    5. 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
    6. 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener  >> fileter  >> servlet

  •  如何使用
  1. 页面中

    ${initParam.contextConfigLocation}

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

    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内的参数进行了详细的分析介绍,需要的朋友参考下

    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元素声明应用...

    spring配置中<context-param> 和<init-param>的 区别

    &lt;context-param&gt; 和&lt;init-param&gt;的 区别代码 博文链接:https://xhy0422.iteye.com/blog/46319

    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;

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

    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元素声明应用范围内的初始化参数。...

    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配置,使用...

    ssh框架的搭建

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;!-- Listener log4jConfigLocation --&gt; &lt;listener-class&gt;org.springframework.web.util.Log4...

    jaxrs-api,javax.ws.rs.Path;

    import javax.ws.rs.core.Context; &lt;!-- &lt;context-param&gt; &lt;param-name&gt;resteasy.scan&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/context-param&gt; --&gt; &lt;context-param&gt; &lt;param-name&gt;resteasy.servlet....

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

    <context-param>与<init-param>的区别与作用

    NULL 博文链接:https://wy649898543.iteye.com/blog/1833263

    DWR+SSH应用的相关配置代码说明

    导入dwr.jar包到lib下,在webroot/WEB-INF下创建dwr.xml文件以及导入dwr20.dtd文件, ------------1)DWR.xml &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE dwr PUBLIC ...-------2)Web.xml中配置

    FCK在线编辑器源码及部署项目引用示例

    4.将以下代码添加到web.xml配置文件中: &lt;servlet-name&gt;Connector&lt;/servlet-name&gt; &lt;servlet-class&gt; com.FCKeditor.connector.ConnectorServlet &lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;baseDir&lt;/param-...

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

    spring/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; ApplicationContext * * * *...

Global site tag (gtag.js) - Google Analytics