JSP显示内容缓存技巧
2008-02-23 07:56:13来源:互联网 阅读 ()
我觉得,浏览事件比数据插入或更新发生的次数多的时候.不妨试试这个办法来提高页面访问速度.
整件事情有点像把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 */
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash