高訪問(wèn)量情況下 如何用JSP編寫(xiě)計(jì)數(shù)器程序_JSP教程
教程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),最后
相關(guān)JSP教程:
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復(fù)習(xí)整理
- JSP腳本元素和注釋復(fù)習(xí)總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實(shí)現(xiàn)
- 網(wǎng)頁(yè)模板:關(guān)于jsp頁(yè)面使用jstl的異常分析
- JSP頁(yè)面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫(kù)的配置方法
- JDBC連接Access數(shù)據(jù)庫(kù)的幾種方式介紹
- 網(wǎng)站圖片路徑的問(wèn)題:絕對(duì)路徑/虛擬路徑
- (jsp/html)網(wǎng)頁(yè)上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對(duì)路徑下的圖片解決方法
JSP教程Rss訂閱編程教程搜索
JSP教程推薦
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP進(jìn)行數(shù)據(jù)庫(kù)連接的實(shí)例
- 解決JSP中使用request亂碼問(wèn)題
- J2ME手機(jī)開(kāi)發(fā)的部分問(wèn)題解答
- 解決JSP中拼裝數(shù)據(jù)為XML出現(xiàn)的問(wèn)題
- json實(shí)現(xiàn)jsp分頁(yè)實(shí)例介紹(附效果圖)
- Windows 2000 server下搭建JSP網(wǎng)站環(huán)境
- J2ME開(kāi)發(fā)之手機(jī)鍵盤(pán)使用注意問(wèn)題
- JSP教程tomcat配置數(shù)據(jù)庫(kù)連接池
- 解讀真正掌握J(rèn)ava語(yǔ)言需要了解的知識(shí)
猜你也喜歡看這些
- JSP由淺入深1—— 熟悉JSP服務(wù)器
- Servlet+Jsp實(shí)現(xiàn)圖片或文件的上傳功能具體思路及代碼
- Struts初級(jí)教程(6)-使用模塊
- Linux系統(tǒng)下兩種自動(dòng)啟動(dòng)Tomcat的方法
- 用JSP來(lái)發(fā)送郵件
- 如何學(xué)習(xí)JSP
- Spring學(xué)習(xí)基礎(chǔ)---與Struts整合
- JSP中獲取ExtJS.Ajax前臺(tái)傳遞的JSON數(shù)據(jù)實(shí)現(xiàn)過(guò)程
- 提升JSP頁(yè)面響應(yīng)速度的七大技巧
- hibernate的緩存和CRUD
- 相關(guān)鏈接:
- 教程說(shuō)明:
JSP教程-高訪問(wèn)量情況下 如何用JSP編寫(xiě)計(jì)數(shù)器程序
。