js| Counter | refresh
<%@ page contenttype= "text/html;charset=gb2312"%>
<!--JSP counter-->
<%--prior to learning ASP, using ASP to prevent the refresh of the counter is very simple, the following is a JSP to do the counter--%>
<title>jsp counter </title>
<body>
<%@ page import= "java.io.*"%>
<%
Out.println (Request.getheader ("Cookie"));
String CurrentRecord = null;//variable to save text
BufferedReader file; BufferedReader object, for reading 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 empty, let it appear as 1 and write
Try
{readstr = File.readline ();}
catch (IOException E)
{System.out.println ("read data error.");}
if (readstr = = null) READSTR = "no record";
To judge cookies, add 1 for the first landing, and no cumulative count when refreshing
else if (Request.getheader ("Cookie") ==null)
{writestr = Integer.parseint (READSTR) +1;}
Else
{writestr = Integer.parseint (READSTR);}
Control for duplicate count due to refresh when writing
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 cnjsp's <b><font color= "Red" ><%=writeStr%></font></b> guest. </p>
</body>