欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java统计用户年/月/周/日网站访问量

程序员文章站 2022-07-09 21:10:17
一:准备工作,引入相关依赖: 二:运行效果图: 下一次访问 三:具体代码如下 (1):CountObjectInfo.java (2):CountXml.java: (3):具体页面index.jsp: (4):xmlcount.xml文件的初始内容如下: (放在WEB_INF目录下) 至此就结束了 ......

 

一:准备工作,引入相关依赖:

  Java统计用户年/月/周/日网站访问量

 

二:运行效果图:

Java统计用户年/月/周/日网站访问量

下一次访问

Java统计用户年/月/周/日网站访问量

三:具体代码如下

 (1):countobjectinfo.java

 1 package cn.csrc.base.count;
 2 
 3 import java.util.date;
 4 
 5 public class countobjectinfo {
 6 
 7     // 总访问量合计
 8     protected int totalcount = 0;
 9     // 日访问量
10     protected int daycount = 0;
11     // 周访问量
12     protected int weekcount = 0;
13     // 月访问量
14     protected int monthcount = 0;
15     // 年访问量
16     protected int yearcount = 0;
17     
18     // 临时访问量
19     protected int tempcount = 0;
20 
21     protected date date = new date();
22 
23     public int getdaycount() {
24         return daycount;
25     }
26 
27     public int getmonthcount() {
28         return monthcount;
29     }
30 
31     public int gettotalcount() {
32         return totalcount;
33     }
34 
35     public int getweekcount() {
36         return weekcount;
37     }
38 
39     public int getyearcount() {
40         return yearcount;
41     }
42 
43     public void setdaycount(int i) {
44         daycount = i;
45     }
46 
47     public void setmonthcount(int i) {
48         monthcount = i;
49     }
50 
51     public void settotalcount(int i) {
52         totalcount = i;
53     }
54 
55     public void setweekcount(int i) {
56         weekcount = i;
57     }
58 
59     public void setyearcount(int i) {
60         yearcount = i;
61     }
62 
63     public date getdate() {
64         return date;
65     }
66 
67     public void setdate(date date) {
68         this.date = date;
69     }
70 
71     public int gettempcount() {
72         return tempcount;
73     }
74 
75     public void settempcount(int i) {
76         tempcount = i;
77     }
78 
79 }

(2):countxml.java:

  1 package cn.csrc.base.count;
  2 
  3 import java.io.filereader;
  4 import java.io.filewriter;
  5 import java.text.simpledateformat;
  6 import java.util.calendar;
  7 import java.util.date;
  8 
  9 import org.exolab.castor.xml.marshaller;
 10 import org.exolab.castor.xml.unmarshaller;
 11 
 12 public class countxml {
 13 
 14     /** 在这里定义xmlcount.xml的绝对路径,注意此处代码要修改的哦 */
 15     private string filename = "d:\\mytools\\eclipse_new\\d\\workspace\\requestcount\\webcontent\\web-inf\\xmlcount.xml";
 16 
 17     private static countobjectinfo obj = null;
 18 
 19     private static countxml instance = null;
 20 
 21     public static countxml getinstance() {
 22         if (instance == null) {
 23             instance = new countxml();
 24         }
 25         return instance;
 26     }
 27 
 28     private countxml() {
 29         try {
 30             obj = read(filename);
 31         } catch (exception e) {
 32             system.out.println(e);
 33         }
 34 
 35     }
 36 
 37     public int gettotalcount() {
 38         return obj.gettotalcount();
 39     }
 40 
 41     public int getdaycount() {
 42         return obj.getdaycount();
 43     }
 44 
 45     public int getmonthcount() {
 46         return obj.getmonthcount();
 47     }
 48 
 49     public int getweekcount() {
 50         return obj.getweekcount();
 51     }
 52 
 53     public int getyearcount() {
 54         return obj.getyearcount();
 55     }
 56     
 57     public int gettempcount() {
 58         return obj.gettempcount();
 59     }
 60 
 61     public synchronized void addcount(date da) {// 比较日期增加计数
 62         if (new simpledateformat("yyyy-mm-dd").format(this.obj.date).equals(
 63                 new simpledateformat("yyyy-mm-dd").format(da)))
 64             this.obj.setdaycount(this.obj.getdaycount() + 1);
 65         else
 66             this.obj.setdaycount(1);
 67 
 68         if (new simpledateformat("yyyy-mm").format(this.obj.date).equals(
 69                 new simpledateformat("yyyy-mm").format(da)))
 70             this.obj.setmonthcount(this.obj.getmonthcount() + 1);
 71         else
 72             obj.setmonthcount(1);
 73 
 74         calendar ca = calendar.getinstance();
 75         ca.settime(da);
 76         ca.setfirstdayofweek(calendar.monday);
 77 
 78         if (ca.get(calendar.day_of_week) == calendar.monday
 79                 && !new simpledateformat("yyyy-mm-dd").format(this.obj.date)
 80                         .equals(new simpledateformat("yyyy-mm-dd").format(da)))
 81             obj.setweekcount(1);
 82         else
 83             obj.setweekcount(obj.getweekcount() + 1);
 84 
 85         if (new simpledateformat("yyyy").format(this.obj.date).equals(
 86                 new simpledateformat("yyyy").format(da)))
 87             this.obj.setyearcount(this.obj.getyearcount() + 1);
 88         else
 89             obj.setyearcount(1);
 90             obj.setdate(da);
 91 
 92         obj.settotalcount(obj.gettotalcount() + 1);
 93         obj.settempcount(obj.gettempcount() + 1);
 94         if (obj.gettempcount() >= 0) {// 只有当临时访问量大于等于20时才保存一次
 95             obj.settempcount(0);// 临时计数器置0
 96             write(filename);
 97         }
 98     }
 99 
100     private void write(string filename) {
101         try {
102             filewriter writer = new filewriter(filename);
103             marshaller.marshal(obj, writer);
104             writer.close();
105         } catch (exception e) {
106             system.out.println(e);
107 
108         }
109     }
110 
111     private countobjectinfo read(string filename) throws exception {
112         filereader reader = new filereader(filename);
113         countobjectinfo result = (countobjectinfo) unmarshaller.unmarshal(
114                 countobjectinfo.class, reader);
115         reader.close();
116         return result;
117     }
118 
119 }

(3):具体页面index.jsp:

 1 <%@ page language="java" contenttype="text/html; charset=utf-8"  
 2     pageencoding="utf-8"%>  
 3 <%@ page import="java.util.date" %>  
 4 <%@ page import="cn.csrc.base.count.countxml" %>    
 5 <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="content-type" content="text/html; charset=utf-8">
 9 <title>统计用户年/月/周/日网站访问量测试</title>
10 </head>
11 <body>
12     浏览器访问测试
13     <br>
14     <%
15         countxml xmlcount = countxml.getinstance();
16         if (session.isnew()) {
17             xmlcount.addcount(new date());//增加访问量
18             int n = xmlcount.gettotalcount();//取总访问量 
19             string count = integer.tostring(n);
20             session.putvalue("count", count);
21         }
22     %>
23     您是第<font color="red"><%=session.getvalue("count")%></font>位访问者 <br>  
24     总访问量:  <%=xmlcount.gettotalcount() %><br>
25     本年访问量:<%=xmlcount.getyearcount() %><br>
26     本月访问量:<%=xmlcount.getmonthcount() %><br>
27     本周访问量:<%=xmlcount.getweekcount() %><br>
28     本日访问量:<%=xmlcount.getdaycount() %><br>
29     临时访问量:<%=xmlcount.gettempcount()%><br>
30 </body>
31 </html>

(4):xmlcount.xml文件的初始内容如下: (放在web_inf目录下)

<?xml version="1.0" encoding="utf-8"?>  
<xml-body>  
</xml-body>  

至此就结束了!