不必继承类 ServletContextListener ,使用简单类完全可以
使用静态块可以加载,这个是自动的,但是记得
如下代码: Properties静态完全没用,必须用普通类型例如String 这个才能真正的完整保存住值得,切记切记
package com.jysoft.memcump.common;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.sun.org.apache.xml.internal.security.Init;
public class InitDbMessageForJava {
public static String driver = "";
public static Properties props =null;
static
{
init();
}
public static void init()
{
if(null ==props)
{
props = new Properties();
}
try {
props.load(InitDbMessageForJava.class.getResourceAsStream("db.properties"));
System.out.println(props.getProperty("driver"));
driver =props.getProperty("driver");
Demo.driver = props.getProperty("driver");;
System.out.println(props.getProperty("url"));
System.out.println(props.getProperty("username"));
System.out.println(props.getProperty("password"));
System.setProperty("driver", props.getProperty("driver"));
System.setProperty("url", props.getProperty("url"));
System.setProperty("username", props.getProperty("username"));
System.setProperty("password", props.getProperty("password"));
} catch (IOException e) {
e.printStackTrace();
}
}
}