A JSP counter that prevents refreshing

Source: Internet
Author: User

<% @ 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>



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.