首页 > 编程 > JSP > 正文

实例代码:JSP高访问量下的计数程序

2024-09-05 00:19:49
字体:
来源:转载
供稿:网友

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

  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;

/**
*
*/
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.*;
/**
*
*/
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.*;
/**
*/
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>");
%>

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表