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

    }

  • 相关阅读:
    Hive表中四种不同数据导出方式以及如何自定义导出列分隔符
    build doris 0.11.5 on centos 7/ubuntu
    centos7 gcc升级
    linux-nohup后台运行
    sqoop import mysql to hive table:GC overhead limit exceeded
    Hive开启mapjoin优化、并行执行、动态分区
    How to Plan and Configure YARN and MapReduce 2 in HDP 2.0
    Android 8 AudioPolicy 初始化
    Android 8 声音调整过程
    qcom wlan kernel 解读 WCNSS_qcom_cfg.ini 文件
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4345525.html
Copyright © 2011-2022 走看看