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 ...
随机推荐
- [论文]Clustering-Based Ensembles as an Alternative to Stacking
Clustering-Based Ensembles as an Alternative to Stacking 作者:Anna Jurek, Yaxin Bi, Shengli Wu, and Ch ...
- Python 类的重写
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/7 22:46 # @Author : lijunjiang # @Fil ...
- Day 21 Object_oriented_programming_2
继承实现原理 python中的类可以同时继承多个父类,继承的顺序有两种:深度优先和广度优先. 一般来讲,经典类在多继承的情况下会按照深度优先的方式查找,新式类会按照广度优先的方式查找 示例解析: 没有 ...
- 记一次kubernetes集群异常: kubelet连接apiserver超时
Background kubernetes是master-slave结构,master node是集群的大脑, 当master node发生故障时整个集群都"out of control&q ...
- webpack学习(一)安装和命令行、一次js/css的打包体验及不同版本错误
一.前言 找了一个视频教程开始学习webpack,跟着视频学习,在自己的实际操作中发现,出现了很多问题.基本上都是因为版本的原因而导致,自己看的视频是基于webpack 1.x版,而自己现在早已是we ...
- Google开源基于Tensorflow的NLP框架重大升级
http://weibo.com/ttarticle/p/show?id=2309404086117195475716
- Flexible and Economical UTF-8 Decoder
http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
- 原生js获取元素的样式信息
工作中经常会需要获取DOM元素的样式,之前都是通过jquery的css()方法,现在总结一下通过原生js获取元素样式的方法. obj.style js var _width = obj.style.w ...
- schema设计
Schema设计 Schema:表的模式: 设计数据的表,索引,以及表和表的关系 在数据建模的基础上将关系模型转为数据库表 满足业务模型需要基础上根据数据库和应用特点优化表结构 关系模型图 ...
- Android图片突出
概述 今天有个群友问 Android 图片凸出 效果怎么弄,早以前有过类似的需求,整个项目的提示框都是一个背景,背景上方有凸出半张图片,所以用layer-list写了一个背景来实现. 思路 随便画了一 ...