<%@ page language="java" contentType="text/html; charset=UTF-8 "
pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
if(application.getAttribute("counter") == null ){
application.setAttribute("counter", "1");
}else{
String strnum = null;
strnum = application.getAttribute("counter").toString();
int iconut = 0;
iconut = Integer.valueOf(strnum).intValue();
iconut++;
application.setAttribute("counter", Integer.toString(iconut));
}
%>
您是第<%=application.getAttribute("counter") %>访问者!
</body>
</html>
知识兔页面运行结果如下:
刷新之后, 计数器加1,结果如下:
更换浏览器或更换客户端地址都会使其访问值正常递增。如果Tomcat服务器重新启动,计数器才会从1重新开始计数。