本文是介紹了jsp技術實現動態頁面到靜態頁面的方法,分享給大家,具體如下:
對於JSP技術實現動態頁面到靜態頁面的方案,我們從三個步驟來說明:
JSP技術實現動態頁面到靜態頁面的方案第一:
為了能深入淺出的理解這個框架的由來,我們首先來了解一下JSP解析器將我們寫的JSP代碼轉換成的JAVA文件的內容。
下面是一個JSP文件test.jsp
﹤%@ page language=java contentType=text/html;charset=GB2312 %﹥ ﹤% out.write(﹤!--文件開始--﹥); %﹥ ﹤html﹥ ﹤head﹥ ﹤body﹥ ﹤%=輸出%﹥ ﹤/body﹥ ﹤/head﹥ ﹤/html﹥
經過Tomcat轉換出的Java文件test$jsp.java內容如下:
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import org.apache.jasper.runtime.*; public class test$jsp extends HttpJspBase { static { } public testOutRedir$jsp( ) { } private static boolean _jspx_inited = false; public final void _jspx_init() throws org.apache.jasper.runtime.JspException { } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out= null; Object page = this; String _value = null; try { if (_jspx_inited == false) { synchronized (this) { if (_jspx_inited == false) { _jspx_init(); _jspx_inited = true; } } } _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType(text/html;charset=GB2312); pageContext = _jspxFactory.getPageContext(this, request, response, , true, 8192, true); application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out= pageContext.getOut(); //為了節省篇幅,我刪除了解釋器添加的注釋 out.write(\r\n); //上一句是由於 ﹤%@ page language=java contentType=text/html;charset=GB2312 %﹥後面的換行產生的 out.write(﹤!--文件開始--﹥); out.write(\r\n﹤html﹥\r\n﹤head﹥\r\n﹤body﹥\r\n); out.print(輸出); out.write(\r\n﹤/body﹥\r\n﹤/head﹥\r\n﹤/html﹥\r\n); } catch (Throwable t) { if (out!= null &&out.getBufferSize() != 0) out.clearBuffer(); if (pageContext != null) pageContext.handlePageException(t); } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext); } } }
從上面的代碼中可以清晰的看到JSP內建的幾個對象(out、request、response、session、pageContext、application、config、page)是怎麼產生的,懂servlet的朋友一看就能明白。
下面重點理解一下out對象,它被聲明為JspWriter類型,JspWriter是一個抽象類,在包javax.servlet.jsp中可以找到它的定義。
abstractpublicclassjavax.servlet.jsp.JspWriterextends java.io.Writer{ final public static intNO_BUFFER = 0; final public static intDEFAULT_BUFFER = -1; final public static intUNBOUNDED_BUFFER = -2; protected intbufferSize; protected BooleanautoFlush; protectedjavax.servlet.jsp.JspWriter(intarg1,booleanarg2); abstractpublicvoidnewLine()throwsIOException; abstractpublicvoidprint(booleanarg0)throwsIOException; abstractpublicvoidprint(chararg0)throwsIOException; abstractpublicvoidprint(intarg0)throwsIOException; abstractpublicvoidprint(longarg0)throwsIOException; abstractpublicvoidprint(floatarg0)throwsIOException; abstractpublicvoidprint(doublearg0)throwsIOException; abstractpublicvoidprint(char[]arg0)throwsIOException; abstractpublicvoidprint(Stringarg0)throwsIOException; abstractpublicvoidprint(Objectarg0)throwsIOException; abstractpublicvoidprintln()throwsIOException; abstractpublicvoidprintln(booleanarg0)throwsIOException; abstractpublicvoidprintln(chararg0)throwsIOException; abstractpublicvoidprintln(intarg0)throwsIOException; abstractpublicvoidprintln(longarg0)throwsIOException; abstractpublicvoidprintln(floatarg0)throwsIOException; abstractpublicvoidprintln(doublearg0)throwsIOException; abstractpublicvoidprintln(char[]arg0)throwsIOException; abstractpublicvoidprintln(Stringarg0)throwsIOException; abtractpublicvoidprintln(Objectarg0)throwsIOException; abstractpublicvoidclear()throwsIOException; abstractpublicvoidclearBuffer()throwsIOException; abstractpublicvoidflush()throwsIOException; abstractpublicvoidclose()throwsIOException; publicintgetBufferSize() ; abstractpublicintgetRemaining(); publicbooleanisAutoFlush(); }
相信到這裡你可能已經知道怎麼做了。是的,來個偷天換日,繼承JspWriter類,然後實現其定義的虛函數,然後把out變量替換成你自己實現的類的實例就ok了。
JSP技術實現動態頁面到靜態頁面的方案第二:
實現替換
假設
﹤%@ page language=java contentType=text/html;charset=GB2312 import=jwb.util.HtmlIntoFile,jwb.util.TempSinglet,java.io.File%﹥ ﹤% JspWriter outout_bak =out;String arg1=argument1;String filePath = /cache/根據參數生成文件名_ + arg1 + .html; //首先判斷文件是否已經存在,如果不存在則執行本頁面,否則跳轉到靜態頁面就OK了File f = new File(pageContext.getServletContext().getRealPath(filePath)); if(f.exists()){ out_bak.clear(); pageContext.forward(filePath); System.out.println(直接轉到靜態頁面); return;}out= new HtmlIntoFile(pageContext.getServletContext().getRealPath(filePath));out.write(﹤!--文件開始--﹥); %﹥ ﹤html﹥ ﹤head﹥ ﹤body﹥ ﹤%= 看吧,這就是輸出被重定向到文件的實現,很簡單吧^_^%﹥ ﹤/body﹥ ﹤/head﹥ ﹤/html﹥ ﹤% out.close(); //關閉生成的靜態文件out_bak.clear();pageContext.forward(filePath); System.out.println(執行本頁面後再轉到靜態頁面);return; %﹥
JSP技術實現動態頁面到靜態頁面的方案第三:
更新問題
下面就討論一下如何更新生成靜態文件,其實從上面實現中你可以看到,很簡單的就是將生成的靜態文件刪除即可,至於什麼時候刪除,要看你的需求了。我能想到的幾種情況如下:
◆當用來生成頁面的數據更新時
◆如果不需要很提供時時的數據可以定時更新
◆永遠不更新
那麼通過這個JSP技術實現動態頁面到靜態頁面的方案,從動態頁面到靜態的轉變就已經告一段落,你是否有點啟發呢?感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!