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 ...
随机推荐
- android 微信开发交流群
有效期很短,可加个人微信 如果已过有效期,加我个人微信,我拉你进群
- C#、.Net学习资料免注册下载基地。。。
原文发布时间为:2008-11-28 -- 来源于本人的百度文章 [由搬家工具导入] http://club.topsage.com/forumdisplay.php?fid=121&filt ...
- iOS--子视图如何穿透上层视图响应事件
一.使用方法:- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event; 二.背景知识iOS系统检 ...
- 复习FBV与CBV
在学习DRF之前~我们要先复习一些知识点~~ FBV和CBV 学习Django的时候~我们已经学习过了CBV以及FBV~~我们来复习一下~~ 什么是FBV和CBV呢~~ FBV 基于函数的视图 CBV ...
- vue element-ui Tabs 标签页实现【更多】功能
element-ui Tabs本身是没有更多功能的,如果在外边添加一个更多按钮,又非常不好看, 而利用API中Tabs Attributes的before-leave勾子方法可以实现这个功能, 简单P ...
- 安全性测试入门 (四):Session Hijacking 用户会话劫持的攻击和防御
本篇继续对于安全性测试话题,结合DVWA进行研习. Session Hijacking用户会话劫持 1. Session和Cookies 这篇严格来说是用户会话劫持诸多情况中的一种,通过会话标识规则来 ...
- [USACO15JAN]踩踏Stampede
[USACO15JAN]踩踏Stampede 题目描述 DJ站在原点上向y轴正半轴看,然后有一群奶牛从他眼前飞过.这些奶牛初始都在第二象限,尾巴在(Xi,Yi),头在(Xi+1,Yi),每Ci秒向右走 ...
- CSS定位与布局:普通流
CSS定位与布局属于CSS的基础,也是CSS布局影响很大的一部分,具体主要包括三种定位与布局机制( Positioning schemes):普通流,浮动,绝对定位. 其实除了这三种之外,还有一些定位 ...
- fauxbar.bak
{"options": { "almostdone":"0", "backup_searchEngines":" ...
- VirtualBox 扩展虚拟硬盘容量
转载:VirtualBox 扩展虚拟硬盘容量 如果使用的是ubuntu主机加xp虚拟机,扩容后,xp还无法识别扩大后的硬盘部分,可以在xp下使用“分区助手”进行处理,即将扩大的空间分给C盘.