jsp實(shí)現(xiàn)將動(dòng)態(tài)網(wǎng)頁轉(zhuǎn)換成靜態(tài)頁面的方法
本文實(shí)例講述了jsp實(shí)現(xiàn)將動(dòng)態(tài)網(wǎng)頁轉(zhuǎn)換成靜態(tài)頁面的方法。分享給大家供大家參考。具體如下:
如果我可以將jsp動(dòng)態(tài)網(wǎng)頁轉(zhuǎn)換成靜態(tài)頁面,那么訪問的時(shí)候就不需要頻繁的訪問數(shù)據(jù)庫了。
jsp 顯示內(nèi)容緩存技巧
前段時(shí)間做自己社區(qū)的論壇,在jive 的基礎(chǔ)上做一個(gè)頁面顯示所有論壇的帖子,可以稱之為總版,模仿forum 類的接口做個(gè)superforum 并且實(shí)現(xiàn)cachable,不過因?yàn)檫@個(gè)頁面刷新量比較大,雖然被cache 了,我還是想辦法進(jìn)行頁面的緩存,感覺用jsp 產(chǎn)生的html靜態(tài)內(nèi)容當(dāng)緩存,頁面訪問速度應(yīng)該有所提高。
首先想到的一種辦法,是采用java.net 的urlconnection 把服務(wù)器上的jsp 抓過來做緩存,不過我覺得這樣做太見外了,自己服務(wù)器上的東西,為何要用http 去訪問.于是想另外一個(gè)辦法,把jsp 的out 對(duì)象的輸出控制到自己希望的地方.比如輸出到靜態(tài)文件,又或者保存成全局的字符串變量.這樣的話,瀏覽就不需要執(zhí)行jsp,只是瀏覽該html 了.僅僅在數(shù)據(jù)有更新的時(shí)候進(jìn)行一次update 操作,把jsp 重新輸出為html.
我覺得,瀏覽事件比數(shù)據(jù)插入或更新發(fā)生的次數(shù)多的時(shí)候.不妨試試這個(gè)辦法來提高頁面訪問速度.
整件事情有點(diǎn)像把jsp 當(dāng)作模板,生成靜態(tài)的html 頁面.
將如下代碼寫入web-xml:
<filter> <filter-name>filecapturefilter</filter-name> <filter-class>com.junjing.filter.filecapturefilter</filter-class> </filter> <filter-mapping> <filter-name>filecapturefilter</filter-name> <url-pattern>/latest.jsp</url-pattern> </filter-mapping>
latest.jsp 是我要cache 的頁面
java 源碼代碼如下:
/** * start file filecapturefilter.java */ package com.junjing.filter; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class filecapturefilter implements filter { private string protdirpath; public void init(filterconfig filterconfig) throws servletexception { protdirpath = filterconfig.getservletcontext().getrealpath("/"); } public void dofilter(servletrequest request,servletresponse response,filterchain chain) throws ioexception, servletexception { string filename = protdirpath + "forum/lastest.html"; printwriter out = response.getwriter(); filecaptureresponsewrapper responsewrapper = new filecaptureresponsewrapper((httpservletresponse)response); chain.dofilter(request, responsewrapper); // fill responsewrapper up string html = responsewrapper.tostring(); //得到的html 頁面結(jié)果字符串 // responsewrapper.writefile(filename); // dump the contents 寫成html 文件,也可以保存在內(nèi)存 //responsewrapper.writeresponse( out ); // back to browser //responsewrapper.sendredirect("lastestthread.jsp"); } public void destroy() {} } /** * end file filecapturefilter.java */
/** * start file filecaptureresponsewrapper.java */ package com.junjing.filter; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class filecaptureresponsewrapper extends httpservletresponsewrapper { private chararraywriter output; public string tostring() { return output.tostring(); } public filecaptureresponsewrapper(httpservletresponse response) { super(response); output = new chararraywriter(); } public printwriter getwriter() { return new printwriter(output); } public void writefile(string filename) throws ioexception { filewriter fw = new filewriter(filename); fw.write( output.tochararray() ); fw.close(); } public void writeresponse(printwriter out) { out.print( output.tochararray() ); } } /** * end file filecaptureresponsewrapper.java */
附件源代碼:
不過采用resin 服務(wù)器的話,以上代碼會(huì)失效。因?yàn)閞esin 沒有實(shí)現(xiàn)getwriter 方法,而是采用getoutputstream 取而代之,所以必須修改些代碼來迎合resin 運(yùn)行環(huán)境:
/** * start file filecaptureresponsewrapper.java */ package com.junjing.filter; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class filecaptureresponsewrapper extends httpservletresponsewrapper { private chararraywriter output; public string tostring() { return output.tostring(); } public filecaptureresponsewrapper(httpservletresponse response) { super(response); output = new chararraywriter(); } public printwriter getwriter() { return new printwriter(output); } public void writefile(string filename) throws ioexception { filewriter fw = new filewriter(filename); fw.write( output.tostring()); fw.close(); } public servletoutputstream getoutputstream() throws java.io.ioexception { return new servletoutputstream(); } public void write(int b) throws ioexception { output.write(b); } public void write(byte b[]) throws ioexception { output.write(new string(b,"gbk")); } public void write(byte b[], int off, int len) throws ioexception { output.write(new string(b, off, len)); } }; } public void writeresponse(printwriter out) { out.print(output.tochararray()); } } /** * end file filecaptureresponsewrapper.java */
希望本文所述對(duì)大家的JSP程序設(shè)計(jì)有所幫助。
相關(guān)文章
web前端超出兩行用省略號(hào)表示的實(shí)現(xiàn)方法
這篇文章主要介紹了web前端超出兩行用省略號(hào)表示的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10實(shí)現(xiàn)jsp驗(yàn)證碼的簡單小例子
實(shí)現(xiàn)jsp驗(yàn)證碼的簡單小例子,需要的朋友可以參考一下2013-04-04Spring AOP切面解決數(shù)據(jù)庫讀寫分離實(shí)例詳解
這篇文章主要介紹了Spring AOP切面解決數(shù)據(jù)庫讀寫分離實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05jsp+mysql數(shù)據(jù)庫操作常用方法實(shí)例總結(jié)
這篇文章主要介紹了jsp+mysql數(shù)據(jù)庫操作常用方法,以實(shí)例形式較為詳細(xì)的總結(jié)了JSP操作mysql數(shù)據(jù)庫實(shí)現(xiàn)基本的增、刪、改、查相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10JSP for循環(huán)中判斷點(diǎn)擊的是哪個(gè)按鈕
做留言板時(shí)遇到數(shù)據(jù)庫中for循環(huán),判斷點(diǎn)擊的是哪個(gè)按鈕的情況,下面是具體的實(shí)現(xiàn),大家可以參考下2014-04-04EJB 3.0 開發(fā)指南之定時(shí)服務(wù)
EJB 3.0 開發(fā)指南之定時(shí)服務(wù)...2006-10-10用Java實(shí)現(xiàn)FTP服務(wù)器解決方案
用Java實(shí)現(xiàn)FTP服務(wù)器解決方案...2006-10-10