JSP高访问量下的计数程序

2016-02-19 16:29 2 1 收藏

下面这个JSP高访问量下的计数程序教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!

【 tulaoshi.com - Web开发 】

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

   /*
   * 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+"");
  // System.out.print(" sep="+(now - lastExecuteTime)+"");
  lastExecuteTime=now;
  executeUpdate();
   }
   else{
  //System.out.print("wait for "+(now - lastExecuteTime)+" seconds:"+"");
   }
  }
  }
  //注:如果你的数据库驱动支持批处理,那么可以将⑵,⑶标记的代码前的注释去掉,同时在代码⑴前加上注释

  类写好了,下面是在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>");
  %>

来源:https://www.tulaoshi.com/n/20160219/1612267.html

延伸阅读
标签: ASP
  网页计数器DIY 随着网络大行其道,网页计数器也流行起来。事实上大多数网站均有网页计数器,用以反映该网站的访问量。计数器的来源很广,Frontpage等网页编辑器自带了网页计数器,有的站点也提供免费的计数器下载。其实熟悉了ASP编程后,自己做一个计数器很容易。下面介绍一种实现方法。 计数器原理是:在第一次使用网页时置初始值1,以...
标签: PHP
  1)文本计数器 <?php $countfile="/count.txt";  //设置保存数据的文件 if (!file_exists($countfile)){//判断文件是否存在 exec( "echo 0 $countfile"); } $fp = fopen($countfile,"rw"); $length=filesize($countfile); $num = fgets($fp,$length); $num += 1; exec( "rm -rf $...
标签: Web开发
  Sun推出的JSP(Java Server Pages)是一种执行于服务器端的动态网页开发技术,它基于Java技术。执行JSP时需要在Web服务器上架设一个编译JSP网页的引擎。配置 JSP 环境可以有多种途径,但主要工作就是安装和配置Web服务器和JSP引擎。 下面就以Tomcat作为JSP引擎,配合Tomcat、Apache、IIS这三种Web服务器来讲述3种搭建JSP运行环境的方案...
标签: Java JAVA基础
计数器是一般网站必备的东东,别小看它了,每当站长看着小小计数器上的数字飞速增长的时候,感觉实在是好极了。以前我们用cgi、asp来写计数器,这方面的文章很多了,在这里,我们将会采用目前比较流行的jsp技术演示如何做一个计数器。 其中我们用到了两个文件,test.jsp文件用于在浏览器中运行,counter.java是后台的一个小java bean程...
标签: Java JAVA基础
//counter.java 读写文件的一个bean import java.io.*; public class counter extends Object { private String currentRecord = null;//保存文本的变量 private BufferedReader file; //BufferedReader对象,用于读取文件数据 private String path;//文件完整路径名 public counter() { } //ReadFile方法用来读取文件filePath中的数据,并返回...

经验教程

609

收藏

31
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部