利用文件来存储访问数据,在访问时读取文件并将数值加1,在保存回文件,一下是代码示例: <%@ page contentType="text/html; charset=gb2312" %> <%@ page import="java.io.*" %> <%! int counter=0; String file="C://file.txt"; BufferedReader br=null; PrintWriter pw=null; public void add(){ File f=new File(file); String s=""; if(!f.exists()){ try{ //f.createNewFile(); pw=new PrintWriter(new FileWriter(file)); //此时生成文件 pw.println("0"); pw.close(); }catch(Exception e){ e.printStackTrace(); } } try{ br=new BufferedReader(new FileReader(file)); s=br.readLine(); int i=Integer.parseInt(s); counter=i+1; br.close(); pw=new PrintWriter(new FileWriter(file)); pw.println(counter); pw.close(); }catch(Exception e){ e.printStackTrace(); } } %> <% add(); %> <html> <head> <title>网页计数器</title> </head> <body> 你是本网站第<%=counter%>贵宾! </body> </html>