jsp在request范围内利用useBean统计访客数量,jsp页面跳转之间统计页面浏览次数的实例源码:
scope_request.jsp源码:
<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘scope_request.jsp’ starting page</title>
</head>
<body>
<jsp:useBean id=”scpoe_request” class=”com.cn.scope.Page” scope=”request”/>
<br>
<p><font color=”blue”>
<h1>访问次数:<jsp:getProperty name=”scpoe_request” property=”num”/></h1>
</font></p>
<jsp:forward page=”scope_request_second.jsp”></jsp:forward>
</body>
</html>
scope_request_second.jsp源码:
<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘scope_request.jsp’ starting page</title>
</head>
<body>
<jsp:useBean id=”scpoe_request” class=”com.cn.scope.Page” scope=”request”/>
<br>
<p><font color=”blue”>
<h1>访问次数:<jsp:getProperty name=”scpoe_request” property=”num”/></h1>
</font></p>
<jsp:forward page=”scope_request_second.jsp”></jsp:forward>
</body>
</html>
Page.java源码文件:
package com.cn.scope;
public class Page {
private int num=0;
public int getNum() {
//num的值随访问次数增加而增加
num++;
return num;
}
public void setNum(int num) {
this.num = num;
}
}