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);
}
}
ServletContext读取配置文件的更多相关文章
- 用ServletContext读取.properties文件
在这里主要介绍ServletContext怎么从.properties文件中用键得到值的. ServletContext读取的.properties文件一般放在的位置有:1直接放在WebRoot下面. ...
- ServletContext 接口读取配置文件要注意的路径问题
在建立一个maven项目时,我们通常把一些文件直接放在resource下面,在ServletContext中有getResource(String path)和getResourceAsStream( ...
- java 4种方式读取配置文件 + 修改配置文件
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...
- java读取配置文件(转)
转载:http://blog.csdn.net/gaogaoshan/article/details/8605887 java 4种方式读取配置文件 + 修改配置文件 方式一:采用Servle ...
- JavaWeb中servlet读取配置文件的方式
我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...
- JAVA使用相对路径读取配置文件
JAVA使用相对路径读取配置文件[align=center][/align][size=medium][/size] 在软件开发中经常遇到读取配置文件,以及文件定位问题.今天做个总结. (一) ...
- Servlet读取配置文件的三种方式
一.利用ServletContext.getRealPath()[或getResourceAsStream()] 特点:读取应用中的任何文件.只能在web环境下. private void text3 ...
- Java基础加强-读取配置文件和内省
Java读取配置文件 1.采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来. String path = "/WEB-INF/jdbc_conne ...
- Spring读取配置文件,地址问题,绝对路径,相对路径
Spring在读取配置文件时,是相对于bin,或者WEB-INF的: “applicationContext.xml”就是找bin或WEB-INF及子文件夹下的文件: “/res/applicatio ...
随机推荐
- Something about the microsoft HttpContext domain design
1. HttpContext.Current.Request, Response 2.HttpHandler-> ProcessRequest 3.HttpModule-> Init, I ...
- C# 读取计算机CPU,HDD信息
public string getCpuInfo() //读取CPU信息 { ManagementClass mobj = new ManagementClass("Win32_Proces ...
- cmd窗口情况下:windows下cmd默认的编码是GBK
想在windows下查看sqlite的utf-8中文需要先 执行chcp 65001把当前页换为utf-8编码 chcp 命令: chcp 65001 就是换成UTF-8代码页,在命令行标题栏上点击 ...
- android的布局-----GridLayout(网格布局)
学习导图 (一)简介 网格布局由GridLayout所代表,在android4.0之后新增加的布局管理器,因此需要android4.0之后的版本中使用,如果在更早的平台使用该布局管理器,则需要导入相应 ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---14
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 九、 Java程序初始化的顺序(二)
之前的一篇博客里我写了关于在一个类中的程序初始化顺序,但是在Java的面向对象里,类之间还存在着继承的关系.所以关于程序的初始化顺序,我们可以再细划分为:父类静态变量,父类的静态代码块,父类构造器,父 ...
- js计算数组中每个元素出现的次数
计算数组中每个元素出现的次数 var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']; var countedNames = names.redu ...
- POI 2014 HOTELS (树形DP)
题目链接 HOTELS 依次枚举每个点,以该点为中心扩展. 每次枚举的时候,从该点的儿子依次出发,搜完一个儿子所有的点之后进行答案统计. 这里用了一个小trick. #include <bits ...
- HashMap之equals和hashCode小陷阱
先以一段代码开始这篇blog. 01 public class Name { 02 03 private String first; //first name 04 private Str ...
- luogu P1214 [USACO1.4]等差数列 Arithmetic Progressions
题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...