关于Servlet读取web应用中不同位置properties文件。
Java代码
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// 读取utils1.properties
InputStream in = this.getServletContext().getResourceAsStream(
"utils1.properties");
Properties prop = new Properties();
prop.load(in);
System.out.println("=======utils1.properties content========");
System.out.println(prop.getProperty("username"));
System.out.println(prop.getProperty("password"));
// 读取utils2.properties
InputStream in2 = this.getServletContext().getResourceAsStream(
"/WEB-INF/classes/utils2.properties");//发布后utils2.properties在web应用中的位置
prop = new Properties();
prop.load(in2);
System.out.println("=======utils2.properties content========");
System.out.println(prop.getProperty("username"));
System.out.println(prop.getProperty("password"));
}