成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

JSP教程之訪問量計數JSP源碼

開發 后端
本JSP教程旨在利用JSP源碼實現訪問量方面的一個計數方法,也是為解決在高訪問量的情況下服務器壓力過大的問題。

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

那么下面本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;//上次更新時間   
  88.  private static long executeSep=60000;//定義更新間隔時間,單位毫秒  
  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. //注:如果你的數據庫驅動支持批處理,那么可以將⑵,⑶標記的代碼前的注釋去掉,同時在代碼⑴前加上注釋   
  138.  
  139.   類寫好了,下面是在JSP中如下調用。  
  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教程為你提供的解決高訪問量下的服務器壓力問題,希望對你有幫助。

【編輯推薦】

  1. 對JSP中的內置對象簡單概述
  2. JSP和Servlet中的幾個編碼的作用及原理
  3. 使用JSP include機制改進外觀
  4. JSP編程應注意的六個常見問題
  5. 什么是JSP以及其強弱勢
責任編輯:仲衡 來源: 互聯網
相關推薦

2009-07-01 15:02:56

JSP程序JSP操作

2009-07-01 15:13:10

JSP留言板

2009-07-01 11:44:32

JSP學習教程

2009-06-30 10:37:56

JSP教程

2009-07-06 14:43:30

JSP元素

2009-06-30 16:33:42

JSP2.0特性JSP教程

2009-06-08 17:50:00

javalinuxjsp

2009-06-30 11:33:55

腳本JSP教程

2009-07-01 11:05:18

頁面與代碼分離JSP源碼

2009-06-30 10:05:24

MD5加密JSP源碼

2009-07-03 16:45:25

JSP實用教程

2009-07-01 10:46:57

JSP程序JSP代碼

2009-07-01 10:55:23

2009-07-02 11:34:42

JSP指令JSP開發

2009-07-07 14:04:55

JSP入門

2009-06-30 11:18:16

HTML表單JSP教程

2009-07-03 14:23:35

JSP實用案例教程

2009-07-06 16:55:06

JSP題目

2009-06-10 17:03:36

JSP動態生成

2009-03-16 15:07:20

JSP分頁window.openJSP表單
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产91 在线播放 | 亚洲a视| 成人一区二区三区 | 五十女人一级毛片 | 久久性色| 日本午夜网| 久久久精品国产 | 久久精品网 | 午夜网站视频 | 欧美a√ | 免费在线观看av网址 | 成人在线免费观看 | 在线欧美a| 欧美一级欧美三级在线观看 | 日韩欧美国产一区二区 | 国产一区二区三区在线免费 | 午夜精品一区二区三区在线 | 一区二区三区视频 | 日韩视频成人 | 久久综合九色综合欧美狠狠 | 美女一级黄 | 综合色播 | 国产精品久久久久久久午夜 | 日韩伦理一区二区三区 | 亚洲国产一区二区三区, | 国产美女一区二区 | 久操伊人 | 国产高清一区二区三区 | 视频国产一区 | 一区二区亚洲 | 久久精品视频网站 | 国产精品久久九九 | 午夜影院视频 | 国产一区91精品张津瑜 | 国产精品免费一区二区三区四区 | 一区二区在线视频 | 欧美高清视频 | 精品久久久久久久久久 | 精品欧美一区二区久久久伦 | 正在播放一区二区 | 免费看的av |