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. 洛谷 P1262 间谍网络==Codevs 4093 EZ的间谍网络

    4093 EZ的间谍网络 时间限制: 10 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B ...

  2. RowFilter 对于已获取到的dataset进行过滤

    原文发布时间为:2009-11-12 -- 来源于本人的百度文章 [由搬家工具导入] DataView的属性RowFilter使用方法 p.s. 重点在于DataView是DateTable相关联的一 ...

  3. pyspider 爬虫教程(一):HTML 和 CSS 选择器

      虽然以前写过 如何抓取WEB页面 和 如何从 WEB 页面中提取信息.但是感觉还是需要一篇 step by step 的教程,不然没有一个总体的认识.不过,没想到这个教程居然会变成一篇译文,在这个 ...

  4. Eclipse配置关联Tomcat并运行项目

    打开Eclipse,单击“Window”菜单,选择最下方的“Preferences”. 单击“Server”选项,选择下方的“Runtime Environments”. 点击“Add”添加Tomca ...

  5. Codeforces Round #441 Div. 2 A B C D

    题目链接 A. Trip for Meal 题意 三个点之间两两有路径,分别长为\(a,b,c\),现在从第一个点出发,走\(n-1\)条边,问总路径最小值. 思路 记起始点相邻的边为\(a,b\), ...

  6. javascript 之 className属性

    Javascript 可以通过className 属性灵活的更改一个标签元素的CSS 类选择器来实现样式的变化. 1.用className 属性修改节点的css类别 代码如下: <html> ...

  7. Day 29 process&thread_1

    进程和线程 1 进程(process): 1.定义: 最小的执行单元.进程就是一个程序在一个数据集上的一次动态执行过程. 进程一般由程序.数据集.进程控制块三部分组成: 我们编写的程序用来描述进程要完 ...

  8. FormatDateTime 当前时间减去几小时的做法

    top_start_modified := FormatDateTime('yyyy-mm-dd hh:mm:ss',(Now - ((1/24)*3)));    top_end_modified ...

  9. web前端生成图片之探索踩坑

    前段时间,产品和运营整了个非常变态的需求,要求将一个活动页面输出为图片,然后用户进行分享 开始以为是用户自己手动截图分享,没想到后来不是,细思极恐,感叹需求之变态. 从网上找了N个方案,最后确定使用  ...

  10. weblogic内存快速配置

    # IF USER_MEM_ARGS the environment variable is set, use it to override ALL MEM_ARGS values USER_MEM_ ...