概述
Application 的作用范围在服务器一开始执行服务,到服务器关闭为止。 Application 相比于Session、request和page的范围最大、停留的时间也最久。
存入application的信息在Web应用程序运行期间,所有的页面都可以访问这个信息。
4大作用域的范围
- application:服务器启动到停止这段时间。
- session:HTTP会话开始到结束这段时间。
- request:HTTP请求开始到结束这段时间。
- page:当前页面从打开到关闭这段时间。
常用方法
// 返回给定名的属性值
1、 getAttribute(String name)
// 设置属性的属性值
2、 setAttribute(String name , Object object)
// 删除给定属性及其属性值
3、 removeAttribute(String name)
// 返回所有可用属性名的枚举
4、 getAttributeNames()
// 返回给定虚拟路径的真实路径
5、 getRealPath(String path)
// 返回给定资源(文件或目录)的URL路径
6、 getResource(String path)
代码演示
<%
Integer count = (Integer)application.getAttribute("count");
if(count != null){
count ++;
}else{
count = 1;
}
application.setAttribute("count",count);
%>
<%
Integer i = (Integer)application.getAttribute("count");
out.println("您好,您是第 " + i +" 位访问本网站的用户" );
%>