Recently learning Jave EE JSP Web Development, the need to achieve the statistics of Web page traffic, just at the beginning do not know how to achieve, and then asked the teacher, the teacher is like this answer me: to achieve the statistics of Web Access, you can use the Application object to achieve, Can not use the Seesion object, because the session belongs to the same conversation, turn off the browser data is not, and application is in the same browser, as long as the same browser, the data stored in the Applicaiton object, so that the data invariance. In fact, I understand these, I just do not know how to implement in the JSP code. Later I can only go online to see if there is a specific solution, search for a long time, there is no answer I want, I want to achieve just simple statistics, not to achieve more complex functions. Later in CSDN here to find the answer, here a simple summary of the implementation of Web page access statistics of several methods:
1. Using application objects for statistics, the effect is that every time you enter the page on the statistics once. But the effect is not good, because the general statistics page traffic, refresh is not counted in the statistics, here is this shortcoming.
Concrete implementation is:
<%@ page language= "java" import= "java.util.*" pageencoding= "GB2312"%>
2. In order to solve the above problem, there is another way, is to use the Application object and session object to statistics, the principle of this method from open browser to close the browser is access once, refresh, return and other operations do not count as a visit. But still has the flaw, when the JSP server starts afresh, the data also is clear zero.
The following is a specific implementation:
<%@ page language= "java" import= "java.util.*" pageencoding= "GB2312"%>
3. The third method is to store the statistics in a local file, such as a TXT file.
This is to resolve the data after restarting the server without fear of loss.
Create a class: Jspcount
Import Java.io.BufferedReader;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.PrintWriter; public class Jspcount {//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;
}
}
To build the JSP file in the Webroot directory: count.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "GB2312"%> <%@
page import= " Org.wwj.count.JSPCount "%>
After the program is run, a count.txt text file is generated for the corresponding Web project under the WebApps directory under Tomcat
4. The fourth method, just save the access to the statistics only, but did not guarantee that the page will not be refreshed when the increase, so it is not good. Of course there will always be a solution, the general solution is to combine the advantages of various schemes. The following is the Session object +application object +txt text to achieve the Web site access statistics.
Import Java.io.BufferedReader;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Javax.servlet.http.HttpServlet; 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;
}
}
JSP file code:
<% @page import= "Org.servlet.count.Counter"%>
<%@ page language= "java" import= "java.util.*" pageencoding= "GB2312"%>
The above four methods are the methods that are available for each improvement, if you want to achieve site access statistics, of course, the last one is the best, knowledge is not sweeping, need to constantly improve on the issue, to obtain the final solution, of course, the last one is not necessarily the best, implementation strategy, if you can use the database can be , but I think it's less efficient to read and write the database every time you visit the site.