<context-param>的作用:

web.xml的配置中<context-param>配置作用
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件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项目完全启动之前被执行.
7.举例.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.
8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.
如:

 <!-- 加载spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-
INF/jason-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
又如: --->自定义context-param,且自定义listener来获取这些信息
 <context-param>
<param-name>urlrewrite</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>cluster</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>servletmapping</param-name>
<param-value>*.bbscs</param-value>
</context-param>
<context-param>
<param-name>poststoragemode</param-name>
<param-value>1</param-value>
</context-param>
<listener>
<listener-class>com.laoer.bbscs.web.servlet.SysListener</listener-class>
</listener> 
 public class SysListener extends HttpServlet implements ServletContextListener
{
private static final Log logger = LogFactory.getLog(SysListener.class); public void contextDestroyed(ServletContextEvent sce)
{
// 用于在容器关闭时,操作
}
// 用于在容器开启时,操作
public void contextInitialized(ServletContextEvent sce)
{
String rootpath = sce.getServletContext().getRealPath("/");
System.out.println("-------------rootPath:"+rootpath);
if (rootpath != null)
{
rootpath = rootpath.replaceAll("\\\\", "/");
} else
{
rootpath = "/";
}
if (!rootpath.endsWith("/"))
{
rootpath = rootpath + "/";
}
Constant.ROOTPATH = rootpath;
logger.info("Application Run Path:" + rootpath);
String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");
boolean burlrewrtie = false;
if (urlrewrtie != null)
{
burlrewrtie = Boolean.parseBoolean(urlrewrtie);
}
Constant.USE_URL_REWRITE = burlrewrtie;
logger.info("Use Urlrewrite:" + burlrewrtie);
其它略之....
}
}
/*
* 最终输出 -------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\ 2009-06-09
* 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO] Application Run
* Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/ 2009-06-09 21:51:46,526
* [com.laoer.bbscs.web.servlet.SysListener]-[INFO] Use Urlrewrite:true
* 2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO] Use
* Cluster:false 2009-06-09 21:51:46,526
* [com.laoer.bbscs.web.servlet.SysListener]-[INFO] SERVLET MAPPING:*.bbscs
* 2009-06-09 21:51:46,573 [com.laoer.bbscs.web.servlet.SysListener]-[INFO] Post
* Storage Mode:1
*/

context-param和init-param区别

web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:

 <context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
</context-param>
(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:

 <servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.wes.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
在servlet中可以通过代码分别取用:

 package com.wes.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class MainServlet extends HttpServlet ...
{
public MainServlet() ...
{
super();
}
public void init() throws ServletException ...
{
System.out.println("下面的两个参数param1是在servlet中存放的");
System.out.println(this.getInitParameter("param1"));
System.out.println("下面的参数是存放在servletcontext中的");
System.out.println(getServletContext().getInitParameter("context/param"));
}
}
第一种参数在servlet里面可以通过getServletContext().getInitParameter("context/param")得到
第二种参数只能在servlet的init()方法中通过this.getInitParameter("param1")取得.
 

<context-param>与<init-param>的更多相关文章

  1. button与input[type=”button”]的区别

    button与input[type="button"]的区别 特别感谢 @守护晴天 ,指出了博客中不细致不严谨的地方,也让我学到了更多,更多是觉得抱歉,由于自己的不细致可能误导了一 ...

  2. 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别

    这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...

  3. <button>和<input type="button"> 的区别

    <button>标签 定义和用法 <button> 标签定义一个按钮. 在 button 元素内部,您可以放置内容,比如文本或图像.这是该元素与使用 input 元素创建的按钮 ...

  4. <button>与<input type="button"> 的区别

    <button> button按钮点击会刷新整个页面 <input type="button">  不会刷新整个页面 本文为本人用来记录自己做的一些东西,如 ...

  5. <button>与<input type="button">的区别

    一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...

  6. 解析<button>和<input type="button"> 的区别

    一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...

  7. <button>与<input type="button">

    在做form表单,点击按钮随机生成两串密钥的时候 1.用第一种按钮的时候,会出现刷新form表单的现象.会把创建密钥前面的输入框中的字消失.虽然能生成密钥1和密钥2,但是会闪一下,随即消失.几个输入框 ...

  8. 解析button和input type=”button”的区别

    一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...

  9. 【转】解析<button>和<input type="button"> 的区别

    一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...

  10. button和input type=button的区别及注意事项

    <button>标签 定义和用法 <button>标签定义一个按钮. 在button元素内部,您可以放置内容,比如文本或图像.这是该元素与使用input元素创建的按钮之间的不同 ...

随机推荐

  1. R笔记1

    gsub format > measurements<-c('3.95*3.99*2.43mm','3*3*5mm','2*2*2mm') > measurements [1] &q ...

  2. Bus Hound 的使用方法

    背景: 最近在研究USB相关协议,需要对USB数据进行抓取分析,Bus Hound是个非常赞的工具,在此将其使用方法记录下来,以备下次快速上手使用. 正文: 主界面如下: 首先关注菜单栏三个选项: C ...

  3. CAN总线 SJA1000中断

    背景: 最近一直在使用C8051F340 + SJA1000来实现CAN通信,就SJA1000部分做个记录. 正文: 整个系统结构拓扑图如下: 两路CAN,C8051F340作为CPU,处理CAN与U ...

  4. Linux之服务器时间同步

    Linux时间同步(把nameNode1作为时间同步服务器) 设置时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 联网情况:ntpdate ...

  5. MySQL Cluster 配置文件(config.ini)详解

    MySQL Cluster 配置文件(config.ini)详解 ################################################################### ...

  6. Ubuntu 14 安装 Chrome/Chromium flash播放器

    在命令终端中执行如下命令即可: sudo apt-get install pepperflashplugin-nonfree sudo update-pepperflashplugin-nonfree ...

  7. 元素间距属性(scrollLeft,scrollWidth,clientWidth,offsetWidth,padding,margin)

    scrollHeight: 获取对象的滚动高度.scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端 ...

  8. Object.prototype.toString.call()进行类型判断

    为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...

  9. php实现图片缩放功能类

    http://www.poluoluo.com/jzxy/201312/255447.html <?php /** * Images类是一个图片处理类 * @package applicatio ...

  10. Unity内存申请和释放

    转自:http://www.jianshu.com/p/b37ee8cea04c 1.资源类型 GameObject, Transform, Mesh, Texture, Material, Shad ...