使用ServletContext读取配置文件信息
第一步:新建配置文件
新建一个servlet
我们知道要读取properties文件 但是不知道他的路径如何写
我们可以先运行
查看发送到服务器后的路径
doGet方法
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/info.properties");
Properties prop = new Properties();
prop.load(is);
String username =prop.getProperty(“username”);
String sex = prop.getProperty(“sex”);
resp.getWriter().print(username+" : "+sex);
}
web.xml中注册servlet
运行程序