您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)JSP如何實(shí)現(xiàn)訪問(wèn)量計(jì)數(shù),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
有時(shí)要為每一篇文章統(tǒng)計(jì)其點(diǎn)擊次數(shù),如果每一次瀏覽都要更新一次庫(kù)的話(huà),那性能在訪問(wèn)量很大的情況下,服務(wù)器的壓力就會(huì)很大了,比較好一點(diǎn)的方法就是先將要更新的數(shù)據(jù)緩存起來(lái),然后每隔一段時(shí)間再利用數(shù)據(jù)庫(kù)的批量處理,批量更新庫(kù)。
那么下面本JSP教程提供源碼如下:
CountBean.java /* * CountData.java * * Created on 2009年6月30日, 下午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; /** * * */ 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 2009年6月30日, 下午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 2009年6月30日, 下午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;//上次更新時(shí)間 private static long executeSep=60000;//定義更新間隔時(shí)間,單位毫秒 /** 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 hitshits=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"); } } } //注:如果你的數(shù)據(jù)庫(kù)驅(qū)動(dòng)支持批處理,那么可以將⑵,⑶標(biāo)記的代碼前的注釋去掉,同時(shí)在代碼⑴前加上注釋 類(lèi)寫(xiě)好了,下面是在JSP中如下調(diào)用。 ﹤% 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﹥"); %﹥
關(guān)于“JSP如何實(shí)現(xiàn)訪問(wèn)量計(jì)數(shù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。