1. Create a Java class to read and write files
Write a file
public static void WriteFile (String filename,long counter) throws IOException {
PrintWriter out=new PrintWriter (new FileWriter (filename));
OUT.PRINTLN (counter);
Out.close ();
}
Read the file
public static long ReadFile (String filename) {
Long couter=0;
File F=new file (filename);
if (!f.exists ()) {
try {
WriteFile (filename, 0);
} catch (IOException e) {
E.printstacktrace ();
}
}
try {
BufferedReader in=new BufferedReader (new FileReader (f));
try {
Couter=long.parselong (In.readline ());
} catch (NumberFormatException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
couter=0;
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
return couter;
}
2.jsp Page Call
<%
String realpath = Request.getsession (). Getservletcontext ()
. Getrealpath ("/")
+ "Counter.txt";
Long counter = Common.readfile (Realpath);
if (Session.getattribute ("visitor") ==null) {
session.setattribute ("visitor", "yes");
session.setmaxinactiveinterval (3600);
counter++;
common.writefile (Realpath, counter);
}
%>
This page has been accessed <font color= ' red ' ><%=counter%></font> times