SystemConfigrationServlet

来源:互联网 发布:淘宝执行运营工资高吗 编辑:程序博客网 时间:2024/04/20 08:35
package com.friendcom.dongzuo;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.Enumeration;import java.util.Properties;import javax.servlet.ServletConfig;import javax.servlet.http.HttpServlet;import org.apache.log4j.Logger;@SuppressWarnings("serial")public class SystemConfigrationServlet extends HttpServlet{        private static Logger logger=Logger.getLogger(SystemConfigrationServlet.class);        private static String configProperties="application.properties";        public void init(){        logger.info("初始化系统配置...");        //容器初始化一个servlet时,会为这个servlet建一个唯一的ServletConfig。容器从DD读出Servlet初始化参数,并把这些参数交给ServletConfig,        //然后把ServletConfig传递给servlet的init(ServletConfig config)方法。也就是说容器只有在创建servlet实例时才会读DD文件中的init-param,并且在servlet一生中只读一次。        //在实际应用过程中,为了便于修改我们并不希望直接把某一变量硬编码到servlet类中,这个时候就会用到ServletConfig接口。我们可以把某些变量放在DD(web.xml)中,        //这样如果我们要修改某一值,可以直接改动DD文件即可。在DD文件(web.xml)中?此处疑问在于web。xml里面的init-param也是读取的application。properites里面的文件        //这样做????        ServletConfig config=getServletConfig();        if(config.getInitParameter("configProperties")!=null){            configProperties=config.getInitParameter("configProperties");        }                Properties prop=new Properties();        InputStream ips = null;        //classLoader作用:加载class文件到JVM以供程序使用        //采用双亲委托模式进行类加载,原因在于        //1,避免重复加载        //2,安全,防止注入        ClassLoader loader = SystemConfigrationServlet.class.getClassLoader();        //默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。         ips = loader.getResourceAsStream(configProperties);        try {            //从输入流configProperties中读取属性列表(键值对的组合)            prop.load(ips);        } catch (IOException e) {            e.printStackTrace();        }        //Enumeration<?> enumeration = prop.propertyNames();        //根据key值,取application.properties里面的value值        String accessPath=prop.get("image.accessPath").toString();        //将value值存入到servlet上下文中,页面上可以通过el表达式取值        config.getServletContext().setAttribute("accessPath", accessPath);        String hallPath=prop.get("hallPath").toString();        config.getServletContext().setAttribute("hallPath", hallPath);                String siteUrl=prop.get("site.url").toString();        config.getServletContext().setAttribute("siteUrl", siteUrl);                String newsCMS=prop.get("cms.news").toString();        config.getServletContext().setAttribute("newsCMS", newsCMS);                String adCMS=prop.get("cms.ad").toString();        config.getServletContext().setAttribute("adCMS", adCMS);                /*while (enumeration.hasMoreElements()) {            String key = enumeration.nextElement().toString();            String value = "";            try {                value = new String(prop.getProperty(key).getBytes("ISO-8859-1"),"UTF-8").toString();            } catch (UnsupportedEncodingException e) {                logger.error("初始化加载银行参数,转换字符集错误......");                e.printStackTrace();            }                         * 删除 key、value 中所有空格                         System.out.println(key+" : "+value);            //CommonPayInfo.BankPayInfo.put(StringUtils.removeSpace(key), StringUtils.removeSpace(value));            //logger.info("key=" + StringUtils.removeSpace(key) + ",value=" + StringUtils.removeSpace(value));        }*/        logger.info("初始化参数结束......");    }                }
0 0