Counters are essential to websites. Don't underestimate them. Every time the webmaster looks at the rapid growth of numbers on a small counter, it feels really good. In the past, we used cgi and asp to write counters. There are many articles in this regard. Here, we will use the popular jsp technology to demonstrate how to make a counter.
Two files are used. The test. jsp file is used to run in the browser. counter. java is a small java bean program in the background to read the counter value and write the counter value. For the storage of the counter, We have stored a file named lyfcount.txt.
The following is the detailed program code (put test. jsp to the web directory, and put counter. java to the class directory ):
// Test. jsp file
<% @ Page contentType = "text/html; charset = gb2312" %>
<HTML>
<HEAD>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta name = "GENERATOR" CONTENT = "Oracle JDeveloper">
<TITLE>
Counter demo
</TITLE>
</HEAD>
<BODY>
<! -- Create and call bean (counter) -->
<Jsp: useBean id = "counter" class = "counter" scope = "request">
</Jsp: useBean>
<%
// Call readfileenders of the counterobject to read the count in lyfcount.txt.
String cont = counter. ReadFile ("/lyfcount.txt ");
// Call the readfilecounter of the counterobject to add the counter to the lyfcount.txt file.
Counter. WriteFile ("/lyfcount.txt", cont); %>
You are the <font color = "red"> <% = cont %> </font> visitor
</BODY>
</HTML>