读取properties文件

/ java / 80浏览
  @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //把资源变成流 , /是相对这个项目的根目录,不要漏了
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");

        Properties properties = new Properties(); //new 一个properties
        properties.load(is); //加载一个流

        String username = properties.getProperty("username"); //获得属性
        String password = properties.getProperty("password");
        resp.getWriter().print(username+":"+password);


    }
username=root
password=root
站长统计代码