聊一聊web应用对象:ServletContext

**定义:**WEB容器启动时,他会为每个web应用程序都创建一个对应的ServletContext对象,它代表当前web应用
ServletContext对象通常也被称为context**域对象**。
ServletConfig对象中维护了ServletContext对象的引用,可以通过ServletConfig、getServletContext方法获得ServletContext对象。

demo1存入对象 ServletContext demo2中取出来

ServletContext的作用
获取WEB应用的全局初始化参数。
String getInitParameter(String name)
Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
Enumeration getInitParameterNames()
Returns the names of the context’s initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.
这里写图片描述

实现数据的共享
删除数据
void removeAttribute(String name)
Removes the attribute with the given name from the servlet context.
存入数据
void setAttribute(String name, Object object)
Binds an object to a given attribute name in this servlet context.
获取数据
Object getAttribute(String name)
Returns the servlet container attribute with the given name, or null if there is no attribute by that name.

读取资源文件
InputStream getResourceAsStream(String path)
Returns the resource located at the named path as an InputStream object.
这里写图片描述

注意 setAttribute(String name,object) 把一个java对象和一个属性名绑定,并存放到ServletContext中,参数name指定属性名,参数Objcet表示共享数据。

小练习: 要求: 点击访问网页的次数,反馈到页面中
这里写图片描述
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值