Java Web five-Web site access Statistics __HTML5

Source: Internet
Author: User
Tags java web

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

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.