首页 > 编程语言 > 详细

JavaWeb学习记录(八)——servlet获取配置信息

时间:2015-06-30 17:59:20      阅读:238      评论:0      收藏:0      [点我收藏+]

jdbc.properties内容如下:

jdbcUrl=jdbc\:mysql\://localhost\:3306/animal
user=root
pass=root

servlet获取资源信息代码如下
public class ResourceServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test1();
        test2();
    }
//方法一
    private void test1() throws IOException, FileNotFoundException {
        System.out.println("--------------test1--------------");
        ServletContext application=getServletContext();
        String path=application.getRealPath("/WEB-INF/classes/jdbc.properties");
        File file=new File(path);
        
        Properties pro=new Properties();
        pro.load(new FileReader(file));    
        System.out.println(pro.getProperty("jdbcUrl"));
        System.out.println(pro.getProperty("user"));
        System.out.println(pro.getProperty("pass"));
    }

//方法二
    private void test2() throws IOException, FileNotFoundException {
        System.out.println("--------------test2--------------");
        ServletContext application=getServletContext();
        URL url=application.getResource("/WEB-INF/classes/jdbc.properties");
        
        Properties pro=new Properties();
        pro.load(url.openStream());
        System.out.println(pro.getProperty("jdbcUrl"));
        System.out.println(pro.getProperty("user"));
        System.out.println(pro.getProperty("pass"));
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

JavaWeb学习记录(八)——servlet获取配置信息

原文:http://www.cnblogs.com/ly-radiata/p/4345525.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!