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

    }

  • 相关阅读:
    分布式系统笔记
    Paxos算法细节详解(一)
    Java G1学习笔记
    Spring Boot 的 10 个核心模块
    k8s 重点
    毕玄:阿里十年,从分布式到云时代的架构演进之路
    netty原理解析
    JVM调优总结(一):基本概念
    《快学 Go 语言》第 16 课 —— 包管理 GOPATH 和 Vendor
    Oracle 检查表空间使用情况
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4345525.html
Copyright © 2011-2022 走看看