本篇內(nèi)容介紹了“JSP技術(shù)如何實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面到靜態(tài)頁(yè)面”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)建站網(wǎng)絡(luò)公司擁有10年的成都網(wǎng)站開(kāi)發(fā)建設(shè)經(jīng)驗(yàn),近1000家客戶的共同信賴。提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站開(kāi)發(fā)、網(wǎng)站定制、買(mǎi)友情鏈接、建網(wǎng)站、網(wǎng)站搭建、響應(yīng)式網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)師打造企業(yè)風(fēng)格,提供周到的售前咨詢和貼心的售后服務(wù)
對(duì)于JSP技術(shù)實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面到靜態(tài)頁(yè)面的方案,我們從三個(gè)步驟來(lái)說(shuō)明:
JSP技術(shù)實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面到靜態(tài)頁(yè)面的方案***:
為了能深入淺出的理解這個(gè)框架的由來(lái),我們首先來(lái)了解一下JSP解析器將我們寫(xiě)的JSP代碼轉(zhuǎn)換成的JAVA文件的內(nèi)容。
下面是一個(gè)JSP文件test.jsp
﹤%@ page language=java contentType=text/html;charset=GB2312 %﹥ ﹤% out.write(﹤!--文件開(kāi)始--﹥); %﹥ ﹤html﹥ ﹤head﹥ ﹤body﹥ ﹤%=輸出%﹥ ﹤/body﹥ ﹤/head﹥ ﹤/html﹥
經(jīng)過(guò)Tomcat轉(zhuǎn)換出的Java文件test$jsp.java內(nèi)容如下:
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(); //為了節(jié)省篇幅,我刪除了解釋器添加的注釋 out.write(\r\n); //上一句是由于 ﹤%@ page language=java contentType=text/html;charset=GB2312 %﹥后面的換行產(chǎn)生的 out.write(﹤!--文件開(kāi)始--﹥); 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內(nèi)建的幾個(gè)對(duì)象(out、request、response、session、pageContext、application、config、page)是怎么產(chǎn)生的,懂servlet的朋友一看就能明白。
下面重點(diǎn)理解一下out對(duì)象,它被聲明為JspWriter類(lèi)型,JspWriter是一個(gè)抽象類(lèi),在包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(); }
相信到這里你可能已經(jīng)知道怎么做了。是的,來(lái)個(gè)偷天換日,繼承JspWriter類(lèi),然后實(shí)現(xiàn)其定義的虛函數(shù),然后把out變量替換成你自己實(shí)現(xiàn)的類(lèi)的實(shí)例就ok了。
JSP技術(shù)實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面到靜態(tài)頁(yè)面的方案第二:
實(shí)現(xiàn)替換
假設(shè)
﹤%@ 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/根據(jù)參數(shù)生成文件名_ + arg1 + .html; //首先判斷文件是否已經(jīng)存在,如果不存在則執(zhí)行本頁(yè)面,否則跳轉(zhuǎn)到靜態(tài)頁(yè)面就OK了File f = new File(pageContext.getServletContext().getRealPath(filePath)); if(f.exists()){ out_bak.clear(); pageContext.forward(filePath); System.out.println(直接轉(zhuǎn)到靜態(tài)頁(yè)面); return;}out= new HtmlIntoFile(pageContext.getServletContext().getRealPath(filePath));out.write(﹤!--文件開(kāi)始--﹥); %﹥ ﹤html﹥ ﹤head﹥ ﹤body﹥ ﹤%= 看吧,這就是輸出被重定向到文件的實(shí)現(xiàn),很簡(jiǎn)單吧^_^%﹥ ﹤/body﹥ ﹤/head﹥ ﹤/html﹥ ﹤% out.close(); //關(guān)閉生成的靜態(tài)文件out_bak.clear();pageContext.forward(filePath); System.out.println(執(zhí)行本頁(yè)面后再轉(zhuǎn)到靜態(tài)頁(yè)面);return; %﹥
JSP技術(shù)實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面到靜態(tài)頁(yè)面的方案第三:
更新問(wèn)題
下面就討論一下如何更新生成靜態(tài)文件,其實(shí)從上面實(shí)現(xiàn)中你可以看到,很簡(jiǎn)單的就是將生成的靜態(tài)文件刪除即可,至于什么時(shí)候刪除,要看你的需求了。我能想到的幾種情況如下
◆當(dāng)用來(lái)生成頁(yè)面的數(shù)據(jù)更新時(shí)
◆如果不需要很提供時(shí)時(shí)的數(shù)據(jù)可以定時(shí)更新
◆永遠(yuǎn)不更新
“JSP技術(shù)如何實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面到靜態(tài)頁(yè)面”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!