引子:
我們以兩個大型網(wǎng)站為例作比較:
51job和智聯(lián)招聘(先聲明我不是為他們作廣告,僅以他們?yōu)槔骷夹g上的比較)
51job采用的是比較“先進”的php技術,而智聯(lián)用的是比較落后的asp.但我們可能會明顯的感覺到51job的反應速度相比智聯(lián)招聘實在是太慢了,為什么會這樣?細心的人可能會察覺到了。智聯(lián)雖然用的是asp,但他采用了另一種更巧妙的技術--asp生成靜態(tài)頁技術。所有的動態(tài)頁基本上都轉換成了html靜態(tài)頁,不用訪問數(shù)據(jù)庫,當然反應快了。
下面我們討論一下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);
?// 根據(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();
嗯,核心技術就是這樣了,如果大家要求的性能更高,可以改用freemarker做模板。freemarker的使用方法可以參閱另一篇文章《Freemarker 模板工具的作用及使用》
引自:http://www.tkk7.com/zhanglijun33/archive/2006/07/28/60568.aspx
posted on 2006-09-15 15:26
壞男孩 閱讀(626)
評論(5) 編輯 收藏 所屬分類:
新知識學習