【初学者】servlet读取配置文件

来源:互联网 发布:cnc车床编程视频教程 编辑:程序博客网 时间:2024/05/16 05:31

配置文件内容

名称:filename.properties

内容:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

Servlet文件doget()方法里的代码:

public void test1(){

InputStream in=this.getServletContext().getResourceAsStream(

"/WEB-INF/classes/filename.properties");

Properties pro=new Properties();

pro.load(in);

String url=pro.getProperty("url");

System.out.printIn(url);

}

public void test2(){

String path=this.getServletContent().getRealPath("/WEB-INF/classes/filename.properties");

FileInputStream fis=new FileInputStream(path);

Properties pro=new Properties();

pro.load(fis);

String driver=pro.getProperty("driver");

System.out.printIn(driver);


}

原创粉丝点击