ServletContext/读取properties文件

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文件)