JSP教程之訪問量計(jì)數(shù)JSP源碼
作者:佚名
本JSP教程旨在利用JSP源碼實(shí)現(xiàn)訪問量方面的一個(gè)計(jì)數(shù)方法,也是為解決在高訪問量的情況下服務(wù)器壓力過大的問題。
有時(shí)要為每一篇文章統(tǒng)計(jì)其點(diǎn)擊次數(shù),如果每一次瀏覽都要更新一次庫(kù)的話,那性能在訪問量很大的情況下,服務(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í)在代碼⑴前加上注釋
- 類寫好了,下面是在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﹥");
- %﹥
以上就是本JSP教程為你提供的解決高訪問量下的服務(wù)器壓力問題,希望對(duì)你有幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來(lái)源:
互聯(lián)網(wǎng)