方案2jJsp计数器,同一电脑10秒内防刷新<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!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=gb2312">
<meta name="GENERATOR"   http-equiv="refresh"     >
<title>无标题文档</title>
<link href="css/counter.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<!--start counter  -->
<div align="center">
  <%@ page import="com.ckj.counter.*,java.io.*,java.util.Date"%>
  <%Counter ct=new Counter();
    String  count="0";
    String counterid="0";
   try{
   // String  strDirPath  =  new  File(application.getRealPath(request.getRequestURI())).getParent();  
   //System.out.println("目录的绝对路径:"  +  strDirPath  +  "<br/>");
   String  strFullPath  =  session.getServletContext().getRealPath("/");  
   System.out.println(strFullPath);
      ct.path=strFullPath+"/counter/count.txt";
  //1 session存储时间值,
  //2 下次比较一下 
  if(session.getAttribute("counttime")==null ){ //如果 初次访问 
   count=ct.doCount();
  // System.out.println("--------计算前11--------"+System.currentTimeMillis());
   session.setAttribute("counttime",String.valueOf(System.currentTimeMillis()));}
   else
   { //如果,刷新间隔超过 
     //System.out.println("--------刷新之差--------"+(System.currentTimeMillis()- Long.valueOf((String)session.getAttribute("counttime")).longValue())); 
   //大于10秒 
   if((System.currentTimeMillis()-Long.valueOf((String)session.getAttribute("counttime")).longValue())>9000) { 
        //System.out.println("--------计算后--------"+System.currentTimeMillis());
        count=ct.doCount(); 
        //session.removeAttribute("counttime");
        session.setAttribute("counttime",String.valueOf(System.currentTimeMillis()));
        }
        //小于10秒 
        else
          count=ct.getCount();
   }
   
    }catch(Exception e){
     
  }
  
  //如果10秒内刷新,不计数   if(true){ 
  }%>你是本站第 <span class="font12bgred" >&nbsp;<%=count%>&nbsp;</span> 位访问者!
<!-- end counter --></div>
</body>
</html>
 方案3、防刷新图形计数器(已调试) 实例6:防刷新图形计数器(已调试)程序说明:上次做的计数器没有采用对文件的读写操作,虽然用到了javabean但是并没有用到scope中的几个参数:appliction,page,session这些参数分别代表了bean的存活的周期,appliction>session>page我们利用这个原理来防止用户刷新从而提高访问次数的漏洞。程序准备:如果你使用的是apache+resin那么请你在resin的根目录下建立一个文本文件counter.txt并在里面任意写一个数字,比如100之类的。其次,请建立一个文件夹用来保存这个程序的所有文件,并在该文件夹里建立一个子目录images,用来保存10张图片,图片格式为gif,图片名称从0--9,图片内容就是0--9十个数字,分别与图片名称对应就可了。程序文件:index.jsp, addone.java, display.javaindex.jsp用来显示纪录结果。addone.java 是一个javabean用来写纪录到文件display.java 也是一个javabean用来读取纪录到index.jsp程序源码:index.jsp<%@ page import="popeyelin.*" contentType="text/html; charset=gb2312" language="java"  %>
<html>
<head>
<title>JSP图形防刷新计数器</title>
</head>
<body>
<jsp:useBean id="a" scope="page" class="popeyelin.addone"/>
<jsp:useBean id="b" scope="page" class="popeyelin.display"/>
  已经有
<% b.counter();
   for(int i=9;i>=0;i--) out.print(b.img[i]);
   %>
  个人访问这个页面
</body>
</html> addone.java package popeyelin;
import java.io.*;
import java.lang.*;
public class addone{
 private String s=new String(); //建立数组变量sl
 public void addone(){
  try {
   BufferedReader buff=new BufferedReader(new FileReader("conuter.txt"));
   String s=buff.readLine();
   int i=Integer.parseInt(s);  //将字符串变量s转化成整形
   i++;
   buff.close();//关闭对象
            PrintWriter pw=new PrintWriter(new FileWriter("oounter.txt"));
   String temp=Integer.toString(i);//将整形变量i转化成字符型
   pw.print(temp);
   pw.close(); 
  }
  catch(IOException e){
   System.out.println(e.toString());
  }
 }
} display.java
  package popeyelin;
import java.io.*;
import java.lang.*;
public class display{
 public String[]img=new String[10];
 public void counter(){
  try{
   BufferedReader buff=new BufferedReader(new FileReader("counter.txt"));
   String s=buff.readLine();
   int i=Integer.parseInt(s);
   int st=10;
   int j=0;
   while(j<=9) {
    img[j]=Integer.toString(i%st);
    img[j]=img[j]+".GIF";
    img[j]="images/"+img[j];
    img[j]="<img src="+img[j]+">";
    img[j]=img[j]+"</img>";
    i/=10;
    j++;
   }
  }
  catch(IOException e){
   System.out.println(e.toString());
  }
 }
} 编译addone.java和display.java后会生成一个文件夹,popeyelin,把这个文件夹拷贝到WEB-INF/class目录下,如果不存在,请手动建立。运行index.jsp你就可以看到这个图片计数器了程序分析:重点就在于对文件的读写,我们看如下代码  BufferedReader buff=new BufferedReader(new FileReader("conuter.txt"));
   String s=buff.readLine();
   int i=Integer.parseInt(s);  //将字符串变量s转化成整形
   i++;
   buff.close();//关闭对象
   PrintWriter pw=new PrintWriter(new FileWriter("oounter.txt"));
   String temp=Integer.toString(i);//将整形变量i转化成字符型
   pw.print(temp);
   pw.close(); 我们如果要对文件进行读写操作,就必须先后建立2个对象,来对文件分别进行读和写而且要注意,我们从文件里读出来的东西是字符串型的,如果我们要对他进行修改必须先转换成int型,要用到 integer.parseint(),如果我们要先东西到文件,同样要先将int转换成string用integer.tostring(),写文件用到pw.print()要写的内容可以保存在变量里,如:pw.print(temp),也可以直接写,如:pw.print("hello,world");记住,写完一定要关毕对象。pw.close() 方案4、用JSP制作页面防刷新计数器(PHP)</head> 
<body> 
<%@ page import="java.io.*" %> 
<% 
String currentRecord = null;//保存文本的变量 
BufferedReader file; //BufferedReader对象,用于读取文件数据 
String nameOfTextFile = "e:\\count.txt"; 
//读取 
file = new BufferedReader(new FileReader(nameOfTextFile)); 
String readStr =null; 
int writeStr =0; //如果计数文本中的计数值为空则让它显示时变成1并写入 
try { readStr = file.readLine(); } 
catch (IOException e) 
{ System.out.println("读取数据错误."); } 
if (readStr == null) readStr = "没有任何记录"; 
//判断cookie,第一次登陆时加1,刷新时不累计计数 
else if (request.getHeader("Cookie")==null) 
{ writeStr = Integer.parseInt(readStr)+1;} 
else { writeStr = Integer.parseInt(readStr);} 
//写入时控制因为刷新引起的重复计数 
if (request.getHeader("Cookie")==null) { 
try { 
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile)); 
pw.println(writeStr); 
pw.close();} 
catch(IOException e) { 
out.println(e.getMessage());} 

%> 
<p align="center">您是傲雪寒梅网站的第<b><font color="red"><%=writeStr%></font></b>位客人。</p> 
</body> 
</html>