ServletContextListener实现全局配置装载入内存
2015-02-05 16:31
239 查看
(2011-03-27 19:34:06)
转载▼
1.定义ServletContextListener
2.修改WEB-INF/web.xml, 启用前面定义的listener
3.在JSP页面里可以调用listener装入的变量值
4.自定义XML配置文件
前面采用的是读取环境变量获取XML文件路径,我们应该把这个配置文件的路径设到环境变量中.这里不赘述.
你也可以直接采用文件物理路径,访问更简单些,但路径做死在程序里,不好修改.
转载▼
标签:
| 分类: java和数据库 |
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; } } } |
<!-- 系统初始化loader --> <listener> <listener-class>com.machome.system.ApplicationLoader</listener-class> </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> |
<?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> |
你也可以直接采用文件物理路径,访问更简单些,但路径做死在程序里,不好修改.
相关文章推荐
- 在tomcat中启动的文件-ServletContextListener实现全局配置装载入内存
- 在搭建Spring框架时,web.xml文件的配置之DispatcherServlet&ContextLoaderListener
- ServletContextListener HttpSessionListener 接口的实现与应用
- 在ServletContextListener实现类中获取spring注入对象
- 使用ServletContextListener实现优雅的系统初始化
- 在ServletContextListener实现类中获取spring注入对象
- 指定spring配置文件的位置 ContextLoaderListener DispatcherServlet
- SpringMVC源码 1 ContextLoaderListener和Servlet容器web.xml配置
- ServletContextListener HttpSessionListener 接口的实现与应用
- 在ServletContextListener实现类中获取spring注入对象
- spring初始化在ServletContextListener实现类中获取spring注入对象
- ServletContextListener实现读取web.xml中context-param信息
- 使用ServletContextListener实现优雅的系统初始化
- ServletContextListener在Tomcat中的配置问题
- 小析web.xml中context-param,Listener,Filter,Servlet处理顺序
- context-param -> listener -> filter -> servlet
- ServletContextListener 的使用例子
- ServletContextListener 扩展
- Spring 配置问题还是代码问题?Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- tomcat定时任务--ServletContextListener