中国教程联盟—打造中国最全面的技术类教程网站
中国教程联盟首页 网站建设 | 图形图象 | 网络编程 | 数据库 | 网站运营 | 网络营销 | 网络安全 | 服务器类 | 组网玩网 | 系统优化 | 数码应用 |
电脑入门 | 办公软件 | 操作系统 | 多媒体 | 软件开发 | 电脑硬件 | 视频教学 | 就业培训 | 机械电子 | 业界动态 | 其他专题 |
网络营销策划
流行时尚网
网站推广 搜索引擎优化 网站评估分析 北京SEO培训 网站优化技术 SEO优化博客
网站教程搜索:    
 当前位置: 中国教程联盟jsp教程网络编程教程jsp实例教程→JSP高访问量下的计数程序 流行时尚 网络营销 图书排行榜

JSP高访问量下的计数程序

中国教程联盟 http://www.jclmcn.net 发布时间:2007-8-13 13:08:31 [ 字体: ]
JSP高访问量下的计数程序,有时要为每一篇文章统计其点击次数,如果每一次浏览都要更新一次库的话,那性能在访问量很大的情况下,服务器的压力就会很大了,比较好一点的方法就是先将要更新的数据缓存起来,然后每隔一段时间再利用数据库的批量处理,批量更新库。
    有时要为每一篇文章统计其点击次数,如果每一次浏览都要更新一次库的话,那性能在访问量很大的情况下,服务器的压力就会很大了,比较好一点的方法就是先将要更新的数据缓存起来,然后每隔一段时间再利用数据库的批量处理,批量更新库。源码如下: 

  CountBean.java

/*
* CountData.java
*
* Created on 2006年10月18日, 下午4:44
*
* 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;

/**
*
* @author http://www.tot.name
*/
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

/*
* CountCache.java
*
* Created on 2006年10月18日, 下午5:01
*
* 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 java.util.*;
/**
*
* @author http://www.tot.name
*/
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");
 }
}
}
//注:如果你的数据库驱动支持批处理,那么可以将⑵,⑶标记的代码前的注释去掉,同时在代码⑴前加上注释 

  类写好了,下面是在JSP中如下调用。

<%
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>");
%> 
作者:不详  来源于:中国教程联盟 [ ] [打 印]
上篇文章:jsp中生成图片缩略图的代码 
下篇文章:JSP/Servlet构建三层管理信息系统
相关技术教程
· JSP高访问量下的计数程序
- 全站教程排行 -
- 本类教程排行 -
   
- 精彩广告推荐 -
- IT图书教程 -
Office五合一教程(附盘) C++与数据结构实验教程 Maya 7从入门到精通
电子商务系统的开发与应用 3ds max6&After Effects6 精通CSS:高级Web标准
网页设计三剑客(附光盘) Dreamweaver 8 大师课堂 .NET约定、惯用法与模式
SQL 数据库开发详解含光盘 Delphi数据库设计与实例 ASP+XML+CSS网络开发编程
ASP.NET数据库设计教程 Flash 8完全自学手册 JAVA技术手册
Ps CS2 中文版入门与实战 ASP动态网页编程 电脑杂志——合订本 CAD CAM CAE
3DS MAX 8基础培训教程 企业软件开发与实施 认证 等级考试 数据库图书
网管员必读 Ajax高级程序设计
特别声明
本站除部分特别声明禁止转载的专稿外的其他技术教程可以自由转载,但请务必注明出处和原始作者。网站技术教程版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的技术文章有版权问题请联系本站,我们尽快予以更正。

- 教程分类导航 -
电脑入门教程
输入法 - 上网冲浪
网络概念 - 电子邮件
浏览技巧 - 其他相关
网站建设教程
HTML/CSS - 网页配色
DREAMWEAVER - FRONTPAGE
Adobe GoLive - 网页特效
图形图象教程
PHOTOSHOP - FIREWORKS
CORELDRAW - pageMaker
ILLUSTRATOR - Freehand
PhotoImpact - Painter
图像辅助 - 作品欣赏
网络编程教程
ASP - PHP - JSP
ASP.NET - XML - VBScript
JavaScript - 其他相关
数据库教程
MSAccess - SQLserver
MySQL - DB2 - ORACLE
SYBASE - FoxPro - 其它
网站运营教程
企业运营 - 策划盈利
搜索引擎 - 网站推广
Google - Baidu - Alexa
建站心得 - 站长故事
办公软件教程
Word - Excel - PowerPoint
WPS Office - 微软Office
多媒体教程
FLASH - 3dsmax - maya
Authorware - 媒体播放
媒体制作 - 制作课件
软件开发教程
JAVA - C语言 - C#语言
C++ - VC - VB - Delphi
汇编语言 - Powerbuilder
其他专题教程
毕业论文 - 软件应用
推荐下载 - 原创文章
网络营销工作室  网站建设 网站推广方案 搜索引擎优化 网站评估分析 北京SEO培训 网站优化技术 网络营销工具 SEO优化技术博客 流行时尚 时尚家居 两性生活 发型设计 
关于中国教程联盟 - 教程目录分类 - 网站建设 - 最新教程 - 热门教程 - 广告联系 - 商业合作 - 郑重声明 - 交换友情链接
服务项目:网页设计,网站程序开发,网站优化,搜索引擎推广。多年从事网站建设和网络营销服务,经验丰富。联系方式 QQ:6255939
Copyright © 2006-2007 jclmcn.com™,All Rights Reserved.页面装载时间: 秒. 津ICP备07000664号