当前位置: 代码迷 >> Java Web开发 >> ServletContext获取properties文件内容解决方案
  详细解决方案

ServletContext获取properties文件内容解决方案

热度:7072   发布时间:2013-02-25 21:18:07.0
ServletContext获取properties文件内容
public class FileAccessServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
InputStream input=this.getClass().getResourceAsStream("/WEB-INF/classes/hao123.properties");
Properties proty = new Properties();
proty.load(input);
out.println("<br>"+"database=" + proty.getProperty("database"));
out.println("<br>"+"username=" + proty.getProperty("username"));
out.println("<br>"+"password=" + proty.getProperty("password"));
input.close();
}

}

hao123.properties 中的内容:dababase=1234 username=admin password=xw123

上面的程序需要导入的类被我省略了,输出的结果是:database=null username=null password=null 
为什么这儿获取的都是空值???还用当我把属性文件放在/WEB-INF/bin/hao123.properties,结果是
无法获取属性文件???


------解决方案--------------------------------------------------------
个人认为:InputStream input=this.getClass().getResourceAsStream("/WEB-INF/classes/hao123.properties");
改成
InputStream input=this.getClass().getResourceAsStream("hao123.properties");
  相关解决方案