注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
Win XP中震荡波后应采取的措施
用MailSpy拦截局域网内危险的病毒
Win XP SP2拖放IE窗口可能引发黑
防范ASP木马的十大基本原则
杀毒软件如何被XP SP2的安全中心
服务器如何防范asp木马
杜绝入侵:八大法则防范ASP网站漏
黑客知识 巧妙配合asp木马取得管
实例讲解:全程追踪入侵JSP网站服
浅谈除不尽理还乱的Spyware间谍软
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Java >> 文章正文
JSP中捕获 OUT 输出的例子
作者:佚名  来源:不详  点击:  更新:2006-12-19
简介:
 

在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>
&

[1] [2] 下一页






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务