成人免费在线观看视频-国产绿帽子多p交换视频-精品国产一区av-免费xx视频-人妻熟妇乱又伦精品视频中文字幕-特级毛片内射www无码

JSP生成靜態頁面實踐和其設計思想路

2012/3/2 15:09:03   閱讀:2616    發布者:2616

引子:
我們以兩個大型網站為例作比較:
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做模板。
經過一翻調試,成功了。。呵
附上源碼。。

JDK 1.5 +ECLipSE +TOMCAT 5.0.28 + MySQL 5.0
數據庫TEST ,表名news
字段: id   int 自動增長 , Title  varchar(20) , Content  varchar(200)  , Author  varchar(10)
makeFile.jsp
 <%
  Connection conn = DBconn.getConnection();
  Statement stmt = conn.createStatement();
  ResultSet Rs = stmt.executeQuery("select * from news");
  System.out.println("success");
 %>
 
  <%
 
  String filePath = request.getRealPath("/")+"template.htm";
  System.out.println(filePath);
 
  String templateContent;
  FileInputStream fileinputstream = new FileInputStream(filePath);
  int lenght = fileinputstream.available(); //available() 返回可以不受阻塞地從此文件輸入流中讀取的字節數。
 
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes); //read(byte[] b) 從此輸入流中將最多 b.length 個字節的數據讀入一個字節數組中。
fileinputstream.close();
//templateContent = new String(bytes);
String title;
String content;
String author;
while(Rs.next())
{
templateContent = new String(bytes);//如果不用這句,則替換一次之后,templateContent中就沒有#**#標志了。所以要重新生成
 title = Rs.getString("Title");
 content = Rs.getString("Content");
 author = Rs.getString("Author");
out.println(title+"********"+content+"****"+author);
out.print("以下是模板內容:<br>"+templateContent+"<br> 以下是置換以后的html內容<br><hr>");
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#author#",author);//替換掉模塊中相應的地方
templateContent=templateContent.replaceAll("#content#",content);
// 根據時間得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+"Html/"+fileame;//生成的html文件保存路徑

out.print(templateContent);
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件輸出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
if(conn!=null)
    {
        conn.close();
    }
    if(stmt!=null)
    {
        stmt.close();
    }


  %>
//數據庫連接文件
import java.sql.*;
public class DBconn {
   
    public DBconn() {
       
        // TODO Auto-generated constructor stub
    }
    public static Connection getConnection()
    {
        Connection conn = null;
       
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://" + "localhost" + "/" + "test" +
        "?useUnicode=true&characterEncoding=GB2312","root","111111");
               
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return conn;
        }
    /*public static void main(String[] args) throws Exception
    {
        Connection con=getConnection();
        System.out.println(con.isClosed());
       
    }
*/
}
 
// 模板文件
template.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>#title#</title>
</head>
<body>
<table width="380" height="107" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFCC99">
  <tr>
    <td height="16" bgcolor="#FFCC99"><div align="center">#title#</div></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">#content#</td>
  </tr>
  <tr>
    <td height="13" align="right" bgcolor="#FFFFFF">#author#</td>
  </tr>
</table>
</body>
</html>

主站蜘蛛池模板: 国产又色又爽又刺激视频| 亚洲人成人无码网www国产| 免费无遮挡在线观看网站| 天堂av无码大芭蕉伊人av孕妇 | 国产女人好紧好爽| 邻居少妇张开双腿让我爽一夜 | 欧美浓毛大泬视频| 日本不卡免费高清视频| 国产丝袜美女一区二区三区| 无码人妻丰满熟妇区五十路百度 | 无码一区二区三区中文字幕| 99久久精品美女高潮喷水| 久久综合给合久久狠狠97色| 最新亚洲人成无码网www电影| 久久久久久综合网天天| 亚洲综合色成在线观看| 亚洲精品乱码久久久久久久久久久久| 久久国产福利国产秒拍| 精品亚洲一区二区三区在线观看| 色欲国产麻豆一精品一av一免费| 亚洲第一无码专区天堂| 天天射寡妇射| 国产精品民宅偷窥盗摄| 性少妇无码播放| 无码欧精品亚洲日韩一区| 成熟丰满熟妇xxxxx丰满| 五月天激情电影| 亚洲 都市 校园 激情 另类| 无码人妻aⅴ一区二区三区69岛| 91免费国产高清在线| 久久久亚洲欧洲日产国码αv | 久久精品黄aa片一区二区三区| 丰满人妻无奈张开双腿av| 亚洲中文字幕无码一区在线| 无码人妻丰满熟妇奶水区码| 性刺激的大陆三级视频| 婷婷开心激情综合五月天| 精品国产成人av在线| 中文字日产幕乱五区| 精品一区二区三区免费播放| 无码无遮挡又大又爽又黄的视频|