Servelet获取properties的几种方法

来源:互联网 发布:网络黄金通缉人名单 编辑:程序博客网 时间:2024/06/08 11:04

第一种

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    String absolutePath = getServletContext().getRealPath("/WEB-INF/cfg.properties");    Properties props = new Properties();    props.load(new FileInputStream(absolutePath));    System.out.println(props.getProperty("p"));    }

第二种

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      ResourceBundle rb = ResourceBundle.getBundle("cfg");    String value = rb.getString("p");    System.out.println(value);    }

第三种

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      ClassLoader cl = this.getClass().getClassLoader();    InputStream is = cl.getResourceAsStream("cfg.properties");    Properties props = new Properties();    props.load(is);    System.out.println(props.getProperty("p"));    }
0 0
原创粉丝点击