一個在JSP下實現將動態網頁面轉為靜態方案

來源:互聯網
上載者:User
js|動態|靜態|頁面

  1.前言

  為了能深入淺出的理解這個架構的由來,我們首先來瞭解一下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中可以找到它的定義。

abstract public class javax.servlet.jsp.JspWriter extends java.io.Writer{
       final public static int NO_BUFFER = 0;
       final public static int DEFAULT_BUFFER = -1;
       final public static int UNBOUNDED_BUFFER = -2;
       protected int bufferSize;
       protected Boolean autoFlush;
       protected javax.servlet.jsp.JspWriter(int arg1, boolean arg2);
    abstract public void newLine() throws IOException ;
    abstract public void print(boolean arg0) throws IOException ;
    abstract public void print(char arg0) throws IOException ;
    abstract public void print(int arg0) throws IOException ;
    abstract public void print(long arg0) throws IOException ;
    abstract public void print(float arg0) throws IOException ;
    abstract public void print(double arg0) throws IOException ;
    abstract public void print(char<> arg0) throws IOException ;
    abstract public void print(String arg0) throws IOException ;
    abstract public void print(Object arg0) throws IOException ;
    abstract public void println() throws IOException ;
    abstract public void println(boolean arg0) throws IOException ;
    abstract public void println(char arg0) throws IOException ;
    abstract public void println(int arg0) throws IOException ;
    abstract public void println(long arg0) throws IOException ;
    abstract public void println(float arg0) throws IOException ;
    abstract public void println(double arg0) throws IOException ;
    abstract public void println(char<> arg0) throws IOException ;
    abstract public void println(String arg0) throws IOException ;
    abtract public void println(Object arg0) throws IOException ;
    abstract public void clear() throws IOException ;
    abstract public void clearBuffer() throws IOException ;
    abstract public void flush() throws IOException ;
    abstract public void close() throws IOException ;
    public int getBufferSize() ;
    abstract public int getRemaining() ;
    public boolean isAutoFlush() ;
}

  我相信當我寫到這裡你可能已經知道我想怎麼做了。是的,來個偷天換日,繼承JspWriter類,然後實現其定義的虛函數,然後把out變數替換成你自己實現的類的執行個體就ok了。

  2.實現替換

  假設:

<%@ page language="java"  contentType="text/html;charset=GB2312"
import="jwb.util.HtmlIntoFile,jwb.util.TempSinglet,java.io.File"%><%
JspWriter out_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;
%>

  3.更新問題

  下面就討論一下如何更新產生靜態檔案,其實從上面實現中你可以看到,很簡單的就是將產生的靜態檔案刪除即可,至於什麼時候刪除,要看你的需求了。我能想到的幾種情況如下:

  當用來產生頁面的資料更新時
  如果不需要很提供時時的資料可以定時更新
  永遠不更新



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.