站长中国
设为首页 | 站长论坛

站长论坛 站长下载
您所在的位置: 站长中国 > 站长学院 > 网络编程 > JSP专区 > 正文

JSP高访问量下的计数程序
  2008年02月22日23:53:06  评论(0条) 字体:[ ]
相关热点: 程序 计数 访问量 the public template and lastExecuteTime

  有时要为每一篇文章统计其点击次数,如果每一次浏览都要更新一次库的话,那性能在访问量很大的情况下,服务器的压力就会很大了,比较好一点的方法就是先将要更新的数据缓存起来,然后每隔一段时间再利用数据库的批量处理,批量更新库。源码如下:

  CountBean.java

/*
* CountData.java
*
* Created on 2006年10月18日, 下午4:44
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/

  package com.tot.count;

/**
*
* @author http://www.tot.name
*/
public class CountBean {
 private String countType;
 int countId;
 /** Creates a new instance of CountData */
 public CountBean() {}
 public void setCountType(String countTypes){
  this.countType=countTypes;
 }
 public void setCountId(int countIds){
  this.countId=countIds;
 }
 public String getCountType(){
  return countType;
 }
 public int getCountId(){
  return countId;
 }
}

  CountCache.java

/*
* CountCache.java
*
* Created on 2006年10月18日, 下午5:01
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/

package com.tot.count;
import java.util.*;
/**
*
* @author http://www.tot.name
*/
public class CountCache {
 public static LinkedList list=new LinkedList();
 /** Creates a new instance of CountCache */
 public CountCache() {}
 public static void add(CountBean cb){
  if(cb!=null){
   list.add(cb);
  }
 }
}

 CountControl.java

 /*
 * CountThread.java
 *
 * Created on 2006年10月18日, 下午4:57
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.tot.count;
import tot.db.DBUtils;
import java.sql.*;
/**
*
* @author http://www.tot.name
*/
public class CountControl{
 private static long lastExecuteTime=0;//上次更新时间 
 private static long executeSep=60000;//定义更新间隔时间,单位毫秒
 /** Creates a new instance of CountThread */
 public CountControl() {}
 public synchronized void executeUpdate(){
  Connection conn=null;
  PreparedStatement ps=null;
  try{
   conn = DBUtils.getConnection();
   conn.setAutoCommit(false);
   ps=conn.prepareStatement("update t_news set hits=hits+1 where id=?");
   for(int i=0;i<CountCache.list.size();i++){
    CountBean cb=(CountBean)CountCache.list.getFirst();
    CountCache.list.removeFirst();
    ps.setInt(1, cb.getCountId());
    ps.executeUpdate();⑴
    //ps.addBatch();⑵
   }
   //int [] counts = ps.executeBatch();⑶
   conn.commit();
  }catch(Exception e){
   e.printStackTrace();
  } finally{
  try{
   if(ps!=null) {
    ps.clearParameters();
ps.close();
ps=null;
  }
 }catch(SQLException e){}
 DBUtils.closeConnection(conn);
 }
}
public long getLast(){
 return lastExecuteTime;
}
public void run(){
 long now = System.currentTimeMillis();
 if ((now - lastExecuteTime) > executeSep) {
  //System.out.print("lastExecuteTime:"+lastExecuteTime);
  //System.out.print(" now:"+now+"\n");
  // System.out.print(" sep="+(now - lastExecuteTime)+"\n");
  lastExecuteTime=now;
  executeUpdate();
 }
 else{
  //System.out.print("wait for "+(now - lastExecuteTime)+" seconds:"+"\n");
 }
}
}
//注:如果你的数据库驱动支持批处理,那么可以将⑵,⑶标记的代码前的注释去掉,同时在代码⑴前加上注释

  类写好了,下面是在JSP中如下调用。

<%
CountBean cb=new CountBean();
cb.setCountId(Integer.parseInt(request.getParameter("cid")));
CountCache.add(cb);
out.print(CountCache.list.size()+"<br>");
CountControl c=new CountControl();
c.run();
out.print(CountCache.list.size()+"<br>");
%>



收藏本文 打印 打印本文  推荐本文 告诉好友 投稿 投稿邮箱

站长排行

学院

新闻

专栏

盈利

[揭密网络黄链]中国留学生买凶专破日本
windows 2008中文版iis7+asp+php+Acces
贴吧发帖机使用教程(绝对原创)
超强弹出窗口代码,什么都挡不住
Windows Vista局域网网络连接设置
JSP语法(6)
FLASH视觉特效实例之地震效果
关于数据分页(转自www.codeproject.co
教你如何在Vista中设置麦克风
不用Photoshop 在线恶搞大头像
全国哀悼日网友自发QQ隐身上线 向死难
淘宝网卖家公然叫卖“艳照门”照片集
抗震救灾/鱼目混珠?
Google绿色专家质疑黑色背景网页节省资
阿里妈妈广告卖主全攻略
电影小说类相关关键字搜索量列表
爆笑 阿里就这样把亿告征服!
大脚:实战单页面优化关键词之——淘宝
国内各IT企业办公环境揭秘(多图)
心系灾区,点燃希望——手机QQ2008祈福
ECSHOP模板制作参考文档
悬挂阿里妈妈会否被百度惩罚
ECSHOP模板下载
阿里妈妈是否是中小站长的救世主?
最强网店ECShop发新版 众多酷炫功能给
土豆网,优酷网,爆米花等视频网站采集
DedeCms模板安装/制作概述
网上商店系统巅峰对决 ECShop vs ShopE
编程中国全站采集规则
http://www.mt86.com全站采集规则
性福联盟 一个不尊重站长的联盟
百度网络广告利润增长将会持续多久
大脚:日赚100元—揭露最新firefox欺骗
分享经验:个人网站如何月赚1万!
大脚:垃圾站超级赚钱法之二—突破“站
迅雷联盟、快车联盟收入对比
大脚:垃圾站超级赚钱法之——前言
Google Adsense的秘密 第二版
风险投资公司vc联系方式名录大全(一)
经理人必看的十个管理网站
站长学院  网页设计 建站教程 图形图象 网络编程

制定完整的网站
制定完整的网站
中国企业网站的
中国企业网站的

网站运营之怎么样来粘住你的用
网麒:网站建设创意
怎样提高网页快照更新
两种方法,教你如何通过微软正
英文网站优化细节
利用SSL加密增强FTP服务器的安
网页设计常用Javascript经典代

新闻线索

如果你有站长界人事变动、重组并购、变革技术出现,以及产品投诉等重要新闻线索,请告诉我们,我们会给予特别关注。
0631-3653338
站长中国编辑部
站长中国24小时新闻热线: 13156089008