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

2019-08-16 09:12:06来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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>  

至此就结束了!


原文链接:https://www.cnblogs.com/zhaosq/p/10998072.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:六:观察者模式

下一篇:面向对象——封装