当前位置: 技术问答>java相关
javabean 如何取得 web.xml 的設定 ?
来源: 互联网 发布时间:2015-05-30
本文导语: 我把數據庫的連結參數寫在 web.xml 中,如何用 javabean 取得,不是用 servlet 請眾大俠幫忙 . | 我的用法 配置文件:/WEB-INF/classes/config.conf serverURL=jdbc:mysql://localhost:3306/yonghang userName=root2 passWord=123456 conMin=...
我把數據庫的連結參數寫在 web.xml 中,如何用 javabean 取得,不是用 servlet
請眾大俠幫忙 .
請眾大俠幫忙 .
|
我的用法
配置文件:/WEB-INF/classes/config.conf
serverURL=jdbc:mysql://localhost:3306/yonghang
userName=root2
passWord=123456
conMin=5
conMax=30
conTimeOut=3
driver=org.gjt.mm.mysql.Driver
LogFile=c:\log.log
ErrLogFile=c:\logerr.log
sysLog=c:\syslog.log
读写的类1:PropManager.java (/WEB-INF/classes/PropManager.class)
import java.io.*;
import java.util.*;
public class PropManager {
private Properties Props;
private String path="/config.conf";
public PropManager(String strConfig)
{
if(strConfig==null || strConfig.equals(""))
init(path);
else
path=strConfig;
init(strConfig);
}
public PropManager()
{
init(path);
}
public String getProperty(String name)
{
try
{
String property=Props.getProperty(name);
if(property==null)
{
property="NULL";
}
return property;
}catch(Exception e)
{
System.err.println("CONFIG FILE ERROR! ");
return null;
}
}
private void init(String paths)
{
InputStream in=null;
try
{
//System.out.println(paths);
in=getClass().getResourceAsStream(paths);
//System.out.println(in);
}catch(Exception e)
{
System.out.println(paths);
System.err.println("Config.File ERROR! NO FOUND!");
}
try
{
Props=new Properties();
Props.load(in);
System.err.println("PropManager Load Successed!");
}catch(IOException e)
{
System.err.println("FileInputStream in ERROR!");
}
}
}
读写的类2:PropertyManager.java (/WEB-INF/classes/PropertyManager.class)
import PropManager;
public class PropertyManager
{
private static PropertyManager propertyManager=null;
private static PropManager propManager=null;
private static String path="/config.conf";
private static boolean staus=false;
public PropertyManager()
{
init();
}
public static String getProperty(String name)
{
checkStaus();
try
{
return propManager.getProperty(name);
}catch(Exception e)
{
System.out.println(" DBPool.PropertyManager.getProperty() ERROR!");
return null;
}
}
//内部方法
private static void checkStaus()
{
if(!staus || propertyManager==null || propManager==null)
{
propertyManager=new PropertyManager();
}
}
private void init()
{
try
{
propManager=new PropManager(path);
System.err.println("PropertyManager Init Successed!");
staus=true;
}catch(Exception e)
{
System.err.println("PropManager in ERROR!");
staus=false;
}
}
public static void setPath(String path_)
{
path=path_;
}
}
在JSP或SERVLET中使用
PropertyManager.getProperty("serverURL");
PropertyManager.getProperty("userName");
PropertyManager.getProperty("passWord");
来得到相关的参数
配置文件:/WEB-INF/classes/config.conf
serverURL=jdbc:mysql://localhost:3306/yonghang
userName=root2
passWord=123456
conMin=5
conMax=30
conTimeOut=3
driver=org.gjt.mm.mysql.Driver
LogFile=c:\log.log
ErrLogFile=c:\logerr.log
sysLog=c:\syslog.log
读写的类1:PropManager.java (/WEB-INF/classes/PropManager.class)
import java.io.*;
import java.util.*;
public class PropManager {
private Properties Props;
private String path="/config.conf";
public PropManager(String strConfig)
{
if(strConfig==null || strConfig.equals(""))
init(path);
else
path=strConfig;
init(strConfig);
}
public PropManager()
{
init(path);
}
public String getProperty(String name)
{
try
{
String property=Props.getProperty(name);
if(property==null)
{
property="NULL";
}
return property;
}catch(Exception e)
{
System.err.println("CONFIG FILE ERROR! ");
return null;
}
}
private void init(String paths)
{
InputStream in=null;
try
{
//System.out.println(paths);
in=getClass().getResourceAsStream(paths);
//System.out.println(in);
}catch(Exception e)
{
System.out.println(paths);
System.err.println("Config.File ERROR! NO FOUND!");
}
try
{
Props=new Properties();
Props.load(in);
System.err.println("PropManager Load Successed!");
}catch(IOException e)
{
System.err.println("FileInputStream in ERROR!");
}
}
}
读写的类2:PropertyManager.java (/WEB-INF/classes/PropertyManager.class)
import PropManager;
public class PropertyManager
{
private static PropertyManager propertyManager=null;
private static PropManager propManager=null;
private static String path="/config.conf";
private static boolean staus=false;
public PropertyManager()
{
init();
}
public static String getProperty(String name)
{
checkStaus();
try
{
return propManager.getProperty(name);
}catch(Exception e)
{
System.out.println(" DBPool.PropertyManager.getProperty() ERROR!");
return null;
}
}
//内部方法
private static void checkStaus()
{
if(!staus || propertyManager==null || propManager==null)
{
propertyManager=new PropertyManager();
}
}
private void init()
{
try
{
propManager=new PropManager(path);
System.err.println("PropertyManager Init Successed!");
staus=true;
}catch(Exception e)
{
System.err.println("PropManager in ERROR!");
staus=false;
}
}
public static void setPath(String path_)
{
path=path_;
}
}
在JSP或SERVLET中使用
PropertyManager.getProperty("serverURL");
PropertyManager.getProperty("userName");
PropertyManager.getProperty("passWord");
来得到相关的参数