[轉(zhuǎn)]利用Java生成靜態(tài)HMTL頁(yè)面
這幾天公司需要生成靜態(tài)的HTML頁(yè)面以減小數(shù)據(jù)庫(kù)與服務(wù)器的壓力和負(fù)擔(dān),于是在網(wǎng)絡(luò)上一陣狂搜,找到幾篇相當(dāng)不錯(cuò)的文章和一些相當(dāng)有用的資料。為了方便,我整理在自己的BLOG,以供參考!
在接下來(lái)的應(yīng)用中,我自己想到另一種解決方案,就是通過(guò)Ajax + Struts + XML解決靜態(tài)頁(yè)面方案,一并寫下來(lái),與大家分享!
1. 生成靜態(tài)頁(yè)面技術(shù)解決方案之一(轉(zhuǎn)載)
2. 生成靜態(tài)頁(yè)面技術(shù)解決方案之二(轉(zhuǎn)載)
3. 一個(gè)實(shí)現(xiàn)將動(dòng)態(tài)頁(yè)面轉(zhuǎn)為靜態(tài)的方案(轉(zhuǎn)載)
4. JSP生成靜態(tài)HTML頁(yè)面范例(轉(zhuǎn)載)
5. 利用XML+XSL生成靜態(tài)頁(yè)面技術(shù)方案(轉(zhuǎn)載)
6. Ajax + Struts + XML解決靜態(tài)頁(yè)面方案(原創(chuàng))
隨著網(wǎng)站訪問(wèn)量的加大,每次從數(shù)據(jù)庫(kù)讀取都是以效率作為代價(jià)的,靜態(tài)頁(yè)加在搜索時(shí),也會(huì)被優(yōu)先考慮。互聯(lián)網(wǎng)上流行的做法是將數(shù)據(jù)源代碼寫入數(shù)據(jù)庫(kù)再?gòu)臄?shù)據(jù)庫(kù)讀取生成靜態(tài)面,這樣無(wú)形間就加大了數(shù)據(jù)庫(kù)。將現(xiàn)有的JSP頁(yè)直接生成靜態(tài)頁(yè),將會(huì)節(jié)省很多。
為什么要生成靜態(tài)首頁(yè)?
1、如果你首頁(yè)讀取的數(shù)據(jù)庫(kù)次數(shù)比較多,速度很慢,而且占用很多服務(wù)器資源。使用靜態(tài)頁(yè)面訪問(wèn)速度當(dāng)然快多了
2、搜索引擎容易搜索到
3、如果程序出問(wèn)題,也能保證首頁(yè)能訪問(wèn)
諸如此類等等好處,那么下面幾篇文章給大家?guī)讉€(gè)完整的解決方案!
----------------------------------------------------------------------------------------------------
轉(zhuǎn)載者前言:這是一個(gè)全面的jsp動(dòng)態(tài)頁(yè)面靜態(tài)化方案,本站的帖子靜態(tài)化方案將借鑒這篇帖子中方法。向http://www.agilejava.org的single的共享精神致敬。
轉(zhuǎn)帖正文:
相信很多人都希望自己的頁(yè)面越快越好,最好是能靜態(tài)的,提高客戶訪問(wèn)速度。也便于搜索引擎搜索。所以,就希望我們的動(dòng)態(tài)讀取數(shù)據(jù)庫(kù)的頁(yè)面,盡可能的生成靜態(tài)頁(yè)面。一下系列文章,介紹一下個(gè)人的解決方案。
本系列將介紹個(gè)人的一種方法,在不改變?cè)瓉?lái)jsp文件的基礎(chǔ)上,只需要加入少量的代碼,就讓你的新聞發(fā)布系統(tǒng),很容易就完全變成靜態(tài)的頁(yè)面。
本文假設(shè)你是用java開(kāi)發(fā)的web動(dòng)態(tài)頁(yè)面。
第一步,加入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");// 你要訪問(wèn)的jsp文件名,如index,不包括擴(kuò)展名
// 則你訪問(wèn)這個(gè)servlet時(shí)加參數(shù).如http://localhost/test/toHtml?file_name=index
url = "/" + file_name + ".jsf";// 你要生成的頁(yè)面的文件名。我的擴(kuò)展名為jsf .
name = ConfConstants.CONTEXT_PATH+""""+ file_name + ".htm";// 這是生成的html文件名,如index.htm.文件名字與源文件名相同。擴(kuò)展名為htm
//ConfConstants.CONTEXT_PATH為你的應(yīng)用的上下文路徑。
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輸出的內(nèi)容寫到xxx.htm
os.writeTo(fos);
fos.close();
PrintWriter ōut = response.getWriter();
out
.print("<p align=center><font size=3 color=red>頁(yè)面已經(jīng)成功生成!single<br>http://www.agilejava.org/space/? 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>
第三步、運(yùn)行servlet。如:http://localhost:8080/test/toHtml?file_name=index
OK,這就在你的test項(xiàng)目的根目錄下,生成了一個(gè)index.htm的靜態(tài)文件。
局限性:本文只能生成一個(gè)文件!訪問(wèn)一次,生成一個(gè)文件。并且生成的文件名也與原來(lái)的文件名相同。
比較適合主頁(yè)生成靜態(tài)頁(yè)面。
本系列的后續(xù)文章將解決更多的問(wèn)題。使之在新聞發(fā)布系統(tǒng)中,很容易就集成應(yīng)用。
----------------------------------------------------------------------------------------------------
注意:轉(zhuǎn)貼本文,請(qǐng)加上本文鏈接http://www.agilejava.org/space/?233/action_viewspace_itemid_21.html
在上一篇文章中,生成靜態(tài)頁(yè)面,是有一定的局限性的。生成主頁(yè)是很方便,但要生成二級(jí)頁(yè)面,就不方便了。
本文假設(shè)一個(gè)新聞發(fā)布系統(tǒng)。希望后臺(tái)發(fā)布的,前臺(tái)顯示的是靜態(tài)的文檔。這就涉及,主頁(yè)要是靜態(tài)的,同時(shí)二級(jí)列表也是靜態(tài)的,新聞內(nèi)容也是靜態(tài)的。也就是說(shuō),在發(fā)布一篇新聞的時(shí)候,可能涉及到三個(gè)地方生成靜態(tài)文檔。并且,要生成一個(gè)網(wǎng)頁(yè),必須訪問(wèn)一個(gè)servlet。在大量生成靜態(tài)網(wǎng)頁(yè)的時(shí)候,
以下方法,可以解決這些問(wèn)題。
一、加入一下servelet
/**
* @file_name 文件名及文件之后的參數(shù).最好為a.jsf?fileId=aaaa
* @path 文件所在的路徑.相對(duì)于根目錄而言的.
* @realName文件要保存的名字
* @realPath文件要保存的真實(shí)路徑。默認(rèn)與文件所在的目錄相同。
*/
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");// 你要訪問(wèn)的jsp文件,如news.jsf。
// file_name如:fileDetail.jsf?fileId=56.要是有參數(shù), 只有一個(gè)參數(shù)。并且以參數(shù)名作為文件名。
String realName = request.getParameter("realName");// 要保存的文件名。如aaa;注意可以沒(méi)有這個(gè)參數(shù)。
String path = request.getParameter("path");// 你要訪問(wèn)的jsp文件路徑。如news。注意可以沒(méi)有這個(gè)參數(shù)。
String realPath = request.getParameter("realPath");// 你要保存的文件路徑,如htmlNews.注意可以沒(méi)有這個(gè)參數(shù)。
// 下面確定要保存的文件名字。
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("."));
}
}
// 下面構(gòu)造要訪問(wèn)的頁(yè)面。
if (path == null || path == "") {
url = "/" + file_name;// 這是你要生成HTML的jsp文件,如
} else {
url = "/" + path + "/" + file_name;// 這是你要生成HTML的jsp文件,如
}
// 下面構(gòu)造要保存的文件名,及路徑。
// 1、如果有realPath,則保存在realPath下。
// 2、如果有path則保存在path下。
// 3、否則,保存在根目錄下。
if (realPath == null || realPath == "") {
if (path == null || path == "") {
name = ConfConstants.CONTEXT_PATH + """" + realName + ".htm";// 這是生成的html文件名,如index.htm.說(shuō)明: 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.
}
// 訪問(wèn)請(qǐng)求的頁(yè)面,并生成指定的文件。
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輸出的內(nèi)容寫到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>
三、寫一個(gè)通用的方法, 供調(diào)用。
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("沒(méi)有連接成功");
} else {
System.out.println("連接成功了 ");
}
} catch (Exception e) {
// TODO: handle exception
}
}
//這個(gè)方法適當(dāng)重載,就可以省去一些參數(shù)傳遞。
}
四、在你的新聞發(fā)布save時(shí),調(diào)用方法。
1、CallHtml.callOnePage("info.jsf?file_id=aaa",news,"", "");//將在news目錄下生成一個(gè)aaa.htm的靜態(tài)文件
2、CallHtml.callOnePage("newsList.jsf",news,"", "");//將在news目錄下生成一個(gè)newsList.htm的靜態(tài)文件,顯示最新的新聞。
3、CallHtml.callOnePage("index.jsf","","", "");//生成主頁(yè)。
好了,這就保持了,主頁(yè)、列表、新聞內(nèi)容都是最新的靜態(tài)頁(yè)面了。
----------------------------------------------------------------------------------------------------
一個(gè)實(shí)現(xiàn)將動(dòng)態(tài)頁(yè)面轉(zhuǎn)為靜態(tài)的方案
1.前言
為了能深入淺出的理解這個(gè)框架的由來(lái),我們首先來(lái)了解一下JSP解析器將我們寫的JSP代碼轉(zhuǎn)換成的JAVA文件的內(nèi)容。
下面是一個(gè)JSP文件test.jsp
經(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);
//上一句是由于后面的換行產(chǎ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內(nèi)建的幾個(gè)對(duì)象(out、request、response、session、pageContext、application、config、page)是怎么產(chǎn)生的,懂servlet的朋友一看就能明白。
下面重點(diǎn)理解一下out對(duì)象,它被聲明為JspWriter類型,JspWriter是一個(gè)抽象類,在包javax.servlet.jsp中可以找到它的定義。
abstractpublic 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);
abstractpublicvoid newLine() throws IOException ;
abstractpublicvoid print(boolean arg0) throws IOException ;
abstractpublicvoid print(char arg0) throws IOException ;
abstractpublicvoid print(int arg0) throws IOException ;
abstractpublicvoid print(long arg0) throws IOException ;
abstractpublicvoid print(float arg0) throws IOException ;
abstractpublicvoid print(double arg0) throws IOException ;
abstractpublicvoid print(char[] arg0) throws IOException ;
abstractpublicvoid print(String arg0) throws IOException ;
abstractpublicvoid print(Object arg0) throws IOException ;
abstractpublicvoid println() throws IOException ;
abstractpublicvoid println(boolean arg0) throws IOException ;
abstractpublicvoid println(char arg0) throws IOException ;
abstractpublicvoid println(int arg0) throws IOException ;
abstractpublicvoid println(long arg0) throws IOException ;
abstractpublicvoid println(float arg0) throws IOException ;
abstractpublicvoid println(double arg0) throws IOException ;
abstractpublicvoid println(char[] arg0) throws IOException ;
abstractpublicvoid println(String arg0) throws IOException ;
abtractpublicvoid println(Object arg0) throws IOException ;
abstractpublicvoid clear() throws IOException ;
abstractpublicvoid clearBuffer() throws IOException ;
abstractpublicvoid flush() throws IOException ;
abstractpublicvoid close() throws IOException ;
public int getBufferSize() ;
abstractpublicint getRemaining() ;
publicboolean isAutoFlush() ;
}
我相信當(dāng)我寫到這里你可能已經(jīng)知道我想怎么做了。是的,來(lái)個(gè)偷天換日,繼承JspWriter類,然后實(shí)現(xiàn)其定義的虛函數(shù),然后把out變量替換成你自己實(shí)現(xiàn)的類的實(shí)例就ok了。
2.實(shí)現(xiàn)替換
假設(shè)
3.更新問(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)不更新
----------------------------------------------------------------------------------------------------
先建立一個(gè)模本頁(yè)面: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### </td>
</tr>
<tr>
<td>###content###
</td>
</tr>
</table>
</body>
</html>
=========================================
再寫一個(gè)jsp頁(yè)面: buildhtml.jsp
<%@ page contentType="text/html; charset=gb2312" import="Java.util.*,java.io.*"%>
<%
try{
String title="jsp生成靜態(tài)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);//替換掉模塊中相應(yīng)的地方
out.print(templateContent);
// 根據(jù)時(shí)間得文件名
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());
}
%>
----------------------------------------------------------------------------------------------------
Ajax + Struts + XML解決靜態(tài)頁(yè)面方案(原創(chuàng))
對(duì)于其他幾位前輩提出的方案,我也是受益良深,所以方案中,最簡(jiǎn)單的就是將JSP輸出為HTML,最麻煩的是利用IO將輸出信息組成HTML文件,最難的對(duì)我來(lái)說(shuō)應(yīng)該是生成XML的方案。我沒(méi)有學(xué)XSL,但據(jù)說(shuō)比較難,遠(yuǎn)不如HTML那么容易。盡管如此,生成XML這種方案卻是我最欣賞的,原因有四:
第一是實(shí)現(xiàn)了表示與數(shù)據(jù)的分離;
第二是易于操作,增刪改都相當(dāng)方便;
第三是跨平臺(tái)特性讓它應(yīng)用領(lǐng)域更廣;
第四XML本身就可以當(dāng)數(shù)據(jù)庫(kù)使用,使得它可以合理組織數(shù)據(jù)。
OK,基于這些原因,小弟在推敲中想到一個(gè)解決方案:如果我們?cè)诜?wù)器生成若干XML文檔,由統(tǒng)一的HTML裝載,然后客戶端執(zhí)行HTML時(shí),通過(guò)AJAX異步載入XML文檔數(shù)據(jù),那么結(jié)果是否會(huì)達(dá)到達(dá)人心動(dòng)的效果呢?
實(shí)現(xiàn)此技術(shù)需要解決的問(wèn)題:
1. 從數(shù)據(jù)庫(kù)取數(shù)據(jù)動(dòng)態(tài)生成并寫入XML文件
2. 利用URL傳入XML文檔名
3. 客戶端使用JS解析URL取XML文檔名然后使用AJAX載入文件,最后動(dòng)態(tài)組織數(shù)據(jù)到頁(yè)面中
以上分析是小弟一些拙見(jiàn),高手勿笑!小弟在想到這個(gè)方案時(shí),便馬上寫了一個(gè)Demo測(cè)試了一下,程序采用的是Strut框架寫的,完成功能就是以上三點(diǎn)的描述,不過(guò)為了測(cè)試方便,并未使用數(shù)據(jù)庫(kù),而是自己定義了用于構(gòu)造XML文件的類手動(dòng)輸入的,程序打包的下載地址:http://download.csdn.net/user/rodgersnow 大家找到利用AJAX生成靜態(tài)HTML的Demo就可以下載了
posted on 2007-11-05 11:47 最後の騎士 閱讀(4274) 評(píng)論(0) 編輯 收藏 所屬分類: jsp