JSP顯示內容緩衝技巧

來源:互聯網
上載者:User
js|緩衝|技巧|顯示


  前段時間做自己社區的論壇,在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 */ 





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.