首页
老网站
正文
JSP显示内容缓存技巧
前段时间做自己社区的论坛,在jive的基础上做一个页面显示所有论坛的帖子,可以称之为总版,模仿Forum类的接口做个SuperForum并且实现Cachable,不过因为这个页面刷新量比较大,虽然被Cache了,我还是想办法进行页面的缓存,感觉用jsp产生的html静态内容当缓存,页面访问速度应该有所提高。
首先想到的一种办法,是采用java.net的URLConnection把服务器上的jsp抓过来做缓存,不过我觉得这样做太见外了,自己服务器上的东西,为何要用HTTP去访问.于是想另外一个办法,把jsp的out对象的输出控制到自己希望的地方.比如输出到静态文件,又或者保存成全局的字符串变量.这样的话,浏览就不需要执行jsp,只是浏览该html了.仅仅在数据有更新的时候进行一次update操作,把jsp重新输出为html.
我觉得,浏览事件比数据插入或更新发生的次数多的时候.不妨试试这个办法来提高页面访问速度.
整件事情有点像把jsp当作模板,生成静态的html页面.
{$ad$}
将如下代码写入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 */
SELECT [t0].[a_id], [t0].[a_site], [t0].[a_model], [t0].[a_type], [t0].[a_special], [t0].[a_name], [t0].[a_from], [t0].[a_from_url], [t0].[a_author], [t0].[a_editor], [t0].[a_describe], [t0].[a_content], [t0].[a_create_date], [t0].[a_tag], [t0].[a_hits], [t0].[a_commend], [t0].[a_istop], [t0].[a_isimg], [t0].[a_cacheimg], [t0].[a_imgurls], [t0].[a_imgurl], [t0].[a_ismedia], [t0].[a_mediaurl], [t0].[a_iscomment], [t0].[a_isRecycle], [t0].[a_weight], [t0].[a_weights], [t0].[a_status], [t0].[a_url], [t0].[a_file_path], [t0].[a_contentfile], [t0].[a_comment], [t0].[a_ispass], [t0].[a_bury], [t0].[a_dig], [t0].[a_score], [t0].[a_rank], [t0].[a_diguser], [t0].[a_buryuser], [t0].[a_viewip], [t0].[a_lastviewdate], [t0].[a_neworimageorvideo], [t0].[class_id], [t0].[class_type], [t0].[class_name], [t0].[class_show_name], [t0].[class_parent_id], [t0].[class_id_path], [t0].[class_name_path], [t0].[class_show_name_path], [t0].[class_depth], [t0].[class_order], [t0].[class_content], [t0].[class_img], [t0].[a_model_name]
FROM [dbo].[mvc_content_view] AS [t0]
WHERE ([t0].[a_id] = @p0) AND ([t0].[a_type] = @p1)
-- @p0: Input BigInt (Size = 0; Prec = 0; Scale = 0) [3375]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [236]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
SELECT TOP (1) [t0].[a_id], [t0].[a_site], [t0].[a_model], [t0].[a_type], [t0].[a_special], [t0].[a_name], [t0].[a_from], [t0].[a_from_url], [t0].[a_author], [t0].[a_editor], [t0].[a_describe], [t0].[a_content], [t0].[a_create_date], [t0].[a_tag], [t0].[a_hits], [t0].[a_commend], [t0].[a_istop], [t0].[a_isimg], [t0].[a_cacheimg], [t0].[a_imgurls], [t0].[a_imgurl], [t0].[a_ismedia], [t0].[a_mediaurl], [t0].[a_iscomment], [t0].[a_isRecycle], [t0].[a_weight], [t0].[a_weights], [t0].[a_status], [t0].[a_url], [t0].[a_file_path], [t0].[a_contentfile], [t0].[a_comment], [t0].[a_ispass], [t0].[a_bury], [t0].[a_dig], [t0].[a_score], [t0].[a_rank], [t0].[a_diguser], [t0].[a_buryuser], [t0].[a_viewip], [t0].[a_lastviewdate], [t0].[a_neworimageorvideo], [t0].[class_id], [t0].[class_type], [t0].[class_name], [t0].[class_show_name], [t0].[class_parent_id], [t0].[class_id_path], [t0].[class_name_path], [t0].[class_show_name_path], [t0].[class_depth], [t0].[class_order], [t0].[class_content], [t0].[class_img], [t0].[a_model_name]
FROM [dbo].[mvc_content_view] AS [t0]
WHERE ([t0].[a_type] = @p0) AND ([t0].[a_id] < @p1)
ORDER BY [t0].[a_id] DESC
-- @p0: Input BigInt (Size = 0; Prec = 0; Scale = 0) [236]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [3375]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
SELECT TOP (1) [t0].[a_id], [t0].[a_site], [t0].[a_model], [t0].[a_type], [t0].[a_special], [t0].[a_name], [t0].[a_from], [t0].[a_from_url], [t0].[a_author], [t0].[a_editor], [t0].[a_describe], [t0].[a_content], [t0].[a_create_date], [t0].[a_tag], [t0].[a_hits], [t0].[a_commend], [t0].[a_istop], [t0].[a_isimg], [t0].[a_cacheimg], [t0].[a_imgurls], [t0].[a_imgurl], [t0].[a_ismedia], [t0].[a_mediaurl], [t0].[a_iscomment], [t0].[a_isRecycle], [t0].[a_weight], [t0].[a_weights], [t0].[a_status], [t0].[a_url], [t0].[a_file_path], [t0].[a_contentfile], [t0].[a_comment], [t0].[a_ispass], [t0].[a_bury], [t0].[a_dig], [t0].[a_score], [t0].[a_rank], [t0].[a_diguser], [t0].[a_buryuser], [t0].[a_viewip], [t0].[a_lastviewdate], [t0].[a_neworimageorvideo], [t0].[class_id], [t0].[class_type], [t0].[class_name], [t0].[class_show_name], [t0].[class_parent_id], [t0].[class_id_path], [t0].[class_name_path], [t0].[class_show_name_path], [t0].[class_depth], [t0].[class_order], [t0].[class_content], [t0].[class_img], [t0].[a_model_name]
FROM [dbo].[mvc_content_view] AS [t0]
WHERE ([t0].[a_type] = @p0) AND ([t0].[a_id] > @p1)
ORDER BY [t0].[a_id]
-- @p0: Input BigInt (Size = 0; Prec = 0; Scale = 0) [236]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [3375]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
SELECT [t0].[a_id], [t0].[a_site], [t0].[a_model], [t0].[a_type], [t0].[a_special], [t0].[a_name], [t0].[a_from], [t0].[a_from_url], [t0].[a_author], [t0].[a_editor], [t0].[a_describe], [t0].[a_content], [t0].[a_create_date], [t0].[a_tag], [t0].[a_hits], [t0].[a_commend], [t0].[a_istop], [t0].[a_isimg], [t0].[a_cacheimg], [t0].[a_imgurls], [t0].[a_imgurl], [t0].[a_ismedia], [t0].[a_mediaurl], [t0].[a_iscomment], [t0].[a_isRecycle], [t0].[a_weight], [t0].[a_weights], [t0].[a_status], [t0].[a_url], [t0].[a_file_path], [t0].[a_contentfile], [t0].[a_comment], [t0].[a_ispass], [t0].[a_bury], [t0].[a_dig], [t0].[a_score], [t0].[a_rank], [t0].[a_diguser], [t0].[a_buryuser], [t0].[a_viewip], [t0].[a_lastviewdate], [t0].[a_neworimageorvideo]
FROM [dbo].[mvc_content] AS [t0]
WHERE ([t0].[a_id] = @p0) AND ([t0].[a_type] = @p1)
-- @p0: Input BigInt (Size = 0; Prec = 0; Scale = 0) [3375]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [236]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
UPDATE [dbo].[mvc_content]
SET [a_hits] = @p19
WHERE ([a_id] = @p0) AND ([a_site] = @p1) AND ([a_model] = @p2) AND ([a_type] = @p3) AND ([a_special] = @p4) AND ([a_name] = @p5) AND ([a_from] = @p6) AND ([a_from_url] IS NULL) AND ([a_author] IS NULL) AND ([a_editor] IS NULL) AND ([a_describe] IS NULL) AND ([a_content] = @p7) AND ([a_create_date] = @p8) AND ([a_tag] = @p9) AND ([a_hits] = @p10) AND (NOT ([a_commend] = 1)) AND (NOT ([a_istop] = 1)) AND (NOT ([a_isimg] = 1)) AND ([a_cacheimg] IS NULL) AND ([a_imgurls] IS NULL) AND ([a_imgurl] = @p11) AND (NOT ([a_ismedia] = 1)) AND ([a_mediaurl] IS NULL) AND (NOT ([a_iscomment] = 1)) AND (NOT ([a_isRecycle] = 1)) AND ([a_weight] = @p12) AND ([a_status] = @p13) AND ([a_url] IS NULL) AND ([a_file_path] IS NULL) AND ([a_contentfile] IS NULL) AND ([a_comment] = @p14) AND ([a_ispass] = @p15) AND ([a_bury] = @p16) AND ([a_dig] = @p17) AND ([a_score] = @p18) AND ([a_diguser] IS NULL) AND ([a_buryuser] IS NULL) AND ([a_viewip] IS NULL) AND ([a_lastviewdate] IS NULL) AND ([a_neworimageorvideo] IS NULL)
SELECT [t1].[a_weights], [t1].[a_rank]
FROM [dbo].[mvc_content] AS [t1]
WHERE ((@@ROWCOUNT) > 0) AND ([t1].[a_id] = @p20)
-- @p0: Input BigInt (Size = 0; Prec = 0; Scale = 0) [3375]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [197]
-- @p2: Input BigInt (Size = 0; Prec = 0; Scale = 0) [24]
-- @p3: Input BigInt (Size = 0; Prec = 0; Scale = 0) [236]
-- @p4: Input BigInt (Size = 0; Prec = 0; Scale = 0) [0]
-- @p5: Input NVarChar (Size = 11; Prec = 0; Scale = 0) [JSP显示内容缓存技巧]
-- @p6: Input NVarChar (Size = 3; Prec = 0; Scale = 0) [174]
-- @p7: Input NVarChar (Size = 9754; Prec = 0; Scale = 0) [前段时间做自己社区的论坛,在jive的基础上做一个页面显示所有论坛的帖子,可以称之为总版,模仿Forum类的接口做个SuperForum并且实现Cachable,不过因为这个页面刷新量比较大,虽然被Cache了,我还是想办法进行页面的缓存,感觉用jsp产生的html静态内容当缓存,页面访问速度应该有所提高。
首先想到的一种办法,是采用java.net的URLConnection把服务器上的jsp抓过来做缓存,不过我觉得这样做太见外了,自己服务器上的东西,为何要用HTTP去访问.于是想另外一个办法,把jsp的out对象的输出控制到自己希望的地方.比如输出到静态文件,又或者保存成全局的字符串变量.这样的话,浏览就不需要执行jsp,只是浏览该html了.仅仅在数据有更新的时候进行一次update操作,把jsp重新输出为html.
我觉得,浏览事件比数据插入或更新发生的次数多的时候.不妨试试这个办法来提高页面访问速度.
整件事情有点像把jsp当作模板,生成静态的html页面.
{$ad$}
将如下代码写入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 */
]
-- @p8: Input DateTime (Size = 0; Prec = 0; Scale = 0) [2007-9-9 12:48:34]
-- @p9: Input NVarChar (Size = 15; Prec = 0; Scale = 0) [JSP,显示,内容,缓存,技巧]
-- @p10: Input BigInt (Size = 0; Prec = 0; Scale = 0) [2731]
-- @p11: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p12: Input BigInt (Size = 0; Prec = 0; Scale = 0) [0]
-- @p13: Input Int (Size = 0; Prec = 0; Scale = 0) [0]
-- @p14: Input BigInt (Size = 0; Prec = 0; Scale = 0) [0]
-- @p15: Input Int (Size = 0; Prec = 0; Scale = 0) [0]
-- @p16: Input BigInt (Size = 0; Prec = 0; Scale = 0) [0]
-- @p17: Input BigInt (Size = 0; Prec = 0; Scale = 0) [0]
-- @p18: Input Float (Size = 0; Prec = 0; Scale = 0) [0]
-- @p19: Input BigInt (Size = 0; Prec = 0; Scale = 0) [2732]
-- @p20: Input BigInt (Size = 0; Prec = 0; Scale = 0) [3375]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1