服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > J2EE > Servlet/Jsp > 查看文档

jsp中捕获 out 输出的例子

  在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();
  %>


  





扫描关注微信公众号