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

}

ServletContext读取配置文件的更多相关文章

  1. 用ServletContext读取.properties文件

    在这里主要介绍ServletContext怎么从.properties文件中用键得到值的. ServletContext读取的.properties文件一般放在的位置有:1直接放在WebRoot下面. ...

  2. ServletContext 接口读取配置文件要注意的路径问题

    在建立一个maven项目时,我们通常把一些文件直接放在resource下面,在ServletContext中有getResource(String path)和getResourceAsStream( ...

  3. java 4种方式读取配置文件 + 修改配置文件

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...

  4. java读取配置文件(转)

    转载:http://blog.csdn.net/gaogaoshan/article/details/8605887 java 4种方式读取配置文件 + 修改配置文件     方式一:采用Servle ...

  5. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  6. JAVA使用相对路径读取配置文件

    JAVA使用相对路径读取配置文件[align=center][/align][size=medium][/size]   在软件开发中经常遇到读取配置文件,以及文件定位问题.今天做个总结.   (一) ...

  7. Servlet读取配置文件的三种方式

    一.利用ServletContext.getRealPath()[或getResourceAsStream()] 特点:读取应用中的任何文件.只能在web环境下. private void text3 ...

  8. Java基础加强-读取配置文件和内省

    Java读取配置文件 1.采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来. String path = "/WEB-INF/jdbc_conne ...

  9. Spring读取配置文件,地址问题,绝对路径,相对路径

    Spring在读取配置文件时,是相对于bin,或者WEB-INF的: “applicationContext.xml”就是找bin或WEB-INF及子文件夹下的文件: “/res/applicatio ...

随机推荐

  1. django Modelform 使用

    前言: 为什么要用form去验证呢? 我们提交的是form表单,在看前端源码时如果检查到POST URL及我们提交的字段,如果没有验证我们是否可以直接POST数据到URL,后台并没有进行校验,直接处理 ...

  2. Scrapy笔记:日志的使用

    scrapy的日志记录有两种方式: spider.logger.xx()和python标准库中的logger = logging.get_Logger('log information') 向日志对象 ...

  3. 在 Fedora 26/27 GNOME 3.24/3.26 环境中安装 FCITX 小企鹅输入法(修订)

    之前我曾经写过一篇文章介绍在 GNOME 3.x 下安装小企鹅输入法框架,但最近在 Fedora 26/27 环境下发现老方法已经失效了,会导致 GNOME 3.24/3.26 桌面在重启后无法进入, ...

  4. HDU5036 Explosion(期望&&bitset)

    #include <iostream> #include <cstring> #include <string> #include <vector> # ...

  5. Sprak RDD简单应用

    来自:http://my.oschina.net/scipio/blog/284957#OSC_h5_11 目录[-] 1.准备文件 2.加载文件 3.显示一行 4.函数运用 (1)map (2)co ...

  6. POJ 1239 Increasing Sequences [DP]

    题意:略. 思路:进行两次dp. 第一次dp从前向后,用dp[x]表示从第x位向前dp[x]位可构成一个数字,且与前面的数组符合题意要求.最后求的dp[n]即为最后一个数字的长度. 而题目还有要求,所 ...

  7. Akka Stream之Graph

    最近在项目中需要实现图的一些操作,因此,初步考虑使用Akka Stream的Graph实现.从而学习了下: 一.介绍 我们知道在Akka Stream中有三种简单的线性数据流操作:Source/Flo ...

  8. 基于http的断点续传和多线程下载

    HTTP协议的GET方法,支持只请求某个资源的某一部分: 206 Partial Content 部分内容响应: Range 请求的资源范围: Content-Range 响应的资源范围: 断点续传: ...

  9. 聊聊、Zookeeper 数据结构和操作命令

    Zookeeper 的视图结构跟标准的 Unix 文件系统很像,都有一个根节点 / .在根节点下面就是一个个的子节点,我们称为 ZNode.ZNode 是 Zookeeper 中最小数据单位,在 ZN ...

  10. python的安全插件

    1.PyFuzz (0.1.3)                                                         - Simple fuzz testing for u ...