日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

高訪問(wèn)量情況下 如何用JSP編寫(xiě)計(jì)數(shù)器程序_JSP教程

編輯Tag賺U幣
教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!

推薦:用JSP的Session實(shí)現(xiàn)在線用戶統(tǒng)計(jì)
現(xiàn)在對(duì)于處理在線用戶,有幾種不同的處理方法。   一種是頁(yè)面刷新由用戶控制,服務(wù)器端控制一個(gè)超時(shí)時(shí)間比如30分鐘,到了時(shí)間之后用戶沒(méi)有動(dòng)作就被踢出。這種方法的優(yōu)點(diǎn)是,如果用戶忘了

  有時(shí)要為每一篇文章統(tǒng)計(jì)其點(diǎn)擊次數(shù),如果每一次瀏覽都要更新一次庫(kù)的話,那性能在訪問(wèn)量很大的情況下,服務(wù)器的壓力就會(huì)很大了,比較好一點(diǎn)的方法就是先將要更新的數(shù)據(jù)緩存起來(lái),然后每隔一段時(shí)間再利用數(shù)據(jù)庫(kù)的批量處理,批量更新庫(kù)。源碼如下:

  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;//上次更新時(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 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");
 }
}
}
//注:如果你的數(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>");
%>

分享:weblogic的jsp問(wèn)題解決方法
在做項(xiàng)目的時(shí)候,jsp在運(yùn)行的時(shí)候出現(xiàn)了一些問(wèn)題,現(xiàn)將我的問(wèn)題解決方法做一個(gè)小結(jié),供以后作項(xiàng)目的參考。 模板無(wú)憂 問(wèn)題1: weblogic 的數(shù)據(jù)庫(kù)連接數(shù)目在程序運(yùn)行中不斷增長(zhǎng),最后

來(lái)源:模板無(wú)憂//所屬分類(lèi):JSP教程/更新時(shí)間:2008-08-22
相關(guān)JSP教程