First: Using the Application object (or ServletContext object) for statistics, the effect is that every time the page is entered once.
Because the general Statistics page traffic, refresh is not counted in the statistics, here is this shortcoming.
Second: Using application objects and session objects to count,
The principle of this method is to access the browser from the open browser to the closed
Refreshes, returns, and so on do not count as one visit.
But still has the flaw, when the JSP server starts afresh, the data also is clear zero.
Third, the statistical data stored in the local file, such as stored in a TXT file. This is to resolve the data after restarting the server without fear of loss.
Four: The Session object +application object +txt text to achieve the Web site access statistics.
The first step is to write a servlet:
public class Counter extends httpservlet{//write file method public static void Write2file (String filename, long count) {try
{PrintWriter out = new PrintWriter (new FileWriter (filename));
Out.println (count);
Out.close ();
catch (IOException e) {//Todo:handle exception e.printstacktrace ();
The method to read the file is public static long readfromfile (String filename) {File File = new file (filename);
Long Count = 0;
if (!file.exists ()) {try {file.createnewfile ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
Write2file (filename, 0);
} try{BufferedReader in = new BufferedReader (new FileReader (file));
try{count = Long.parselong (In.readline ());
catch (NumberFormatException e) {//Todo:handle exception e.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); catch (FileNotFoundException e) {//Todo:handle exception e.printstacktrace ();
return count; }
}
Second, build the JSP file in the Webroot directory
'
<% @page import= "Org.servlet.count.Counter"%>
<%@ page language= "java" import= "java.util.*" pageencoding= "GB2312"%>