Web.xml配置详解之context-param

简介:  格式定义:<context-param>  <param-name>contextConfigLocation</param-name>  <param-value>contextConfigLocationValue></param-value>  </context-param>  <c
  •  格式定义:


  1. <context-param>  

  2. <param-name>contextConfigLocation</param-name>  

  3. <param-value>contextConfigLocationValue></param-value>  

  4. </context-param>  

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>contextConfigLocationValue></param-value>
</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")

 

原文链接:

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

 

目录
相关文章
|
6月前
|
前端开发 JavaScript Java
Web.xml - Servlet与Filter的url-pattern
Web.xml - Servlet与Filter的url-pattern
67 8
|
29天前
Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]【解决方案】
Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]【解决方案】
22 0
|
8月前
|
Web App开发 安全 iOS开发
04dwr - web.xml配置(常用 init-param 参数列表)
04dwr - web.xml配置(常用 init-param 参数列表)
30 0
|
9月前
Servlet配置web.xml
Servlet配置web.xml
80 0
|
Java 前端开发 数据格式
|
XML 前端开发 JavaScript
SpringMVC之web.xml,了解必要配置项
SpringMVC之web.xml,了解必要配置项
213 0
SpringMVC之web.xml,了解必要配置项
|
容器
Web.xml配置详解之context-param
 格式定义:    contextConfigLocation   contextConfigLocationValue>     作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。
994 0
|
Web App开发 Java 数据格式
Spring项目的配置文件们(web.xml context servlet springmvc)
我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目都必须有的,我理解它是servlet的配置文件,也就是说,与spring无关。即使你开发的是一个纯粹jsp页面的web项目,你也必须配置这个文件。
4759 0
|
Java Spring 数据格式

热门文章

最新文章