时间: 2021-07-31 作者:daque
前段功夫做本人社区的乒坛,在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 */