如何在servlet中读取web-inf目录下的properties配置文件

@WebServlet("/exer5")
public class Exer5 extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


//1、获取servletContext对象
ServletContext context = getServletContext();

//2、读取配置文件
InputStream is = context.getResourceAsStream("/WEB-INF/fil.properties");
//3、把属性放在Propeties中

Properties p = new Properties();
p.load(is);

System.out.println(p.getProperty("driver"));
System.out.println(p.getProperty("url"));
System.out.println(p.getProperty("user"));
System.out.println(p.getProperty("password"));

}


}

猜你喜欢

转载自blog.csdn.net/weixin_41637749/article/details/79458250