熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

JSP高訪問量下的計數程序

2022-06-13   來源: JSP教程 

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

  CountBeanjava

/*
* CountDatajava
*
* Created on 下午:
*
* To change this template choose Tools | Options and locate the template under
* the Source Creation and Management node Rightclick the template and choose
* Open You can then make changes to the template in the Source Editor
*/
  package comtotcount;

/**
*
* @author
*/
public class CountBean {
 private String countType;
 int countId;
 /** Creates a new instance of CountData */
 public CountBean() {}
 public void setCountType(String countTypes){
  thiscountType=countTypes;
 }
 public void setCountId(int countIds){
  thiscountId=countIds;
 }
 public String getCountType(){
  return countType;
 }
 public int getCountId(){
  return countId;
 }
}
  CountCachejava

/*
* CountCachejava
*
* Created on 下午:
*
* To change this template choose Tools | Options and locate the template under
* the Source Creation and Management node Rightclick the template and choose
* Open You can then make changes to the template in the Source Editor
*/

package comtotcount;
import javautil*;
/**
*
* @author
*/
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){
   listadd(cb);
  }
 }
}

 CountControljava

 /*
 * CountThreadjava
 *
 * Created on 下午:
 *
 * To change this template choose Tools | Options and locate the template under
 * the Source Creation and Management node Rightclick the template and choose
 * Open You can then make changes to the template in the Source Editor
 */

package comtotcount;
import totdbDBUtils;
import javasql*;
/**
*
* @author
*/
public class CountControl{
 private static long lastExecuteTime=;//上次更新時間 
 private static long executeSep=;//定義更新間隔時間單位毫秒
 /** Creates a new instance of CountThread */
 public CountControl() {}
 public synchronized void executeUpdate(){
  Connection conn=null;
  PreparedStatement ps=null;
  try{
   conn = DBUtilsgetConnection();
   connsetAutoCommit(false);
   ps=connprepareStatement("update t_news set hits=hits+ where id=?");
   for(int i=;i<CountCachelistsize();i++){
    CountBean cb=(CountBean)CountCachelistgetFirst();
    CountCachelistremoveFirst();
    pssetInt( cbgetCountId());
    psexecuteUpdate();⑴
    //psaddBatch();⑵
   }
   //int [] counts = psexecuteBatch();⑶
   conncommit();
  }catch(Exception e){
   eprintStackTrace();
  } finally{
  try{
   if(ps!=null) {
    psclearParameters();
psclose();
ps=null;
  }
 }catch(SQLException e){}
 DBUtilscloseConnection(conn);
 }
}
public long getLast(){
 return lastExecuteTime;
}
public void run(){
 long now = SystemcurrentTimeMillis();
 if ((now lastExecuteTime) > executeSep) {
  //Systemoutprint("lastExecuteTime:"+lastExecuteTime);
  //Systemoutprint(" now:"+now+"n");
  // Systemoutprint(" sep="+(now lastExecuteTime)+"n");
  lastExecuteTime=now;
  executeUpdate();
 }
 else{
  //Systemoutprint("wait for "+(now lastExecuteTime)+" seconds:"+"n");
 }
}
}
//注如果你的數據庫驅動支持批處理那麼可以將⑵⑶標記的代碼前的注釋去掉同時在代碼⑴前加上注釋
  類寫好了下面是在JSP中如下調用

<%
CountBean cb=new CountBean();
cbsetCountId(IntegerparseInt(requestgetParameter("cid")));
CountCacheadd(cb);
outprint(CountCachelistsize()+"<br>");
CountControl c=new CountControl();
crun();
outprint(CountCachelistsize()+"<br>");
%>
 


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20050.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.