java web实例训练知识错误总结(二)

一、servletContext理解

1.点这里(详细)

2.便于理解

java web实例训练知识错误总结(二)
2.java web实例训练知识错误总结(二)

java web实例训练知识错误总结(二)

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  读取properties文件
java web实例训练知识错误总结(二)
首先在webroot目录下创建properties,(new-file-dbinf.properties)
//读取文件
InputStream inputstream=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下,需要使用类加载器去加载。
InputStream inputstream=classname.class.getClassLoader().getResourceAsStream("dbinf.properties");
4. 读取文件的实际路径 
//读取文件的实际路径
String path=this.getServletContext().getRealPath("/images/IMG_4679.JPG");
out.println("<img src='images/IMG_4679.JPG' width=600px height=500px>"+path);
java web实例训练知识错误总结(二)

二、requestdispatcher 的使用总结

点这里

三、

对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)