日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
JSP教程之訪問量計(jì)數(shù)JSP源碼

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

那么下面本JSP教程提供源碼如下:

 
 
 
  1. CountBean.java  
  2.  
  3. /*  
  4. * CountData.java  
  5. *  
  6. * Created on 2009年6月30日, 下午4:44  
  7. *  
  8. * To change this template, choose Tools | Options and locate the template under  
  9. * the Source Creation and Management node. Right-click the template and choose  
  10. * Open. You can then make changes to the template in the Source Editor.  
  11. */   
  12.  
  13. package com.tot.count;  
  14.  
  15. /**  
  16. *  
  17. *   
  18. */  
  19. public class CountBean {  
  20.  private String countType;  
  21.  int countId;  
  22.  /** Creates a new instance of CountData */  
  23.  public CountBean() {}  
  24.  public void setCountType(String countTypes){  
  25.   this.countType=countTypes;  
  26.  }  
  27.  public void setCountId(int countIds){  
  28.   this.countId=countIds;  
  29.  }  
  30.  public String getCountType(){  
  31.   return countType;  
  32.  }  
  33.  public int getCountId(){  
  34.   return countId;  
  35.  }  
  36. }   
  37.  
  38.   CountCache.java  
  39.  
  40. /*  
  41. * CountCache.java  
  42. *  
  43. * Created on 2009年6月30日, 下午5:01  
  44. *  
  45. * To change this template, choose Tools | Options and locate the template under  
  46. * the Source Creation and Management node. Right-click the template and choose  
  47. * Open. You can then make changes to the template in the Source Editor.  
  48. */  
  49.  
  50. package com.tot.count;  
  51. import java.util.*;  
  52. /**  
  53. *  
  54. * @author http://www.tot.name  
  55. */  
  56. public class CountCache {  
  57.  public static LinkedList list=new LinkedList();   
  58.  /** Creates a new instance of CountCache */  
  59.  public CountCache() {}  
  60.  public static void add(CountBean cb){  
  61.   if(cb!=null){  
  62.    list.add(cb);  
  63.   }  
  64.  }  
  65. }  
  66.  
  67.  CountControl.java  
  68.  
  69.  /*  
  70.  * CountThread.java  
  71.  *  
  72.  * Created on 2009年6月30日, 下午4:57  
  73.  *  
  74.  * To change this template, choose Tools | Options and locate the template under  
  75.  * the Source Creation and Management node. Right-click the template and choose  
  76.  * Open. You can then make changes to the template in the Source Editor.  
  77.  */  
  78.  
  79. package com.tot.count;  
  80. import tot.db.DBUtils;  
  81. import java.sql.*;  
  82. /**  
  83. *  
  84. * @author http://www.tot.name  
  85. */  
  86. public class CountControl{   
  87.  private static long lastExecuteTime=0;//上次更新時(shí)間   
  88.  private static long executeSep=60000;//定義更新間隔時(shí)間,單位毫秒  
  89.  /** Creates a new instance of CountThread */  
  90.  public CountControl() {}  
  91.  public synchronized void executeUpdate(){  
  92.   Connection conn=null;  
  93.   PreparedStatement ps=null;  
  94.   try{  
  95.    conn = DBUtils.getConnection();   
  96.    conn.setAutoCommit(false);  
  97.    ps=conn.prepareStatement("update t_news set hitshits=hits+1 where id=?");  
  98.    for(int i=0;i﹤CountCache.list.size();i++){  
  99.     CountBean cb=(CountBean)CountCache.list.getFirst();  
  100.     CountCache.list.removeFirst();   
  101.     ps.setInt(1, cb.getCountId());  
  102.     ps.executeUpdate();⑴  
  103.     //ps.addBatch();⑵  
  104.    }  
  105.    //int [] counts = ps.executeBatch();⑶  
  106.    conn.commit();  
  107.   }catch(Exception e){  
  108.    e.printStackTrace();  
  109.   } finally{  
  110.   try{  
  111.    if(ps!=null) {  
  112.     ps.clearParameters();  
  113. ps.close();  
  114. ps=null;  
  115.   }  
  116.  }catch(SQLException e){}  
  117.  DBUtils.closeConnection(conn);  
  118.  }  
  119. }  
  120. public long getLast(){  
  121.  return lastExecuteTime;  
  122. }  
  123. public void run(){  
  124.  long now = System.currentTimeMillis();  
  125.  if ((now - lastExecuteTime) ﹥ executeSep) {  
  126.   //System.out.print("lastExecuteTime:"+lastExecuteTime);  
  127.   //System.out.print(" now:"+now+"\n");  
  128.   // System.out.print(" sep="+(now - lastExecuteTime)+"\n");  
  129.   lastExecuteTime=now;  
  130.   executeUpdate();  
  131.  }  
  132.  else{  
  133.   //System.out.print("wait for "+(now - lastExecuteTime)+" seconds:"+"\n");  
  134.  }  
  135. }  
  136. }  
  137. //注:如果你的數(shù)據(jù)庫(kù)驅(qū)動(dòng)支持批處理,那么可以將⑵,⑶標(biāo)記的代碼前的注釋去掉,同時(shí)在代碼⑴前加上注釋   
  138.  
  139.   類寫好了,下面是在JSP中如下調(diào)用。  
  140.  
  141. ﹤%  
  142. CountBean cb=new CountBean();  
  143. cb.setCountId(Integer.parseInt(request.getParameter("cid")));  
  144. CountCache.add(cb);  
  145. out.print(CountCache.list.size()+"﹤br﹥");  
  146. CountControl c=new CountControl();  
  147. c.run();  
  148. out.print(CountCache.list.size()+"﹤br﹥");  
  149. %﹥   
  150.  

以上就是本JSP教程為你提供的解決高訪問量下的服務(wù)器壓力問題,希望對(duì)你有幫助。


文章題目:JSP教程之訪問量計(jì)數(shù)JSP源碼
鏈接URL:http://www.5511xx.com/article/djcpgoe.html