Java实现:月,日,年,周,访问量统计
生活随笔
收集整理的這篇文章主要介紹了
Java实现:月,日,年,周,访问量统计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:工程截圖:
二:運行效果圖:
三:CountObjectInfo.java:
package cn.csrc.base.count;import java.util.Date;public class CountObjectInfo {// 總訪問量合計protected int totalCount = 0;// 日訪問量protected int dayCount = 0;// 周訪問量protected int weekCount = 0;// 月訪問量protected int monthCount = 0;// 年訪問量protected int yearCount = 0;// 臨時訪問量protected int tempCount = 0;protected Date date = new Date();public int getDayCount() {return dayCount;}public int getMonthCount() {return monthCount;}public int getTotalCount() {return totalCount;}public int getWeekCount() {return weekCount;}public int getYearCount() {return yearCount;}public void setDayCount(int i) {dayCount = i;}public void setMonthCount(int i) {monthCount = i;}public void setTotalCount(int i) {totalCount = i;}public void setWeekCount(int i) {weekCount = i;}public void setYearCount(int i) {yearCount = i;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}public int getTempCount() {return tempCount;}public void setTempCount(int i) {tempCount = i;}}四:CountXml.java: package cn.csrc.base.count;import java.io.FileReader; import java.io.FileWriter; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;import org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.Unmarshaller;public class CountXml {/** 在這里定義XMLCOUNT.XML的絕對路徑,注意此處代碼要修改的哦 */private String fileName = "E:\\workspace\\Count\\WebContent\\WEB-INF\\xmlcount.xml";private static CountObjectInfo obj = null;private static CountXml instance = null;public static CountXml getInstance() {if (instance == null) {instance = new CountXml();}return instance;}private CountXml() {try {obj = read(fileName);} catch (Exception e) {System.out.println(e);}}public int getTotalCount() {return obj.getTotalCount();}public int getDayCount() {return obj.getDayCount();}public int getMonthCount() {return obj.getMonthCount();}public int getWeekCount() {return obj.getWeekCount();}public int getYearCount() {return obj.getYearCount();}public synchronized void addcount(Date da) {// 比較日期增加計數if (new SimpleDateFormat("yyyy-MM-dd").format(this.obj.date).equals(new SimpleDateFormat("yyyy-MM-dd").format(da)))this.obj.setDayCount(this.obj.getDayCount() + 1);elsethis.obj.setDayCount(1);if (new SimpleDateFormat("yyyy-MM").format(this.obj.date).equals(new SimpleDateFormat("yyyy-MM").format(da)))this.obj.setMonthCount(this.obj.getMonthCount() + 1);elseobj.setMonthCount(1);Calendar ca = Calendar.getInstance();ca.setTime(da);ca.setFirstDayOfWeek(Calendar.MONDAY);if (ca.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY&& !new SimpleDateFormat("yyyy-MM-dd").format(this.obj.date).equals(new SimpleDateFormat("yyyy-MM-dd").format(da)))obj.setWeekCount(1);elseobj.setWeekCount(obj.getWeekCount() + 1);if (new SimpleDateFormat("yyyy").format(this.obj.date).equals(new SimpleDateFormat("yyyy").format(da)))this.obj.setYearCount(this.obj.getYearCount() + 1);elseobj.setYearCount(1);obj.setDate(da);obj.setTotalCount(obj.getTotalCount() + 1);obj.setTempCount(obj.getTempCount() + 1);if (obj.getTempCount() >= 0) {// 只有當臨時訪問量大于等于20時才保存一次obj.setTempCount(0);// 臨時計數器置0write(fileName);}}private void write(String fileName) {try {FileWriter writer = new FileWriter(fileName);Marshaller.marshal(obj, writer);writer.close();} catch (Exception e) {System.out.println(e);}}private CountObjectInfo read(String fileName) throws Exception {FileReader reader = new FileReader(fileName);CountObjectInfo result = (CountObjectInfo) Unmarshaller.unmarshal(CountObjectInfo.class, reader);reader.close();return result;}}
五:index.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ page import="cn.csrc.base.count.CountXml" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>計算器測試</title> </head> <body>計算器測試<br><%CountXml xmlcount = CountXml.getInstance();if (session.isNew()) {xmlcount.addcount(new Date());//增加訪問量int n = xmlcount.getTotalCount();//取總訪問量 String count = Integer.toString(n);session.putValue("count", count);}%>您是第<font color="red"><%=session.getValue("count")%></font>位訪問者 <br> 總訪問量: <%=xmlcount.getTotalCount() %><br>本年訪問量:<%=xmlcount.getYearCount() %><br>本月訪問量:<%=xmlcount.getMonthCount() %><br>本周訪問量:<%=xmlcount.getWeekCount() %><br>本日訪問量:<%=xmlcount.getDayCount() %><br> </body> </html>
六:xmlcount.xml文件的初始內容如下: <?xml version="1.0" encoding="UTF-8"?> <xml-body> </xml-body>
七:源碼下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/7615273
八:程序參考地址:http://bujingyun23.blog.163.com/blog/static/18131024320131594942633/
總結
以上是生活随笔為你收集整理的Java实现:月,日,年,周,访问量统计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux suid提权原理,Linux
- 下一篇: 手机游戏降低游戏延迟的软件_怎么降低手机