Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
JSP中捕获 OUT 输出的例子

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

在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( "开始输出, 在浏览器将会什么都不到
" );
%>



<%
??? out.println( "文件内容" );
%>




<%
out.close();
%>

 

 

 




相关文章

相关软件