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:13:10

JSP留言板

2009-07-01 15:02:56

JSP程序JSP操作

2009-07-01 11:44:32

JSP学习教程

2009-06-30 10:37:56

JSP教程

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 11:05:18

页面与代码分离JSP源码

2009-06-30 10:05:24

MD5加密JSP源码

2009-07-01 10:46:57

JSP程序JSP代码

2009-07-02 11:34:42

JSP指令JSP开发

2009-07-07 14:04:55

JSP入门

2009-07-03 14:23:35

JSP实用案例教程

2009-06-30 11:18:16

HTML表单JSP教程

2009-07-03 16:45:25

JSP实用教程

2009-06-30 15:54:00

数据库访问JSP

2009-07-02 09:00:25

JDBC设计JSP访问数据库

2009-03-16 09:09:18

数据库JDBCJSP
点赞
收藏

51CTO技术栈公众号