zoukankan      html  css  js  c++  java
  • JavaWeb学习记录(八)——servlet获取配置信息

    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);
        }

    }

  • 相关阅读:
    Ubuntu 16.04 OneDrive自动同步
    在conda环境中pip使用清华源秒速安装skimage、opencv、tensorflow、pytorch1.2.0等p
    写论文的最佳实践
    训练误差、测试误差、泛化误差的区别
    输入法 ctrl+句号 切换 中英文符号
    理解Graham扫描算法 查找凸包
    PDF阅读器 SumatraPDF 设置:电子书字体字号的更换及行距设置
    友情链接
    CRC全套~~~ 转载
    mysql插入中文出错,提示1366
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4345525.html
Copyright © 2011-2022 走看看