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. 为了兼容性问题,本人一律淘汰不兼容如下三种浏览器的js

    原文发布时间为:2009-04-16 -- 来源于本人的百度文章 [由搬家工具导入] JavaScript: 不兼容 IE    FF火狐   Google 一律不作收藏了。。。。 最好还能兼容 Op ...

  2. TCP/IP协议详解笔记——ARP协议和RARP协议

    ARP:地址解析协议 对于以太网,数据链路层上是根据48bit的以太网地址来确定目的接口,设备驱动程序从不检查IP数据报中的目的IP地址.ARP协议为IP地址到对应的硬件地址之间提供动态映射. 工作过 ...

  3. PE笔记之NT头PE文件头

    typedef struct _IMAGE_FILE_HEADER {       WORD    Machine;                         //014C-IMAGE_FILE ...

  4. python:virtualenv的使用

    安装virtualenv 使用pip安装:pip install virtualenv virtualenvwrapper 其中virtualenvwrapper是virtualenv的扩展工具,用于 ...

  5. hdu 1852(快速幂模+有除法的时候取模的公式)

    Beijing 2008 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Tota ...

  6. hdu 1595(最短路变形好题)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  7. ORACLE的字符串操作函数

    字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...

  8. 机器学习(4):数据分析的工具-pandas的使用

    前面几节说一些沉闷的概念,你若看了估计已经心生厌倦,我也是.所以,找到了一个理由来说一个有兴趣的话题,就是数据分析.是什么理由呢?就是,机器学习的处理过程中,数据分析是经常出现的操作.就算机器对大量样 ...

  9. TCPMon使用总结

    一.TCPMon介绍 TCPMon是apache下的一个项目,下载链接:http://ws.apache.org/commons/tcpmon/download.cgi TCPMon相当于一个中转站, ...

  10. CodeForces - 316E3 Summer Homework

    Discription By the age of three Smart Beaver mastered all arithmetic operations and got this summer ...