在CSDN里面看了一篇关于将动态JSP内容保存为静态页面的文章,忘记网址了,大家可以搜索一下 :)
他没有提供源代码,然后自己测试着写了一个.
主要想法是捕获 out 的输出后,可以保存到一些静态文件中,可以写一个 JSP的缓冲程序.
代码有待完善, 希望有这方面经验的朋友来共同完善.
在RESIN环境中测试成功,没有在tomcat其他服务器下测试,还存在一个问题,就是不能够同时输出到IE浏览器中.
?
以下为程序代码, 例如保存到 test.jsp 文件中,然后在IE中执行
http://....../test.jsp
将看不到任何输出,但是可以在后台resin的DOS窗口中看到输出的内容
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%!
//继承 JspWriter 类
class MyOut extends JspWriter
{
private HttpServletResponse response;
//将输出语句都存入os中
public CharArrayWriter os;
public MyOut()
{
super(0, false);
os = new CharArrayWriter();
}
public String getString() {
return os.toString();
}
public final void write(byte buf[], int off, int len)
throws IOException
{
os.write( new String(buf, off, len) );
}
public final void write(char buf[], int off, int len)
throws IOException
{
os.write( new String(buf, off, len) );
}
public final void write(int ch)
throws IOException
{
os.write( String.valueOf(ch) );
}
public final void write(char buf[])
throws IOException
{
os.write( String.valueOf(buf) );
}
public final void write(String s)
throws IOException
{
os.write( s );
}
public final void write(String s, int off, int len)
throws IOException
{
os.write( s, off, len );
}
public final void newLine()
throws IOException
{
os.write( "\n\r" );
}
public final void print(boolean b)
throws IOException
{
os.write( String.valueOf(b) );
}
public final void print(char ch)
throws IOException
{
os.write( String.valueOf(ch) );
}
public final void print(int i)
throws IOException
{
os.write( String.valueOf(i) );
}
public final void print(long l)
throws IOException
{
os.write( String.valueOf(l) );
}
public final void print(float f)
throws IOException
{
os.write( String.valueOf(f) );
}
public final void print(double d)
throws IOException
{
os.write( String.valueOf(d) );
}
public final void print(char s[])
throws IOException
{
os.write( String.valueOf(s) );
}
public final void print(String s)
throws IOException
{
os.write( s );
}
public final void print(Object o)
throws IOException
{
os.write( String.valueOf(o) );
}
public final void println()
throws IOException
{
os.write( "\n\r" );
}
public final void println(boolean b)
throws IOException
{
os.write( String.valueOf(b) );
}
public final void println(char ch)
throws IOException
{
os.write( String.valueOf(ch) );
}
public final void println(int i)
throws IOException
{
os.write( String.valueOf(i) );
}
public final void println(long l)
throws IOException
{
os.write( String.valueOf(l) );
}
public final void println(float f)
throws IOException
{
os.write( String.valueOf(f) );
}
public final void println(double d)
throws IOException
{
os.write( String.valueOf(d) );
}
public final void println(char s[])
throws IOException
{
os.write(s, 0, s.length);
}
public final void println(String s)
throws IOException
{
os.write(s);
}
public final void println(Object o)
throws IOException
{
os.write( String.valueOf(o) );
}
public final void clear()
throws IOException
{
os.reset();
}
public final void flush()
throws IOException
{
os.flush();
}
public void clearBuffer() {
os.reset();
}
public ServletOutputStream getOutputStream()
throws java.io.IOException
{
return response.getOutputStream();
}
//执行该方法可以将内容输出,或者保存为文件
public final void close()
throws IOException
{
System.out.println( "以下为静态输出" );
System.out.println( os.toString() );
}
public final int getBufferSize()
{
if(bufferSize == 0)
return bufferSize;
else
return response.getBufferSize();
}
public final int getRemaining()
{
return os.size();
}
public final boolean isAutoFlush()
{
return autoFlush;
}
}
%>
<%
out = new MyOut();
out.println( "开始输出, 在浏览器将会什么都不到<BR>" );
%>
<html>
<head>
<body>
<%
out.println( "文件内容" );
%>
</body>
</head>
</html>
<%
out.close();
%>
本文地址:http://com.8s8s.com/it/it15764.htm