JSP產生靜態頁實踐及其設計思想

來源:互聯網
上載者:User

引子:
我們以兩個大型網站為例作比較:
51job和智聯招聘(先聲明我不是為他們作廣告,僅以他們為例作技術上的比較)
51job採用的是比較“先進”的php技術,而智聯用的是比較落後的asp.但我們可能會明顯的感覺到51job的反應速度相比智聯招聘實在是太慢了,為什麼會這樣?細心的人可能會察覺到了。智聯雖然用的是asp,但他採用了另一種更巧妙的技術--asp產生靜態頁技術。所有的動態網頁基本上都轉換成了html靜態頁,不用訪問資料庫,當然反應快了。
下面我們討論一下jsp怎麼轉換成html??
首先要做一個模板。尾碼不限,但一般都用*.template例子
<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>
<P align="center">
#title#<BR><BR><BR>
作者:#author#<BR><BR>
<BR>
#content#<BR><BR><BR><BR>
</P>
</body>
</html>
做一個處理模板的類或者jsp檔案、(為說明問題我們從簡單入手以一個jsp檔案為例)
filePath = request.getRealPath("/")+"WEB-INF/templates/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("以下是模板內容:<br>"+templateContent+"<br> 以下是置換以後的html內容<br><hr>");
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#author#",editer);//替換掉模組中相應的地方
templateContent=templateContent.replaceAll("#content#",content);
// 根據時間得檔案名稱
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//產生的html檔案儲存路徑

out.print(templateContent);
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立檔案輸出資料流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
嗯,核心技術就是這樣了,如果大家要求的效能更高,可以改用freemarker做模板。

相關文章

聯繫我們

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