J显示内容缓存技巧_JSP技巧_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

J显示内容缓存技巧

作者:黑客防线网安JSP教程基地 来源:黑客防线网安JSP教程基地 浏览次数:0

本篇关键词:技巧内容显示public
黑客防线网安网讯:  前段时间做自己社区的论坛,在jive的基础上做一个页面显示所有论坛的帖子,可以称之为总版,模仿Forum类的接口做个SuperForum并且实现Cachable,不过因为这个页面刷新量比较大,虽然被Ca...

  前段时间做自己社区的论坛在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 */ 
  

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-16435-1.html
网站维护教程更新时间:2012-04-07 00:21:34  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer