JSP顯示內(nèi)容緩存技巧
發(fā)表時(shí)間:2024-01-18 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]前段時(shí)間做自己社區(qū)的論壇,在jive的基礎(chǔ)上做一個(gè)頁(yè)面顯示所有論壇的帖子,可以稱(chēng)之為總版,模仿Forum類(lèi)的接口做個(gè)SuperForum并且實(shí)現(xiàn)Cachable,不過(guò)因?yàn)檫@個(gè)頁(yè)面刷新量比較大,雖然被Cache了,我還是想辦法進(jìn)行頁(yè)面的緩存,感覺(jué)用jsp產(chǎn)生的html靜態(tài)內(nèi)容當(dāng)緩存,頁(yè)面訪(fǎng)問(wèn)速度應(yīng)該...
前段時(shí)間做自己社區(qū)的論壇,在jive的基礎(chǔ)上做一個(gè)頁(yè)面顯示所有論壇的帖子,可以稱(chēng)之為總版,模仿Forum類(lèi)的接口做個(gè)SuperForum并且實(shí)現(xiàn)Cachable,不過(guò)因?yàn)檫@個(gè)頁(yè)面刷新量比較大,雖然被Cache了,我還是想辦法進(jìn)行頁(yè)面的緩存,感覺(jué)用jsp產(chǎn)生的html靜態(tài)內(nèi)容當(dāng)緩存,頁(yè)面訪(fǎng)問(wèn)速度應(yīng)該有所提高。
首先想到的一種辦法,是采用java.net的URLConnection把服務(wù)器上的jsp抓過(guò)來(lái)做緩存,不過(guò)我覺(jué)得這樣做太見(jiàn)外了,自己服務(wù)器上的東西,為何要用HTTP去訪(fǎng)問(wèn).于是想另外一個(gè)辦法,把jsp的out對(duì)象的輸出控制到自己希望的地方.比如輸出到靜態(tài)文件,又或者保存成全局的字符串變量.這樣的話(huà),瀏覽就不需要執(zhí)行jsp,只是瀏覽該html了.僅僅在數(shù)據(jù)有更新的時(shí)候進(jìn)行一次update操作,把jsp重新輸出為html.
我覺(jué)得,瀏覽事件比數(shù)據(jù)插入或更新發(fā)生的次數(shù)多的時(shí)候.不妨試試這個(gè)辦法來(lái)提高頁(yè)面訪(fǎng)問(wèn)速度.
整件事情有點(diǎn)像把jsp當(dāng)作模板,生成靜態(tài)的html頁(yè)面.
將如下代碼寫(xiě)入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的頁(yè)面
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頁(yè)面結(jié)果字符串
// responseWrapper.writeFile(fileName);
// dump the contents 寫(xiě)成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 */
附件源代碼
不過(guò)采用resin服務(wù)器的話(huà),以上代碼會(huì)失效。因?yàn)閞esin沒(méi)有實(shí)現(xiàn)getWriter方法,而是采用getOutputStream取而代之,所以必須修改些代碼來(lái)迎合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 */