读取properties文件------servletcontext及dao层读取
用servletcontext读取properties文件-------1)
重点在于:InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/kms.properties");//获取properties内容。 这个地址是编译后的地址src文件夹就会消失,变成在WEB-INF文件夹中的classes里!

properties位置的几种可能:
/WEB-INF/classes/kms.properties-------- 》
/WEB-INF/classes/cn/itcaste/kms.properties------>
/kms.properties------------------------------------------>
用servletcontext读取properties文件-------2)
传统的io流属于相对路径,是相对于java虚拟机,目前也就是tomcat里的bin文件夹,所以在用之前要取绝对路径(这个方法的好处在于可以取到资源名字)

在dao层读取properties文件
如果读取配置文件不是servlet的话,就只能通过类装载器去读取了(不能传递serletcontext,因为降低层和层的耦合度)
demo:ServletDemo12
package cn.itcaste.web.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import cn.itcaste.web.dao.UserDao; /**
* Servlet implementation class ServletDemo12
*/
@WebServlet("/ServletDemo12")
public class ServletDemo12 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao userdao=new UserDao();
userdao.updata();
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
UserDao.java
/**
*
*/
package cn.itcaste.web.dao; import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; /**
* @author: snowing
* @date : 2017年3月28日
*
*/
//如果读取配置文件不是servlet的话,就只能通过类装载器去读取了(不能传递serletcontext,因为降低层和层的耦合度)
public class UserDao {
public void updata() throws IOException{ InputStream in=UserDao.class.getClassLoader().getResourceAsStream("kms.properties");
Properties props=new Properties();
props.load(in);
System.out.println(props.getProperty("url"));
} }
kms.properties

结果输出:访问:http://localhost:8082/day04/ServletDemo12
控制台:jdbc:myaql://locahost:3306/test
因为dao层多个方法都需要读取properties,所以可以用静态代码块(注意配置文件不要太大!)

读取properties文件细节:如果配置文件properties修改了,要得到实时的数据就不能用类装载器来读取了,
因为类只加载一次!!!!
所以用普通的io流来读取,当然一定要得到绝对路径 代码如下:

读取properties文件------servletcontext及dao层读取的更多相关文章
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- java基础—java读取properties文件
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- Java基础学习总结(15)——java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- Java的Properties类和读取.properties文件
一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...
- Java读取Properties文件的六种方法
使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
随机推荐
- Atitit.mysql oracle with as模式临时表模式 CTE 语句的使用,减少子查询的结构性 mssql sql server..
Atitit.mysql oracle with as模式临时表模式 CTE 语句的使用,减少子查询的结构性 mssql sql server.. 1. with ... as (...) 在mys ...
- solr 简单搭建 数据库数据同步(待续)
原来在别的公司负责过文档检索模块的维护(意思就是不是俺开发的啦). 所以就略微接触和研究了下文档检索. 文档检索事实上是全文检索.是通过一种技术把N多文档进行一定规律的分割归类,然后创建易于搜索的索引 ...
- 56. Two Sum【easy】
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 监控 Linux 性能的 18 个命令行工具(转)
http://www.oschina.net/translate/command-line-tools-to-monitor-linux-performance?cmp&p=1# 1.Top- ...
- makefile之strip函数
#$(strip <string> ) #名称:去空格函数--strip. #功能:去掉<string>字串中开头和结尾的空字符,并将中间的多个连续空字符(如果有的话)合并为一 ...
- EAV/ESS 8.x 自定义服务器正确方法+更新服务器列表
下面用64位的ESET Smart Security 8.0.319.1进行设置自定义更新服务器说明(注:修改方法32位和64位通用) 1. 让我们先看一下ESET Smart Security 8 ...
- jquery插件-table转Json数据插件
使 用开源插件Table-to-json: 官方地址:http://lightswitch05.github.io/table-to-json/ 功能说明:将js对象table转换成javascrip ...
- trk压力测试工具(测试tcp)
wrk 是web站点压力测试工具 针对tcp协议的压力测试工具,没有找到合适的. 自己写一个,起名 trk.
- 对redis深入理解
1.Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet.如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog. ...
- 读取大csv文件数据插入到MySql或者Oracle数据库通用处理
import java.io.BufferedInputStream; import java.io.BufferedReader;import java.io.BufferedWriter;impo ...