引子:
我們以兩個大型網站為例作比較:
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();
新聞熱點
疑難解答