在建立一个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.

最新文章

  1. C++系统预定义4个用于标准数据流对象
  2. django 1.7之后python manage.py syncdb没有了
  3. MySQL高级查询语句
  4. 关于JS变量提升的一些坑
  5. 通过NuGet获取sqlite对应的.net的dll
  6. cocos2dx从入门到精通课程
  7. hdu 4280 网络流
  8. python(三)一个文件读写操作的小程序
  9. Web Development Terms
  10. Ubuntu 添加sudo用户
  11. IOS 保存图片至相册
  12. 【笨木头Lua专栏】基础补充04:迭代器初探
  13. C语言学习(一)
  14. 【转载】Session的生命周期
  15. HTML5 CSS3 专题 :诱人的实例 3D展示商品信息
  16. Java compiler 并行编译思考
  17. MyBatis insert/delete/update 的返回值
  18. HBuilder
  19. 使用yield生成器,用Python实现用户对用户输入信息的监听和过滤
  20. JS,Jquery获取各种屏幕的宽度和高度(转载)

热门文章

  1. Qt5 调试之详细日志文件输出(qInstallMessageHandler)
  2. Yarn 调度器Scheduler详解
  3. netty学习记录2
  4. thrift 调取 python php go 客户端代码
  5. 从一个线上服务器警告谈谈backlog
  6. unity3d NavMeshAgent 寻路画线/画路径
  7. adb常用命令(手机测试)
  8. ThinkPHP5 Model分层及多对多关联的建立
  9. npm 版本问题
  10. final 内部类 static