利用Java生成靜態HMTL頁面的方法收集

利用Java生成靜態HMTL頁面的方法收集

生成靜態頁面技術解決方案之一

轉載者前言:這是一個全面的jsp動態頁面靜態化方案,本站的帖子靜態化方案將借鑑這篇帖子中方法。向[url]http://www.agilejava.org[/url]的single的共享精神致敬。

轉帖正文:

       相信很多人都希望自己的頁面越快越好,最好是能靜態的,提高客戶訪問速度。也便於搜索引擎搜索。所以,就希望我們的動態讀取數據庫的頁面,儘可能的生成靜態頁面。一下系列文章,介紹一下個人的解決方案。

      本系列將介紹個人的一種方法,在不改變原來jsp文件的基礎上,只需要加入少量的代碼,就讓你的新聞發佈系統,很容易就完全變成靜態的頁面。

            本文假設你是用java開發的web動態頁面。

           第一步,加入servlet.代碼如下。


      public class ToHtml extends HttpServlet {

       public void service(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
        String url = "";
        String name = "";

        ServletContext sc = getServletContext();

        String file_name = request.getParameter("file_name");// 你要訪問的jsp文件名,如index,不包括擴展名

        // 則你訪問這個servlet時加參數.如[url]http://localhost/test/toHtml?file_name=index[/url]

        url = "/" + file_name + ".jsf";// 你要生成的頁面的文件名。我的擴展名爲jsf .

        name = ConfConstants.CONTEXT_PATH+"\\"+ file_name + ".htm";// 這是生成的html文件名,如index.htm.文件名字與源文件名相同。擴展名爲htm

      //ConfConstants.CONTEXT_PATH爲你的應用的上下文路徑。

        RequestDispatcher rd = sc.getRequestDispatcher(url);

        final ByteArrayOutputStream ōs = new ByteArrayOutputStream();

        final ServletOutputStream stream = new ServletOutputStream() {
         public void write(byte[] data, int offset, int length) {
          os.write(data, offset, length);
         }

         public void write(int b) throws IOException {
          os.write(b);
         }
        };

        final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));

        HttpServletResponse rep = new HttpServletResponseWrapper(response) {
         public ServletOutputStream getOutputStream() {
          return stream;
         }

         public PrintWriter getWriter() {
          return pw;
         }
        };
        rd.include(request, rep);
        pw.flush();
        FileOutputStream fos = new FileOutputStream(name); // 把jsp輸出的內容寫到xxx.htm
        os.writeTo(fos);
        fos.close();
        PrintWriter ōut = response.getWriter();
        out
          .print("<p align=center><font size=3 color=red>頁面已經成功生成!single<br>[url]http://www.agilejava.org/space/?[/url] 233</font></p>");
       }
      }

      第二步、配置你的web.xml

       <servlet>
        <servlet-name>toHtml</servlet-name>
        <servlet-class>mj.util.html.ToHtml</servlet-class>//你的servlet的類。
       </servlet>
       <servlet-mapping>
        <servlet-name>toHtml</servlet-name>
        <url-pattern>/toHtml</url-pattern>
       </servlet-mapping>

      第三步、運行servlet。如:[url]http://localhost:8080/test/toHtml?file_name=index[/url]

      

      OK,這就在你的test項目的根目錄下,生成了一個index.htm的靜態文件。 

      

      侷限性:本文只能生成一個文件!訪問一次,生成一個文件。並且生成的文件名也與原來的文件名相同。

      比較適合主頁生成靜態頁面。

      

      本系列的後續文章將解決更多的問題。使之在新聞發佈系統中,很容易就集成應用。

----------------------------------------------------------------------------------------------------

生成靜態頁面技術解決方案之二



            注意:轉貼本文,請加上本文鏈接[url]http://www.agilejava.org/space/?233/action_viewspace_itemid_21.html [/url]

           在上一篇文章中,生成靜態頁面,是有一定的侷限性的。生成主頁是很方便,但要生成二級頁面,就不方便了。

             本文假設一個新聞發佈系統。希望後臺發佈的,前臺顯示的是靜態的文檔。這就涉及,主頁要是靜態的,同時二級列表也是靜態的,新聞內容也是靜態的。也就是 說, 在發佈一篇新聞的時候,可能涉及到三個地方生成靜態文檔。並且,要生成一個網頁,必須訪問一個servlet。在大量生成靜態網頁的時候,

            以下方法,可以解決這些問題。

          一、加入一下servelet

          /**
           * @file_name 文件名及文件之後的參數.最好爲a.jsf?fileId=aaaa
           * @path 文件所在的路徑.相對於根目錄而言的.
           * @realName文件要保存的名字
           * @realPath文件要保存的真實路徑。默認與文件所在的目錄相同。
           */
          public class ToHtmlPath extends HttpServlet {

           public void service(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
            String url = "";
            String name = "";

            ServletContext sc = getServletContext();

            String file_name = request.getParameter("file_name");// 你要訪問的jsp文件,如news.jsf。
            // file_name如:fileDetail.jsf?fileId=56.要是有參數, 只有一個參數。並且以參數名作爲文件名。
            String realName = request.getParameter("realName");// 要保存的文件名。如aaa;注意可以沒有這個參數。

            String path = request.getParameter("path");// 你要訪問的jsp文件路徑。如news。注意可以沒有這個參數。

            String realPath = request.getParameter("realPath");// 你要保存的文件路徑,如htmlNews.注意可以沒有這個參數。
            // 下面確定要保存的文件名字。
            if (realName == null || realName == "") {
             int a = 0;
             a = file_name.indexOf("=") + 1;
             realName = file_name.substring(a);
             if (realName.indexOf(".")>0) {
              realName = file_name.substring(0, file_name.indexOf("."));
             }
            }
            // 下面構造要訪問的頁面。
            if (path == null || path == "") {
             url = "/" + file_name;// 這是你要生成HTML的jsp文件,如
            } else {
             url = "/" + path + "/" + file_name;// 這是你要生成HTML的jsp文件,如
            }
            // 下面構造要保存的文件名,及路徑。
            // 1、如果有realPath,則保存在realPath下。
            // 2、如果有path則保存在path下。
            // 3、否則,保存在根目錄下。
            if (realPath == null || realPath == "") {
             if (path == null || path == "") {
              name = ConfConstants.CONTEXT_PATH + "\\" + realName + ".htm";// 這是生成的html文件名,如index.htm.說明: ConfConstants.CONTEXT_PATH爲你的上下文路徑。
             } else {
              name = ConfConstants.CONTEXT_PATH + "\\" + path + "\\"
                + realName + ".htm";// 這是生成的html文件名,如index.htm.
             }
            } else {
             name = ConfConstants.CONTEXT_PATH + "\\" + realPath + "\\"
               + realName + ".htm";// 這是生成的html文件名,如index.htm.
            }

            // 訪問請求的頁面,並生成指定的文件。
            RequestDispatcher rd = sc.getRequestDispatcher(url);

            final ByteArrayOutputStream ōs = new ByteArrayOutputStream();

            final ServletOutputStream stream = new ServletOutputStream() {
             public void write(byte[] data, int offset, int length) {
              os.write(data, offset, length);
             }

             public void write(int b) throws IOException {
              os.write(b);
             }
            };

            final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));

            HttpServletResponse rep = new HttpServletResponseWrapper(response) {
             public ServletOutputStream getOutputStream() {
              return stream;
             }

             public PrintWriter getWriter() {
              return pw;
             }
            };
            rd.include(request, rep);
            pw.flush();
            FileOutputStream fos = new FileOutputStream(name); // 把jsp輸出的內容寫到xxx.htm
            os.writeTo(fos);
            fos.close();
            PrintWriter ōut = response.getWriter();
            out.print("<p align=center><font size=3 color=red>success!</font></p>");
           }
          }

          

          二、在web.xml裏面配置你的servlet

           <servlet>
            <servlet-name>toHtmlPath</servlet-name>
            <servlet-class>mj.util.html.ToHtmlPath</servlet-class>
           </servlet>
           <servlet-mapping>
            <servlet-name>toHtmlPath</servlet-name>
            <url-pattern>/toHtmlPath</url-pattern>
           </servlet-mapping>

          三、寫一個通用的方法, 供調用。

          public class CallHtml {

           public static void callOnePage(String fileName, String path,
             String realName, String realPath) {
            try {
             String str = "http://localhost:8080/test/toHtmlPath?file_name="
               + fileName + "&&path=" + path + "&&realName=" + realName
               + "&&realPath=" + realPath;
             int httpResult;
             URL url = new URL(str);
             URLConnection connection = url.openConnection();
             connection.connect();
             HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
             httpResult = httpURLConnection.getResponseCode();
             if (httpResult != HttpURLConnection.HTTP_OK) {
              System.out.println("沒有連接成功");
             } else {
              System.out.println("連接成功了 ");
             }
            } catch (Exception e) {
             // TODO: handle exception
            }
           }

          //這個方法適當重載,就可以省去一些參數傳遞。

          }

          四、在你的新聞發佈save時,調用方法。

          1、CallHtml.callOnePage("info.jsf?file_id=aaa",news,"", "");//將在news目錄下生成一個aaa.htm的靜態文件

          2、CallHtml.callOnePage("newsList.jsf",news,"", "");//將在news目錄下生成一個newsList.htm的靜態文件,顯示最新的新聞。

          3、CallHtml.callOnePage("index.jsf","","", "");//生成主頁。

          好了,這就保持了,主頁、列表、新聞內容都是最新的靜態頁面了。


----------------------------------------------------------------------------------------------------

一個實現將動態頁面轉爲靜態的方案

1.前言
爲了能深入淺出的理解這個框架的由來,我們首先來了解一下JSP解析器將我們寫的JSP代碼轉換成的JAVA文件的內容。
下面是一個JSP文件test.jsp
經過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);
//上一句是由於後面的換行產生的
                out.write();
                out.write(\r\n\r\n\r\n\r\n);
                out.print( 輸出 );
                out.write(\r\n\r\n\r\n\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.實現替換
假設
3.更新問題
下面就討論一下如何更新生成靜態文件,其實從上面實現中你可以看到,很簡單的就是將生成的靜態文件刪除即可,至於什麼時候刪除,要看你的需求了。我能想到的幾種情況如下
當用來生成頁面的數據更新時
如果不需要很提供時時的數據可以定時更新
永遠不更新
----------------------------------------------------------------------------------------------------

JSP生成靜態HTML頁面範例

先建立一個模本頁面:template.htm

<Html>
<head>
<title>###title###</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../Css.css" rel=stylesheet type=text/css>
</head>
<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td align="center">###title###</td>
</tr>
<tr>
<td align="center">作者:###author###&nbsp;&nbsp;</td>
</tr>
<tr>
<td>###content###
</td>
</tr>
</table>
</body>
</html>

=========================================
再寫一個jsp頁面: buildhtml.jsp

<%@ page contentType="text/html; charset=gb2312" import="Java.util.*,java.io.*"%>
<%
try{
String title="jsp生成靜態html文件";
String content="小樣,還搞不定你?";
String editer="webjxcom";
String filePath = "";
filePath = request.getRealPath("/")+"template.htm";
out.print(filePath);
String templateContent="";
FileInputStream fileinputstream = new FileInputStream(filePath);//讀取模塊文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
out.print(templateContent);
templateContent=templateContent.replaceAll("###title###",title);
templateContent=templateContent.replaceAll("###content###",content);
templateContent=templateContent.replaceAll("###author###",editer);//替換掉模塊中相應的地方
out.print(templateContent);
// 根據時間得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路徑
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件輸出流
out.print("文件輸出路徑:<br>");
out.print(fileame);
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}
%>

-------------------------------------------
mport java.io.*;

import java.net.*;



public class Tools {

    final static Object lock = new Object();

    public static void makeHtml(String page, String filePath)...{

        makeHtml(page,filePath,"UTF-8");

    }



    public static void makeHtml(String page, String filePath,String chartset) {

        synchronized (lock) {

            HttpURLConnection huc = null;

            BufferedReader br = null;

            BufferedWriter bw = null;

            try {

                huc = (HttpURLConnection)new URL(page).openConnection();

                System.setProperty("sun.net.client.defaultConnectTimeout", "30000");

                System.setProperty("sun.net.client.defaultReadTimeout", "30000");

                huc.connect();

                InputStream stream = huc.getInputStream();

                bw = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(filePath),chartset));

                br = new BufferedReader(new InputStreamReader(stream, chartset));

                String line;

                while((line = br.readLine())!= null){

                   if(line.trim().length() > 0){

                        bw.write(line);

                        bw.newLine();

                   }

                }

            }catch (Exception e) {

                e.printStackTrace();

            }finally {

                try {

                    br.close();

                    bw.close();

                    huc.disconnect();

                }catch (Exception e) {

                    e.printStackTrace();

                }

            }

        }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章