按照需求分为简单计数器,防止刷新计数器,单用户计数器。
1、简单计数器:简单计数器在获取用户发哦访问时对计数变量进行+1的操作,计数变量在所有的用户之间共享。同时在自增函数钱加上关键字synchronized,对其进行串行化处理。没有判断是否是一个用户刷新页面的功能。代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!
int counter =0 ;
synchronized void counterFuc()
{
counter =counter+1;
}
%>
<% counterFuc(); %>
简单计数器<br>
你是本站的地<%= counter %>访问者
</body>
</html>
2、防止刷新计数器:
在获取用户发哦访问时首先判断其session,如果是一个新的回话则访问量+1,同时读取服务器保存访问量的文本值,如果文本值大于会话中的访问变量时就意味着服务里重新启动过,计数器已经重新开始,这是应该把访问量赋值为文本中的访问量。代码如下:
<%@ page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META NAME="GENERATOR" CONTENT="Oracle JDeveloper">
<TITLE>
防刷新计数器
</TITLE>
</HEAD>
<BODY>
<!--创建并调用bean(counter)-->
<jsp:useBean id="counter" class="bean.counter" scope="request">
</jsp:useBean>
<%
int number=0;
String path=this.getServletContext().getRealPath("/");
//调用counter对象的ReadFile方法来读取文件count.txt中的计数
String cont=counter.ReadFile(path+"/count.txt");
//调用counter对象的WriteFile方法来将计数器加一后写入到文件count.txt中
if (session.isNew())
{
counter.WriteFile(path+"/count.txt",cont);
number=Integer.parseInt(cont);
number=number+1;
}
else
{
number=Integer.parseInt(cont);
}
%>
防刷新计数器<br>
您是第<%=number%>位访问者 <br>
-----------------
</BODY>
</HTML>
package bean;
import java.io.*;
public class counter extends Object {
private String currentRecord = null;//保存文本的变量
private BufferedReader file; //BufferedReader对象,用于读取文件数据
private String path;//文件完整路径名
public counter() {
}
//ReadFile方法用来读取文件filePath中的数据,并返回这个数据
public String ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
//创建新的BufferedReader对象
file = new BufferedReader(new FileReader(path));
String returnStr =null;
try
{
//读取一行数据并保存到currentRecord变量中
currentRecord = file.readLine();
}
catch (IOException e)
{//错误处理
System.out.println("读取数据错误.");
}
if (currentRecord == null)
//如果文件为空
returnStr = "没有任何记录";
else
{//文件不为空
returnStr =currentRecord;
}
//返回读取文件的数据
return returnStr;
}
//ReadFile方法用来将数据counter+1后写入到文本文件filePath中
//以实现计数增长的功能
public void WriteFile(String filePath,String counter) throws FileNotFoundException
{
path = filePath;
//将counter转换为int类型并加一
int Writestr = Integer.parseInt(counter)+1;
try {
//创建PrintWriter对象,用于写入数据到文件中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
//用文本格式打印整数Writestr
pw.println(Writestr);
//清除PrintWriter对象
pw.close();
} catch(IOException e) {
//错误处理
System.out.println("写入文件错误"+e.getMessage());
}
}
}
3、单用户计数器:
所谓单用户计数器自己了解自己的访问次数
先判断是否是一个新的会话,如果是一个新的会话则读取用户的cookie中的 logincount 值 ,该值记录了用户的访问次数,如果cookie不存在则用户访问次数为1。如果存在就执行+1操作。如果用户访问的是一个新的会话,还要把用户的访问量重新保存到cookie的变量中代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=GBK"
pageEncoding="GBK"
%>
<META http-equiv="Content-Type" content="text/html; charset=GBK">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE>防刷新计数器</TITLE>
</HEAD>
<BODY>
<%
int usercount=0;
Cookie thisCookie = null;
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++)
{
thisCookie = cookies[i];
if (thisCookie.getName().equals("Count"))
{
usercount=Integer.parseInt(thisCookie.getValue());
if (session.isNew())
{
usercount++;
}
thisCookie.setValue(String.valueOf(usercount));
thisCookie.setMaxAge(100000);
response.addCookie(thisCookie);
break;
}
}
if (usercount==0)
{
usercount=1;
thisCookie = new Cookie("Count", "1");
thisCookie.setMaxAge(100000);
response.addCookie(thisCookie);
}
%>
单用户计数器<br>
你是本站第<%=usercount%>个访问用户。
</BODY>
</HTML>
转载请注明原地址:http://blog.csdn.net/u013967628/article/details/38232811