自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JSP教程之訪問量計(jì)數(shù)JSP源碼

開發(fā) 后端
本JSP教程旨在利用JSP源碼實(shí)現(xiàn)訪問量方面的一個(gè)計(jì)數(shù)方法,也是為解決在高訪問量的情況下服務(wù)器壓力過大的問題。

有時(shí)要為每一篇文章統(tǒng)計(jì)其點(diǎn)擊次數(shù),如果每一次瀏覽都要更新一次庫(kù)的話,那性能在訪問量很大的情況下,服務(wù)器的壓力就會(huì)很大了,比較好一點(diǎn)的方法就是先將要更新的數(shù)據(jù)緩存起來(lái),然后每隔一段時(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ì)你有幫助。

【編輯推薦】

  1. 對(duì)JSP中的內(nèi)置對(duì)象簡(jiǎn)單概述
  2. JSP和Servlet中的幾個(gè)編碼的作用及原理
  3. 使用JSP include機(jī)制改進(jìn)外觀
  4. JSP編程應(yīng)注意的六個(gè)常見問題
  5. 什么是JSP以及其強(qiáng)弱勢(shì)
責(zé)任編輯:仲衡 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2009-06-30 10:37:56

JSP教程

2009-07-01 15:13:10

JSP留言板

2009-07-01 15:02:56

JSP程序JSP操作

2009-07-01 11:44:32

JSP學(xué)習(xí)教程

2009-06-30 16:33:42

JSP2.0特性JSP教程

2009-07-06 14:43:30

JSP元素

2009-06-30 11:33:55

腳本JSP教程

2009-06-08 17:50:00

javalinuxjsp

2009-07-01 10:55:23

2009-07-01 10:46:57

JSP程序JSP代碼

2009-06-30 11:18:16

HTML表單JSP教程

2009-07-03 14:23:35

JSP實(shí)用案例教程

2009-07-02 11:34:42

JSP指令JSP開發(fā)

2009-07-07 14:04:55

JSP入門

2009-07-01 11:05:18

頁(yè)面與代碼分離JSP源碼

2009-06-30 10:05:24

MD5加密JSP源碼

2009-07-03 16:45:25

JSP實(shí)用教程

2009-08-10 15:09:15

JSP架構(gòu)JSP開發(fā)工具

2009-07-06 17:46:25

JSP HTTP服務(wù)器

2009-06-30 15:54:00

數(shù)據(jù)庫(kù)訪問JSP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)