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 ...
随机推荐
- 洛谷 P1783 海滩防御
题目描述 WLP同学最近迷上了一款网络联机对战游戏(终于知道为毛JOHNKRAM每天刷洛谷效率那么低了),但是他却为了这个游戏很苦恼,因为他在海边的造船厂和仓库总是被敌方派人偷袭.于是,WLP动用了他 ...
- 【UVA10561】Treblecross(SG函数)
题意:有n个格子排成一行,其中一些格子里面有字符X.两个游戏者轮流操作,每次可以选一个空格,在里面放上字符X. 如果此时有3个连续的X出现,则该游戏者赢得比赛.初始条件下不会有3个X连续出现. 判断先 ...
- jQuery1.4与json格式兼容问题
原文发布时间为:2010-10-10 -- 来源于本人的百度文章 [由搬家工具导入] 原来使用jQuery1.3.2编写的代码,更换到1.4.2后,使用jQuery.ajax()加载的json文件,不 ...
- .NET返回上一页
原文发布时间为:2010-05-25 -- 来源于本人的百度文章 [由搬家工具导入] if (Request.UrlReferrer != null) { ...
- Repeater用ul li,一行显示多条数据
原文发布时间为:2009-08-26 -- 来源于本人的百度文章 [由搬家工具导入] .rep { width:680px; float:left; l ...
- php中Mail_mimeDecode无法读取foxmail等eml文件正文问题
使用$cat -A xx.eml文件,发现foxmail的eml文件文件结尾和空行使用的\r\r\n, 如: Received: from WDGTO0MYSBX754J (unknown [106. ...
- 用chardet判断字符编码的方法
转自http://www.cnblogs.com/xiaowuyi/archive/2012/03/09/2387173.html 用chardet判断字符编码的方法 1.chardet下载与安装 ...
- 【强连通分量】 Kosaraju和Tarjan算法 (标准模板+详细注释)
codevs 题意:求最大强连通分量的大小以及所包含的顶点有哪些 Tarjan算法 #include<iostream> #include<queue> #include< ...
- ELK之filebeat替代logstash收集日志
filebeat->redis->logstash->elasticsearch 官网下载地址:https://www.elastic.co/downloads/beats/file ...
- luogu P1854 花店橱窗布置
题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V是花瓶的数目.花束可以移动,并且每束花用1到F的整数标识 ...