当前位置: 代码迷 >> 综合 >> 使用servletContext读取资源文件
  详细解决方案

使用servletContext读取资源文件

热度:52   发布时间:2023-10-19 16:27:31.0
//读取webroot目录下的资源
InputStream in = this.getServletContext().getResourceAsStream("/db.properties");

System.out.println(in);



//获取web资源的绝对路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
FileInputStream in = new FileInputStream(path);

Properties prop = new Properties();
prop.load(in);

String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");


//读取web工程中资源文件

InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(in);

String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");