ServletContextListener实现全局配置装载入内存

  (2011-03-27 19:34:06)
标签: 

侦听器

 

全局变量

 

xml

 

杂谈

分类: java和数据库
  • 1.定义ServletContextListener
public class ApplicationLoader implements ServletContextListener{
   
    private String ip;
    private String port;
    private String username;
    private String password;
    private String server;
    public void contextInitialized(ServletContextEvent arg0) {
       
        //装载配置文件
        this.loadXmlConfig();
       
        // 将配置文件里的值装入context属性,这样可以在JSP,SERVLET里调用
        ServletContext context = arg0.getServletContext();
        context.setAttribute("ip", this.ip);
        context.setAttribute("port", this.port);
        context.setAttribute("username", this.username);
        context.setAttribute("password", this.password);
        context.setAttribute("server", this.server);
       
    }
     public void loadXmlConfig() {
        //获取配置文件路径, 定义在系统环境变量中
        String xmlFile = System.getenv(Constants.ENVNAME);
       
        //Dom4J方式获取XML文件的节点值
        List<String> retList = null;
        retList = Dom4JParser.readNodeText(new File(
                xmlFile+"/ibiconfig/xml/amconfig.xml"),"//host/ip");
        if(null != retList){
            for(String temp:retList){
                this.ip = temp;
            }
        }

        //Dom4J方式获取XML文件的节点值
        retList = Dom4JParser.readNodeText(new File(
                xmlFile+"/ibiconfig/xml/amconfig.xml"),"//host/port");
        if(null != retList){
            for(String temp:retList){
                this.port = temp;
            }
        }


        //Dom4J方式获取XML文件的节点值
        retList = Dom4JParser.readNodeText(new File(
                xmlFile+"/ibiconfig/xml/amconfig.xml"),"//host/authentication/username");
        if(null != retList){
            for(String temp:retList){
                this.username = temp;
            }
        }


        //Dom4J方式获取XML文件的节点值
        retList = Dom4JParser.readNodeText(new File(
                xmlFile+"/ibiconfig/xml/amconfig.xml"),"//host//authentication/password");
        if(null != retList){
            for(String temp:retList){
                this.password = temp;
            }
        }


        //Dom4J方式获取XML文件的节点值
        retList = Dom4JParser.readNodeText(new File(
                xmlFile+"/ibiconfig/xml/amconfig.xml"),"//host//authentication/server");
        if(null != retList){
            for(String temp:retList){
                this.server = temp;
            }
               
    }


  • 2.修改WEB-INF/web.xml, 启用前面定义的listener
    <!-- 系统初始化loader -->
    <listener>
        <listener-class>com.machome.system.ApplicationLoader</listener-class>
    </listener>


  • 3.在JSP页面里可以调用listener装入的变量值
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<br />
<%=getServletContext().getAttribute("ip")%> <br />
<%=getServletContext().getAttribute("username")%> <br />
<%=getServletContext().getAttribute("password")%> <br />
</body>
</html>


  • 4.自定义XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- reload tomcat after changed this file-->
<root>
    <host>
         <ip>192.168.0.101</ip>
         <port>8080</port>
         <authentication>
             <username>macg</username>
             <password>008421</password>
             <server>EDASERVE</server>
         </authentication>      
    </host>
</root>

前面采用的是读取环境变量获取XML文件路径,我们应该把这个配置文件的路径设到环境变量中.这里不赘述.
你也可以直接采用文件物理路径,访问更简单些,但路径做死在程序里,不好修改.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值