`
zlwgege
  • 浏览: 14450 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

学习笔记:年、日、月,周访问量统计

阅读更多

 学习笔记:年、日、月,周访问量统计

学习别人的心得:

------------- 

应某位同学的要求,发布了这个源码,即下面的tt.jar文件,直接放到TOMCAT下面就可以用了

-------------

突然接到一个任务,要用JSP实现一个访问量计数器。无从下手,于是从网络上找,找到了这篇文章,果然同大多数网络文章一样,东西是对的,是好的,但东西就是不发全,很多JAR包还有XML都没发上来,完全没法用。加入我的附件中的JAR包,然后配上初使的XML,即可实现。

 

初使的xmlcount.xml

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

</xml-body>

 --------serializer.jar和xerceslmpl.jar一定要引入进来,要不然你效果可以实现,但是文件会写不进去,这样服务器关掉之后就取不到数据了。XMLCOUNT.XML里面会是空的

-------------------http://www.blogjava.net/hilor/archive/2009/04/03/135486.html 
    看了champion的文章《灵活的计数器的设计与实现》与源码,(这篇文章在 http://champion.ewuxi.com/old/opensource/my/count.htm),就做了这个练习。用session来计数,它是将访问量保存在一个xml文件中,读写xml文件用的是Castor。

一、这是jsp调用页testcount.jsp

<%@ page import="java.util.Date" %>
<%@ page import="count.CountXml" %>

计算器测试<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>

二、用到的两个类
1、CountXml.java

package count;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.Properties;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;


import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;

public class CountXml{
        private String fileName = "h:\\Tomcat 5.0\\webapps\\zz3zcwbwebhome\\WEB-INF\\xmlcount.xml";
        private static CountObjectInf 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);
            else
                this.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);
            else
                obj.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);
            else
                 obj.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);
           else
                obj.setYearCount(1);
           obj.setDate(da);

   obj.setTotalCount(obj.getTotalCount()+1); 
           obj.setTempCount(obj.getTempCount()+1);
           if(obj.getTempCount()>=20){//只有当临时访问量大于等于20时才保存一次
                     obj.setTempCount(0);//临时计数器置0
                      write(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 CountObjectInf read(String fileName) throws Exception {
                FileReader reader = new FileReader(fileName);
                CountObjectInf result = (CountObjectInf) 

Unmarshaller.unmarshal(CountObjectInf.class, reader);
                reader.close();
                return result;
         }
}

2、

package count;

import java.util.Date;
public class CountObjectInf {
// 总访问量合计
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();

/**
 * @return
 */
public int getDayCount() {
return dayCount;
}

/**
 * @return
 */
public int getMonthCount() {
return monthCount;
}

/**
 * @return
 */
public int getTotalCount() {
return totalCount;
}

/**
 * @return
 */
public int getWeekCount() {
return weekCount;
}

/**
 * @return
 */
public int getYearCount() {
return yearCount;
}

/**
 * @param i
 */
public void setDayCount(int i) {
dayCount = i;
}

/**
 * @param i
 */
public void setMonthCount(int i) {
monthCount = i;
}

/**
 * @param i
 */
public void setTotalCount(int i) {
totalCount = i;
}

/**
 * @param i
 */
public void setWeekCount(int i) {
  weekCount = i;
}

/**
 * @param i
 */
public void setYearCount(int i) {
  yearCount = i;
}



/**
 * @return
 */
public Date getDate() {
  return date;
}

/**
 * @param date
 */
public void setDate(Date date) {
  this.date = date;
}

/**
 * @return
 */
public int getTempCount() {
   return tempCount;
}

/**
 * @param i
 */
public void setTempCount(int i) {
  tempCount = i;
}

}

  • tt.jar (2 MB)
  • 下载次数: 122
2
1
分享到:
评论
4 楼 zlwgege 2010-12-17  
liuying_ly 写道
zlwgege 写道
<?xml version="1.0" encoding="UTF-8"?>
<count-object-inf month-count="1" total-count="9" temp-count="0" day-count="1" week-count="9" year-count="1"><date>2010-08-18T10:54:22.840+08:00</date></count-object-inf>

这是访问之后的XML被修改后的样子


我的xml里面还是没有参数   服务器重新启动就没数据了     那几个包怎么引入的啊   谢谢啦

放到lib文件之后,右件要添加的jar包,选择buildPath

还有.那个tt.jar是单独的.是我做的一个例子.看我文章开头的那句话
3 楼 liuying_ly 2010-12-14  
zlwgege 写道
<?xml version="1.0" encoding="UTF-8"?>
<count-object-inf month-count="1" total-count="9" temp-count="0" day-count="1" week-count="9" year-count="1"><date>2010-08-18T10:54:22.840+08:00</date></count-object-inf>

这是访问之后的XML被修改后的样子


我的xml里面还是没有参数   服务器重新启动就没数据了     那几个包怎么引入的啊   谢谢啦
2 楼 liuying_ly 2010-12-14  
楼主 你好    serializer.jar  castor-1.2-xml.jar  xercesImpl.jar tt.jar

这四个包就放入lib文件夹就可以了吗,它是可以自己 引入吗
1 楼 zlwgege 2009-08-18  
<?xml version="1.0" encoding="UTF-8"?>
<count-object-inf month-count="1" total-count="9" temp-count="0" day-count="1" week-count="9" year-count="1"><date>2010-08-18T10:54:22.840+08:00</date></count-object-inf>

这是访问之后的XML被修改后的样子

相关推荐

Global site tag (gtag.js) - Google Analytics