2.
1. //获取servletcontext,
ServletContext servletcontext=this.getServletContext();
//设置属性
servletcontext.setAttribute("name", "ru");
out.println("传递参数‘南成如’到name属性中");
2.//获取servletcontext
ServletContext servletcontext=this.getServletContext();
//获取属性对象。
String sc=(String)servletcontext.getAttribute("name");
out.println("未删除属性前:"+sc);
//删除属性对象
servletcontext.removeAttribute("name");
sc=(String)servletcontext.getAttribute("name");
out.println("删除后:"+sc);
3.用 ServletContext
首先在webroot目录下创建properties,(new-file-dbinf.properties)
//读取文件
//读取文件
InputStreaminputstream=this.getServletContext().getResourceAsStream("dbinf.properties");
//通过Properties对象获取properties中的信息
Properties properties=new Properties();
properties.load(inputstream);
//读取信息
out.println("username="+properties.getProperty("username")+"password="+properties.getProperty("password"));
注:如果 dbinf.properties放在了src下,需要使用类加载器去加载。
InputStreaminputstream=classname.class.getClassLoader().getResourceAsStream("dbinf.properties");
4. 读取文件的实际路径
注:如果 dbinf.properties放在了src下,需要使用类加载器去加载。
InputStreaminputstream=classname.class.getClassLoader().getResourceAsStream("dbinf.properties");
4. 读取文件的实际路径
//读取文件的实际路径
Stringpath=this.getServletContext().getRealPath("/images/IMG_4679.JPG");