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 ...
随机推荐
- VijosP1303 导弹拦截
背景 实中编程者联盟为了培养技术精湛的后备人才,必须从基础题开始训练. 描述 某国为了防御敌国的导弹袭击,研发出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度, ...
- 教你怎么使用Windows7系统自带的备份与还原的方法
原文发布时间为:2010-09-09 -- 来源于本人的百度文章 [由搬家工具导入] 继续单击“下一步”按钮,在其后界面中检查上述备份设置是否正确,如果不正确的话可以直接单击“取消”按钮,重新设置备份 ...
- ashx接收参数 ashx传递参数
原文发布时间为:2009-09-30 -- 来源于本人的百度文章 [由搬家工具导入] Handler.ashx文件: <%@ WebHandler Language="C#" ...
- .net显示今天农历的代码!
原文发布时间为:2010-04-11 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Globalization;//namespace Proje ...
- js7:表单的学习,Forms对象
原文发布时间为:2008-11-09 -- 来源于本人的百度文章 [由搬家工具导入] dreamveawer中,选择插入——表单——然后后面的几个选项进行学习: 大体上这么些类型: <html& ...
- derby数据库的一些总结
本文主要是针对在osgi开发过程中的一些问题进行总结,其中dbcp数据源的配置是在SpringDM下配置的.一,derby数据源的内嵌模式 该模式的主要应用是嵌入式程序,因为其小巧,且不 ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---13
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- Selenium2+python自动化(学习笔记2)
from selenium import webdriverdriver = webdriver.Ie()driver.get=("http://www.baidu.com")dr ...
- java中Map的entrySet 和keySet的使用
存储这样的一个数据关系结构 使用嵌套map存储 可以通过调用 entrySet方法 或者 keySet方法 进行迭代或者增强for循环 便利输出 这里演示 迭代器的方式进行遍历 package ...
- hdu 1690(Floyed)
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...