Java web 5-3网页记数器
package beans;
public class Counter {
private int count;
public Counter() {count=0;}
public int getCount() {
count++;
return count;
}
public void setCount(int count) {this.count=count;}
}
<%@ 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>网页访问数量</title>
</head>
<body>
<jsp:useBean id="counter" scope="application" class="beans.Counter"/>
这次访问的是第1个页面:counter1.jsp!<br>
两页面共被访问次数:
<jsp:getProperty name="counter" property="count"/>
</body>
</html>
<%@ 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>网页访问数量</title>
</head>
<body>
<jsp:useBean id="counter" scope="application" class="beans.Counter"/>
这次访问的是第2个页面:counter2.jsp!<br>
两页面共被访问次数:
<jsp:getProperty name="counter" property="count"/>
</body>
</html>
运行: