<% @ Page contentType = "text/html; charset = gb2312" %>
<! -- Jsp counter -->
<% -- When I used to learn ASP, it was very easy to use ASP to prevent refreshing. The following is a counter made with JSP -- %>
<Html>
<Head>
<Title> jsp counter </title>
</Head>
<Body>
<% @ Page import = "java. io. *" %>
<%
// Out. println (request. getHeader ("Cookie "));
String currentRecord = null; // variable for saving text
BufferedReader file; // BufferedReader object, used to read file data
String nameOfTextFile = "count.txt ";
// Read
File = new BufferedReader (new FileReader (nameOfTextFile ));
String readStr = null;
Int writeStr = 0; // If the Count value in the count text is null, it is displayed as 1 and written
Try
{ReadStr = file. readLine ();}
Catch (IOException e)
{System. out. println ("Data reading error .");}
If (readStr = null) readStr = "no record ";
// Determine the cookie. Add 1 when logging on for the first time, and do not count the total number when refreshing.
Else if (request. getHeader ("Cookie") = null)
{WriteStr = Integer. parseInt (readStr) + 1 ;}
Else
{WriteStr = Integer. parseInt (readStr );}
// Control the repeated Count caused by refresh during write operations
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"> you are the <B> <font color = "red"> <% = writeStr %> </font> </B> guest of CNJSP.. </P>
</Body>
</Html>