国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

實例代碼:JSP高訪問量下的計數(shù)程序

2024-09-05 00:19:49
字體:
供稿:網(wǎng)友

  有時要為每一篇文章統(tǒng)計其點擊次數(shù),如果每一次瀏覽都要更新一次庫的話,那性能在訪問量很大的情況下,服務(wù)器的壓力就會很大了,比較好一點的方法就是先將要更新的數(shù)據(jù)緩存起來,然后每隔一段時間再利用數(shù)據(jù)庫的批量處理,批量更新庫。源碼如下:

  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");
 }
}
}

//注:如果你的數(shù)據(jù)庫驅(qū)動支持批處理,那么可以將⑵,⑶標記的代碼前的注釋去掉,同時在代碼⑴前加上注釋

  類寫好了,下面是在jsp中如下調(diào)用。

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

,歡迎訪問網(wǎng)頁設(shè)計愛好者web開發(fā)。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巴彦县| 漯河市| 江孜县| 民县| 墨江| 冷水江市| 始兴县| 正安县| 朝阳市| 安宁市| 军事| 石楼县| 固镇县| 蓬溪县| 土默特左旗| 江北区| 青海省| 辽阳县| 深州市| 东台市| 澄江县| 金华市| 晋州市| 佛山市| 福泉市| 嘉善县| 五莲县| 渭南市| 大洼县| 苏尼特右旗| 明溪县| 沁源县| 泽库县| 恩平市| 都昌县| 大姚县| 外汇| 新竹县| 沁阳市| 澄迈县| 平安县|