**定义:**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表示共享数据。
小练习: 要求: 点击访问网页的次数,反馈到页面中