<%@ page contentType="text/html;charset=gb2312" %>
<!--jsp电子计数器-->
<%-- 之前学ASP时,用ASP做避免更新的电子计数器非常简单,下列是一个用JSP做的电子计数器--%>
<html>
<head>
<title>jsp电子计数器</title>
</head>
<body>
<%@ page import="java.io.*" %>
<%
//out.println(request.getHeader("Cookie"));
String currentRecord = null;//储存文字的自变量
BufferedReader file; //BufferedReader目标,用以读取文件数据信息
String nameOfTextFile = "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">您是CNJSP的第<b><font color="red"><%=writeStr%></font></b>位顾客。</p>
</body>
</html>