ServletContext详解

ServletContext详解
2. ServletContext详解

ServletContext详解

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文件
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. 读取文件的实际路径 
//读取文件的实际路径
Stringpath=this.getServletContext().getRealPath("/images/IMG_4679.JPG");
out.println("<img src='images/IMG_4679.JPG'width=600px height=500px>"+path);
ServletContext详解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值