servlet获取并存储web.xml中context-param参数

在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);
        }
    }

}
时间: 2024-08-27 05:26:10

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

servlet无法自动在web.xml中配置

在新建dynamic web project 时,dynamic web module version选择2.5. 原文地址:https://www.cnblogs.com/wxd136/p/9455819.html

spring mvc在web.xml中的配置

spring mvc将所有的请求都经过一个servlet控制器-DispatcherServlet,这个servlet的工作就是将一个客户端的request请求分发给不同的springmvc控制器,既然是一个控制器Servlet就需要在web.xml中配置. <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.Dispa

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

一.spring中如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,spring会默认加载WEB-INF/applicationContext.xml文件(若没有,要新建一个). 例如: <context-param> <param-name>contextConfigLocation</param-name> <param-value&

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

引自:http://blog.csdn.net/yakson/article/details/9203231 web.xml里面可以定义两种参数:1.application范围内的参数,存放在servletContext中,在web.xml中配置如下: <context-param> <param-name>context/param</param-name> <param-value>avalible during application</para

web.xml 中的listener、filter、servlet加载及一些配置

在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> filter -> servlet 同时还存在着这样一种配置节:context-param,它用于向 Servlet

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

首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.  但不会因为 filter 写在 listener 的前面而会先加载 filter.  最终得出的结论是:listener -> filter -> servlet 同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息.我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是

web.xml中Filter,Listener,Servlet的区别

一.Servlet Servlet是基本的服务端程序,他来自接口Servlet,接口中有方法service.而Servlet的一个重要实现类,则是tomcat服务器的核心,那就是HttpServlet HttpServlet有方法: public abstract class HttpServlet extends GenericServlet { private static final String METHOD_DELETE = "DELETE"; private static

获取web.xml中的context-param和init-param定义的值

web.xml里面可以定义两种参数:<context-param> 和</init-param> (1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下: 1 <context-param> 2 <param-name>context/param</param-name> 3 <param-value>avalible during application</param-val

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

在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> filter -> servlet 同时还存在着这样一种配置节:context-param,它用于向 Servlet