ServletContext 接口读取配置文件要注意的路径问题
在建立一个maven项目时,我们通常把一些文件直接放在resource下面,在ServletContext中有getResource(String path)和getResourceAsStream(String path)这两个方法,可以
利用这两个方法读取配置文件的内容:
配置文件在项目的位置如下图所示:
这里一定要注意读取配置文件应该以类路径,如resource目录下的db.properties文件,他的类路径是/WEB-INF/classes/db.properties(在target 目录下,我已经用红圈圈住了)
所以:读取这个配置文件的的代码如下:this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
下面引用servlet规范中的文字说明理由;
The getResource and getResourceAsStream methods take a String with a leading
“/” as an argument that gives the path of the resource relative to the root of the
context or relative to the META-INF/resources directory of a JAR file inside the
web application’s WEB-INF/lib directory. These methods will first search the root
of the web application context for the requested resource before looking at any of the
JAR files in the WEB-INF/lib directory. The order in which the JAR files in the
WEB-INF/lib directory are scanned is undefined. This hierarchy of documents may
exist in the server’s file system, in a Web application archive file, on a remote server,
or at some other location.
最新文章
- C++系统预定义4个用于标准数据流对象
- django 1.7之后python manage.py syncdb没有了
- MySQL高级查询语句
- 关于JS变量提升的一些坑
- 通过NuGet获取sqlite对应的.net的dll
- cocos2dx从入门到精通课程
- hdu 4280 网络流
- python(三)一个文件读写操作的小程序
- Web Development Terms
- Ubuntu 添加sudo用户
- IOS 保存图片至相册
- 【笨木头Lua专栏】基础补充04:迭代器初探
- C语言学习(一)
- 【转载】Session的生命周期
- HTML5 CSS3 专题 :诱人的实例 3D展示商品信息
- Java compiler 并行编译思考
- MyBatis insert/delete/update 的返回值
- HBuilder
- 使用yield生成器,用Python实现用户对用户输入信息的监听和过滤
- JS,Jquery获取各种屏幕的宽度和高度(转载)