国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jsp實現將動態網頁轉換成靜態頁面的方法

2020-07-27 21:30:10
字體:
來源:轉載
供稿:網友

本文實例講述了jsp實現將動態網頁轉換成靜態頁面的方法。分享給大家供大家參考。具體如下:

如果我可以將jsp動態網頁轉換成靜態頁面,那么訪問的時候就不需要頻繁的訪問數據庫了。

jsp 顯示內容緩存技巧

前段時間做自己社區的論壇,在jive 的基礎上做一個頁面顯示所有論壇的帖子,可以稱之為總版,模仿forum 類的接口做個superforum 并且實現cachable,不過因為這個頁面刷新量比較大,雖然被cache 了,我還是想辦法進行頁面的緩存,感覺用jsp 產生的html靜態內容當緩存,頁面訪問速度應該有所提高。

首先想到的一種辦法,是采用java.net 的urlconnection 把服務器上的jsp 抓過來做緩存,不過我覺得這樣做太見外了,自己服務器上的東西,為何要用http 去訪問.于是想另外一個辦法,把jsp 的out 對象的輸出控制到自己希望的地方.比如輸出到靜態文件,又或者保存成全局的字符串變量.這樣的話,瀏覽就不需要執行jsp,只是瀏覽該html 了.僅僅在數據有更新的時候進行一次update 操作,把jsp 重新輸出為html.

我覺得,瀏覽事件比數據插入或更新發生的次數多的時候.不妨試試這個辦法來提高頁面訪問速度.

整件事情有點像把jsp 當作模板,生成靜態的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 頁面結果字符串 // responsewrapper.writefile(filename); // dump the contents 寫成html 文件,也可以保存在內存 //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 服務器的話,以上代碼會失效。因為resin 沒有實現getwriter 方法,而是采用getoutputstream 取而代之,所以必須修改些代碼來迎合resin 運行環境:

/** * 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 */

希望本文所述對大家的JSP程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台北市| 南昌县| 双鸭山市| 牟定县| 崇仁县| 宁陵县| 定边县| 新沂市| 阿坝| 仁寿县| 屏边| 曲麻莱县| 宁都县| 苍梧县| 周宁县| 宁波市| 沧州市| 拜城县| 余庆县| 吕梁市| 云南省| 靖州| 老河口市| 河北区| 车险| 宜都市| 临夏县| 徐水县| 磴口县| 利辛县| 大厂| 阳西县| 南昌县| 巴里| 桑日县| 兴安县| 兴安县| 遵义市| 丹阳市| 桂阳县| 衡山县|