在web.xml中定义了context-param,一般不会随意改动,所以在监听器中做一次处理,容器启动时读取并存储在Properties中,方便以后取值。

SysProperties 类用于存储 context 键值;

SystemListener 监听器类,处理 context-param 参数。

/**
* 用于存储参数键值
*/
public final class SysProperties {
private static SysProperties instance;
private Properties initProperties = new Properties(); private SysProperties() { } public static SysProperties getInstance() {
if (instance == null) {
instance = new SysProperties();
}
return instance;
} /**
* 获取对应的值
* @param key String 提供的键(param-name)
* @return String 键对应的值(param-value)
*/
public String getProperty(String key)
{
return this.initProperties.getProperty(key);
} /**
* 检测是否包含该键
* @param key String 键
* @return boolean 该键存在返回 true,否则返回 false
*/
public boolean containsKey(String key) {
return this.initProperties.containsKey(key);
} /**
* 存储参数
* @param key String param-name
* @param object String param-value
*/
public void put(String key, String object) {
this.initProperties.put(key, object);
}
}
/**
* 系统监听,程序启动时初始化并存储相关参数
*/
public class SystemListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
initContextParam(servletContextEvent);
} @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { } private void initContextParam(ServletContextEvent event) {
Enumeration<String> names = event.getServletContext().getInitParameterNames();
while (names.hasMoreElements())
{
String name = names.nextElement();
String value = event.getServletContext().getInitParameter(name);
SysProperties.getInstance().put(name, value);
}
} }

servlet获取并存储web.xml中context-param参数的更多相关文章

  1. 读取web.xml中设置的参数

    以获取Filer元素里设置的参数为例 先在web.xml文件中配置如下 <?xml version="1.0" encoding="UTF-8"?> ...

  2. (转)web.xml中的contextConfigLocation在spring中的作用

    (转)web.xml中的contextConfigLocation在spring中的作用   一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...

  3. web.xml中contextConfigLocation的作用(转)

    原文地址:http://blog.csdn.net/zhangliao613/article/details/6289114 原文格式较乱,此处略作整理.内容未变. 在web.xml中使用contex ...

  4. 【转】web.xml中的contextConfigLocation在spring中的作用

    一.spring中如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,sp ...

  5. web.xml中load-on-startup

    <servlet> <servlet-name>SystemInit</servlet-name> <servlet-class>sjgl.system ...

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

    引自:http://blog.csdn.net/yakson/article/details/9203231 web.xml里面可以定义两种参数:1.application范围内的参数,存放在serv ...

  7. web.xml中:<context-param>与<init-param>的区别与作用及获取方法

    <context-param>的作用: web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件w ...

  8. [转]web.xml中servlet ,filter ,listener ,interceptor的作用与区别

    原文链接:https://blog.csdn.net/netdevgirl/article/details/51483273 一.概念: 1.servlet:servlet是一种运行服务器端的java ...

  9. web.xml 中的listener、 filter、servlet 加载顺序及其详解

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

随机推荐

  1. Python图片识别——人工智能篇

     一.安装pytesseract和PIL PIL全称:Python Imaging Library,python图像处理库,这个库支持多种文件格式,并提供了强大的图像处理和图形处理能力. 由于PIL仅 ...

  2. C#设计模式(7)——适配器模式

    一.概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 二.模型 三.代码实现 using System; /// 这里以 ...

  3. 差一点搞混了Transactional注解

    今天给我的Srping业务层加如下Service和Transactional注解: @Service @Scope(BeanDefinition.SCOPE_SINGLETON) @Transacti ...

  4. centos6 搭建IPSEC

    http://www.maxwhale.com/how-to-install-l2tp-vpn-on-centos/ http://blog.earth-works.com/2013/02/22/ho ...

  5. Java探索之旅(14)——文本I/O与读写

    1文件类File    ❶封装文件或路径的属性.不包括创建和读写文件操作.File实例并不会实际创建文件.不论文件存在与否,可以创建任意文件名的实例.两种实例创建方式如下:               ...

  6. 具体问题:3、hibernate跟Mybatis/ ibatis 的区别,为什么选择?

    第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...

  7. JVM优化(未完)

    -XX:+PrintGC 在eclipse控制台打印简单的GC信息,与-verbose:gc一样 -verbose:gc 在eclipse控制台打印简单的GC信息 -XX:+PrintGCDetail ...

  8. 6、RNA-Seq Analysis Pipeline

    Created by Dhivya Arasappan, last modified by Dennis C Wylie on Nov 08, 2015 This pipeline uses an a ...

  9. MIC-3395单板机不识别PCI设备

    硬件环境: 单板机:MIC-3395 外设设备:自研的DSP6678板卡 现象: MIC-3395单板机不能识别DSP6678板卡,但是在MIC-3392上能够正常识别,排查若干问题后,升级3395的 ...

  10. ES Docs-1:Installation Elasticsearch-2.3.1

    installation Elasticsearch requires at least Java 7. Specifically as of this writing, it is recommen ...