application
介绍
介绍:application意思是应用程序,是JSP内置对象,类似于“全局变量”,应用于实现用户之间的数据共享
常用方法
返回类型 | 方法 | 说明 |
---|---|---|
void | setAttribute(String key, Object value) | 以key/value的形式保存对象值 |
Object | getAttribute(String key) | 通过key获取对象值 |
String | getRealPath(String path) | 返回相对路径的真实路径 |
案例:实现访问人数统计
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
/* Integer 是 int 的封装类,
int 的默认I值是0
Integer 的默认值是null
*/
Integer count = 0;
Integer c = (Integer)application.getAttribute("count");
if(c != null){
//说明有数据
count = c;
}
count ++;//把当前的用户加进去
// 然后再把更改后的用户数量保存到application的作用域
application.setAttribute("count", count);
%>
<h1>
你是第<%=application.getAttri