zoukankan      html  css  js  c++  java
  • ServletContext读取配置文件

    package servlet;

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     * Servlet implementation class ServletDemo5
     */
    public class ServletDemo5 extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            read7();
        }
        private void read7() throws IOException {
            String str = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
            System.out.println(str);
            FileInputStream is = new FileInputStream(str);
            Properties prop = new Properties();
            prop.load(is);
            String url = prop.getProperty("url");
            String name = prop.getProperty("username");
            String pwd = prop.getProperty("password");
            System.out.println(url);
            System.out.println(name);
            System.out.println(pwd);
        }
        private void read6() throws IOException {
            FileInputStream is = new FileInputStream("classes/db.properties");
            Properties prop = new Properties();
            prop.load(is);
            String url = prop.getProperty("url");
            String name = prop.getProperty("username");
            String pwd = prop.getProperty("password");
            System.out.println(url);
            System.out.println(name);
            System.out.println(pwd);
        }
        private void read5() throws IOException {
            String str = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
            System.out.println(str);
        }
        private void read4() throws IOException {
            InputStream is = this.getServletContext().getResourceAsStream("WEB-INF/classes/servlet/db.properties");
            Properties prop = new Properties();
            prop.load(is);
            String url = prop.getProperty("url");
            String name = prop.getProperty("username");
            String pwd = prop.getProperty("password");
            System.out.println(url);
            System.out.println(name);
            System.out.println(pwd);
        }
        private void read3() throws IOException {
            InputStream is = this.getServletContext().getResourceAsStream("WEB-INF/db.properties");
            Properties prop = new Properties();
            prop.load(is);
            String url = prop.getProperty("url");
            String name = prop.getProperty("username");
            String pwd = prop.getProperty("password");
            System.out.println(url);
            System.out.println(name);
            System.out.println(pwd);
        }

        private void read2() throws IOException {
            InputStream is = this.getServletContext().getResourceAsStream("/db.properties");
            Properties prop = new Properties();
            prop.load(is);
            String url = prop.getProperty("url");
            String name = prop.getProperty("username");
            String pwd = prop.getProperty("password");
            System.out.println(url);
            System.out.println(name);
            System.out.println(pwd);
        }

        private void read1() throws IOException {
            InputStream is = this.getServletContext().getResourceAsStream("WEB-INF/classes/db.properties");
            Properties prop = new Properties();
            prop.load(is);
            String url = prop.getProperty("url");
            String name = prop.getProperty("username");
            String pwd = prop.getProperty("password");
            System.out.println(url);
            System.out.println(name);
            System.out.println(pwd);
        }

        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }

    }

  • 相关阅读:
    洛谷P1724 东风谷早苗
    hdu 1001 Sum Problem
    洛谷 P1006 传纸条
    codevs 6116 区间素数
    LibreOJ #101. 最大流
    洛谷 P1455 搭配购买
    LibreOJ #119. 最短路 (堆优化dijkstra)
    LibreOJ #109. 并查集
    COGS.1200 ganggang的烦恼
    uoj #15. 【NOIP2014】生活大爆炸版石头剪刀布
  • 原文地址:https://www.cnblogs.com/siashan/p/3911774.html
Copyright © 2011-2022 走看看