国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP生成靜態(tài)頁實踐及其設(shè)計思想[轉(zhuǎn)]

2024-09-05 00:21:36
字體:
供稿:網(wǎng)友

引子:
我們以兩個大型網(wǎng)站為例作比較:
51job和智聯(lián)招聘(先聲明我不是為他們作廣告,僅以他們?yōu)槔骷夹g(shù)上的比較)
51job采用的是比較“先進”的php技術(shù),而智聯(lián)用的是比較落后的asp.但我們可能會明顯的感覺到51job的反應(yīng)速度相比智聯(lián)招聘實在是太慢了,為什么會這樣?細心的人可能會察覺到了。智聯(lián)雖然用的是asp,但他采用了另一種更巧妙的技術(shù)--asp生成靜態(tài)頁技術(shù)。所有的動態(tài)頁基本上都轉(zhuǎn)換成了html靜態(tài)頁,不用訪問數(shù)據(jù)庫,當(dāng)然反應(yīng)快了。
下面我們討論一下jsp怎么轉(zhuǎn)換成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>
#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("以下是模板內(nèi)容:<br>"+templateContent+"<br> 以下是置換以后的html內(nèi)容<br><hr>");
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#author#",editer);//替換掉模塊中相應(yīng)的地方
templateContent=templateContent.replaceAll("#content#",content);
// 根據(jù)時間得文件名
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();
嗯,核心技術(shù)就是這樣了,如果大家要求的性能更高,可以改用freemarker做模板。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙洋县| 久治县| 万全县| 涟水县| 崇礼县| 平安县| 泸西县| 阳泉市| 嘉禾县| 当雄县| 喀喇| 宜良县| 乐至县| 象山县| 金平| 阿拉善右旗| 墨竹工卡县| 十堰市| 南投市| 澳门| 宜昌市| 台东市| 栖霞市| 长沙市| 自治县| 高阳县| 金平| 芦溪县| 藁城市| 石门县| 刚察县| 桐庐县| 麟游县| 吴桥县| 安乡县| 苏尼特右旗| 景德镇市| 上思县| 蕲春县| 平乡县| 北京市|