美文网首页
ServletContext/读取properties文件

ServletContext/读取properties文件

作者: 8dc010427b39 | 来源:发表于2017-01-20 16:49 被阅读0次

ServletContext  被服务器共享

web应用关闭 tomcat关闭 web应用reload ServletContext被销毁


获取ServletContext对象

ServletContext servletContext = this.getServletContext();

ServletContext servletContext2 = this.getServletConfig().getServletContext();


设置ServletContext对象

servletContext2.setAttribute("name", "aaa");


读取ServletContext对象

Object attribute =servletContext2.getAttribute("name");



删除ServletContext对象

servletContext2.getAttribute("name");



ServletContext对象读取properties文件

db.properties放在webRoot目录下

InputStream inputStream = this.getServletContext().getResourceAsStream("db.properties");

//创建properties

Properties properties = new Properties();

properties.load(inputStream);

properties.getProperty("name");


ServletContext对象imgs下图片路径

String path=this.getServletContext().getRealPath("/imgs/s.jpg");


文件放在src目录

InputStream inputStream=Servlet5.class.getClassLoader().getResourceAsStream("dbinfo.prop");

相关文章

网友评论

      本文标题:ServletContext/读取properties文件

      本文链接:https://www.haomeiwen.com/subject/qvawbttx.html