<p><br />
<%@ page contentType="text/html;charset=gb2312" %> <br />
<!--jsp计数器--> <br />
<%-- 以前学<a class="link_keyword" href="http://www.qqread.com/keywords/asp.html" target="_blank">ASP</a>时,用ASP做防止刷新的计数器很简单,以下是一个用JSP做的计数器--%> <br /><table border="0" align="left" class=ppc_download_ad_frame cellspacing=0 cellpadding=0 width="1%" style="margin-top:10px;margin-right:7px;margin-bottom:3px;margin-left:0px">
<tr>
<td id=a><img height=9 width=9></td>
<td id=b width="100%"><img height=1 width=1></td>
<td id=c><img height=9 width=9></td>
</tr>
<tr>
<td id=d><img height=1 width=1></td>
<td id=e valign=top height="100%"><!-- frame contents -->
<table cellspacing=0 cellpadding=0 border=0>
<tr>
<td><div id="w_hzh"></div></td>
</tr>
</table>
<!-- /frame contents --></td>
<td id=f><img height=1 width=1></td>
</tr>
<tr>
<td id=g><img height=9 width=9></td>
<td id=h><img height=1 width=1></td>
<td id=i><img height=9 width=9></td>
</tr>
</table>
<html> <br />
<head> <br />
<title>jsp计数器</title> <br />
</head> <br />
<body> <br />
<%@ page import="java.io.*" %> <br />
<% <br />
//out.println(request.getHeader("Cookie")); <br />
String currentRecord = null;//保存文本的变量 <br />
BufferedReader file; //BufferedReader对象,用于读取文件数据 <br />
String nameOfTextFile = "count.txt"; </p>
<p>//读取 <br />
file = new BufferedReader(new FileReader(nameOfTextFile)); <br />
String readStr =null; <br />
int writeStr =0; //如果计数文本中的计数值为空则让它显示时变成1并写入 <br />
try <br />
{ readStr = file.readLine(); } <br />
catch (IOException e) <br />
{ System.out.println("读取数据错误."); } <br />
if (readStr == null) readStr = "没有任何记录"; </p>
<p>//判断cookie,第一次登陆时加1,刷新时不累计计数 <br />
else if (request.getHeader("Cookie")==null) <br />
{ writeStr = Integer.parseInt(readStr)+1;} <br />
else <br />
{ writeStr = Integer.parseInt(readStr);} </p>
<p>//写入时控制因为刷新引起的重复计数 <br />
if (request.getHeader("Cookie")==null) <br />
{ <br />
try { <br />
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile)); <br />
pw.println(writeStr); <br />
pw.close();} <br />
catch(IOException e) { <br />
out.println(e.getMessage());} <br />
} <br />
%> <br />
<p align="center">您是CNJSP的第<b><font color="red"><%=writeStr%></font></b>位客人。</p> <br />
</body> <br />
</html>
</p>
</div>
</div>