如何将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 */  
.

posted @ 2008-04-30 00:04  springMVC3.1例子  阅读(329)  评论(0编辑  收藏  举报