`
xin
  • 浏览: 13589 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

jsp高访问量下的计数程序

阅读更多

有时要为每一篇文章统计其点击次数,如果每一次浏览都要更新一次库的话,那性能在访问量很大的情况下,服务器的压力就会很大了,比较好一点的方法就是先将要更新的数据缓存起来,然后每隔一段时间再利用数据库的批量处理,批量更新库。源码如下:

CountBean.java

Java代码
public class CountBean {  
 private String countType;  
 int countId;  
 /** Creates a new instance of CountData */ 
 public CountBean() {}  
 public void setCountType(String countTypes){  
  this.countType=countTypes;  
 }  
 public void setCountId(int countIds){  
  this.countId=countIds;  
 }  
 public String getCountType(){  
  return countType;  
 }  
 public int getCountId(){  
  return countId;  
 }  


public class CountBean {
 private String countType;
 int countId;
 /** Creates a new instance of CountData */
 public CountBean() {}
 public void setCountType(String countTypes){
  this.countType=countTypes;
 }
 public void setCountId(int countIds){
  this.countId=countIds;
 }
 public String getCountType(){
  return countType;
 }
 public int getCountId(){
  return countId;
 }
} CountCache.java

Java代码
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){  
   list.add(cb);  
  }  
 }  
}  
 
 CountControl.java  
 
 /* 
 * CountThread.java 
 * 
 * Created on 2006年10月18日, 下午4:57 
 * 
 * To change this template, choose Tools | Options and locate the template under 
 * the Source Creation and Management node. Right-click the template and choose 
 * Open. You can then make changes to the template in the Source Editor. 
 */ 
 
package com.tot.count;  
import tot.db.DBUtils;  
import java.sql.*;  
/** 

* @author http://www.tot.name 
*/ 
public class CountControl{   
 private static long lastExecuteTime=0;//上次更新时间   
 private static long executeSep=60000;//定义更新间隔时间,单位毫秒  
 /** Creates a new instance of CountThread */ 
 public CountControl() {}  
 public synchronized void executeUpdate(){  
  Connection conn=null;  
  PreparedStatement ps=null;  
  try{  
   conn = DBUtils.getConnection();   
   conn.setAutoCommit(false);  
   ps=conn.prepareStatement("update t_news set hits=hits+1 where id=?");  
   for(int i=0;i<CountCache.list.size();i++){  
    CountBean cb=(CountBean)CountCache.list.getFirst();  
    CountCache.list.removeFirst();   
    ps.setInt(1, cb.getCountId());  
    ps.executeUpdate();⑴  
    //ps.addBatch();⑵  
   }  
   //int [] counts = ps.executeBatch();⑶  
   conn.commit();  
  }catch(Exception e){  
   e.printStackTrace();  
  } finally{  
  try{  
   if(ps!=null) {  
    ps.clearParameters();  
ps.close();  
ps=null;  
  }  
 }catch(SQLException e){}  
 DBUtils.closeConnection(conn);  
 }  
}  
public long getLast(){  
 return lastExecuteTime;  
}  
public void run(){  
 long now = System.currentTimeMillis();  
 if ((now - lastExecuteTime) > executeSep) {  
  //System.out.print("lastExecuteTime:"+lastExecuteTime);  
  //System.out.print(" now:"+now+"\n");  
  // System.out.print(" sep="+(now - lastExecuteTime)+"\n");  
  lastExecuteTime=now;  
  executeUpdate();  
 }  
 else{  
  //System.out.print("wait for "+(now - lastExecuteTime)+" seconds:"+"\n");  
 }  
}  
}  
//注:如果你的数据库驱动支持批处理,那么可以将⑵,⑶标记的代码前的注释去掉,同时在代码⑴前加上注释 

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){
   list.add(cb);
  }
 }
}

 CountControl.java

 /*
 * CountThread.java
 *
 * Created on 2006年10月18日, 下午4:57
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.tot.count;
import tot.db.DBUtils;
import java.sql.*;
/**
*
* @author http://www.tot.name
*/
public class CountControl{
 private static long lastExecuteTime=0;//上次更新时间 
 private static long executeSep=60000;//定义更新间隔时间,单位毫秒
 /** Creates a new instance of CountThread */
 public CountControl() {}
 public synchronized void executeUpdate(){
  Connection conn=null;
  PreparedStatement ps=null;
  try{
   conn = DBUtils.getConnection();
   conn.setAutoCommit(false);
   ps=conn.prepareStatement("update t_news set hits=hits+1 where id=?");
   for(int i=0;i<CountCache.list.size();i++){
    CountBean cb=(CountBean)CountCache.list.getFirst();
    CountCache.list.removeFirst();
    ps.setInt(1, cb.getCountId());
    ps.executeUpdate();⑴
    //ps.addBatch();⑵
   }
   //int [] counts = ps.executeBatch();⑶
   conn.commit();
  }catch(Exception e){
   e.printStackTrace();
  } finally{
  try{
   if(ps!=null) {
    ps.clearParameters();
ps.close();
ps=null;
  }
 }catch(SQLException e){}
 DBUtils.closeConnection(conn);
 }
}
public long getLast(){
 return lastExecuteTime;
}
public void run(){
 long now = System.currentTimeMillis();
 if ((now - lastExecuteTime) > executeSep) {
  //System.out.print("lastExecuteTime:"+lastExecuteTime);
  //System.out.print(" now:"+now+"\n");
  // System.out.print(" sep="+(now - lastExecuteTime)+"\n");
  lastExecuteTime=now;
  executeUpdate();
 }
 else{
  //System.out.print("wait for "+(now - lastExecuteTime)+" seconds:"+"\n");
 }
}
}
//注:如果你的数据库驱动支持批处理,那么可以将⑵,⑶标记的代码前的注释去掉,同时在代码⑴前加上注释

Java代码
jsp页面中 

jsp页面中Java代码
<%  
CountBean cb=new CountBean();  
cb.setCountId(Integer.parseInt(request.getParameter("cid")));  
CountCache.add(cb);  
out.print(CountCache.list.size()+"<br>");  
CountControl c=new CountControl();  
c.run();  
out.print(CountCache.list.size()+"<br>");  
%> 

<%
CountBean cb=new CountBean();
cb.setCountId(Integer.parseInt(request.getParameter("cid")));
CountCache.add(cb);
out.print(CountCache.list.size()+"<br>");
CountControl c=new CountControl();
c.run();
out.print(CountCache.list.size()+"<br>");
%> 
分享到:
评论
1 楼 jcyanfan 2008-10-23  

相关推荐

    JSP高访问量下的计数程序

    下面将详细解析这三个类的功能以及它们如何协同工作来实现高访问量下对某个资源(如文章或页面)的计数。 ### 一、`CountBean` 类 `CountBean` 类主要用于存储计数相关的数据,例如计数类型(`countType`)和计数...

    jsp访问量

    "jsp访问量"这个主题通常涉及到如何统计和分析JSP页面被用户访问的数量,这是衡量网站或Web应用受欢迎程度和用户活动度的重要指标。 首先,我们需要理解JSP页面的工作原理。JSP文件本质上是HTML代码中嵌入了Java...

    jsp.rar_访问统计_访问量

    本教程将聚焦于如何使用JavaServer Pages(JSP)技术来实现页面的访问量统计。JSP是Java平台上的一个动态网页技术,允许开发者将HTML代码与Java代码结合,从而在服务器端生成动态内容。 首先,我们需要理解JSP的...

    jsp统计访问人数源码

    - 使用`application`对象:为了统计所有用户的总访问量,我们可以利用`application`对象存储全局的访问次数。每次有新访问时,我们都会增加这个值。 5. JSP脚本元素: 我们可以在JSP页面中使用脚本元素()来插入...

    JSP相关练习题,涉及语法及相关基础知识

    题目 1: 用 JSP 实现高访问量下的计数程序 这个题目考察了 JSP 在高并发访问下的计数程序实现。解决方案可以使用 JavaBean 来存储计数器的值,然后使用 JSP pageContext 对象来获取当前的计数值,并在页面上显示。...

    网站统计在线人数,当前年、月、日访问量JAVA代码

    2. **访问量计数**:每次用户访问页面时,我们可以增加一个全局的访问计数器。这个计数器可以是简单的变量,也可以存储在数据库或内存缓存(如Redis)中。对于日访问量,我们需要每天零点重置计数器。 以下是实现...

    六祎:使用Serlvet做的访问量统计!

    从文件列表来看,`index.jsp`很可能是用来展示访问统计结果的页面,可能包含HTML和JSP语法,用于渲染访问量。`WEB-INF`目录下通常存放Servlet配置文件`web.xml`,在这里可以定义Servlet的映射和初始化参数。而`META-...

    可运行-JSP访问统计

    **可运行-JSP访问统计** 在Java Web开发中,JSP(JavaServer Pages)是一种用于创建动态网页的技术。本文将深入探讨如何利用JSP实现一个简单的访问统计功能,以跟踪和展示网站用户的访问数据。 首先,我们需要理解...

    jsp网站完整实例

    "访问量"统计通常需要记录每个页面的访问次数,可以使用session或cookie来实现简单的计数,或者通过日志分析来获取更精确的数据。在Servlet中,可以设置一个全局变量或使用数据库记录每次访问,然后在页面上显示累计...

    JSP实现网站流量统计

    在网站运营中,流量统计是一项至关重要的任务,它能够帮助我们了解网站的访问情况,包括访问量、用户行为、热门页面等信息。本资源提供了JSP实现网站流量统计的一种方法,对于学习和实践Web开发,尤其是JSP应用,...

    javaweb访问量

    在Java Web开发中,"访问量"通常是指网站或应用程序被用户访问的次数。这是一个重要的指标,用于评估网站的受欢迎程度、流量以及潜在的商业价值。实现这一功能,开发者需要跟踪并记录每次用户请求,而这里提到的方法...

    完全基于jsp的在线统计系统

    本系统完全基于JSP(JavaServer Pages)技术,提供了一系列核心功能,包括网站计数器、用户停留时间统计、实时在线人数统计以及网站访问量统计。本文将深入探讨这些功能的实现原理及应用价值。 首先,网站计数器是...

    JSP调用JavaBean在网页上动态生成柱状图

    "JSP高访问量下的计数程序.txt"可能涉及在高并发环境下统计网页访问量的技术。在大量用户同时访问时,为了确保计数的准确性和系统的稳定,可能需要使用线程安全的计数方式,例如Java的`synchronized`关键字或者`...

    JSP+JAVA BEAN实现的计数器程序.rar_计数器

    在IT领域,尤其是在Web开发中,计数器是一种常见的功能,用于记录页面访问量或用户交互次数。本示例中的“JSP+JAVA BEAN实现的计数器程序”是利用JavaServer Pages(JSP)技术和JavaBeans组件来创建一个简单的计数器...

    访问统计模块jsp.rar

    5. **报表展示**:为了便于分析,访问统计模块可能还包含一个报表页面,使用JSP和HTML展示统计结果,如访问量排名、用户行为分析图表等。这可能涉及到使用JSP内置对象如`<c:forEach>`标签遍历数据集,或者使用开源的...

    访问量统计

    在IT行业中,访问量统计是网站运营和数据分析的基础工作之一,它可以帮助我们了解网站的受欢迎程度,优化用户体验,以及制定有效的市场策略。本教程将详细讲解如何使用Servlet过滤器(Filter)来实现一个简单的访问...

    jsp-mysql-counter.rar_jsp mysql

    - **并发控制**:在高并发环境下,要防止多个线程同时更新计数,可能导致计数错误。 - **缓存**:可以考虑使用缓存技术(如Redis)来减少对数据库的频繁访问。 - **数据库优化**:定期分析和优化SQL语句,确保...

    熟悉JSP开发环境

    - 在 `application` 对象中存储整个应用程序的访问计数。 - 使用 `out` 对象将计数结果显示在页面上。 #### 五、基本内置对象的使用(request, exception) **实验目的:** - 理解 `request` 和 `exception` ...

    JSP_counter计数器

    JSP计数器是一个实用的工具,用于追踪网页访问量。通过理解和实现JSP计数器,开发者可以更好地掌握JSP动态网页开发以及服务器端状态管理。同时,了解如何在JSP中处理数据持久化和状态保存,对提升Web应用的性能和...

Global site tag (gtag.js) - Google Analytics