A JSP counter that can prevent refreshing

Source: Internet
Author: User
Tags count integer
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>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.