九九热线视频精品99-动漫高h纯肉无码视频在线观看-午夜理论无码片在线观看免费-99精品国产在热久久无码-亚洲国产成人乱码

JSP生成靜態(tài)頁(yè)面實(shí)踐和其設(shè)計(jì)思想路

2012/3/2 15:09:03   閱讀:2621    發(fā)布者:2621

引子:
我們以?xún)蓚€(gè)大型網(wǎng)站為例作比較:
51job和智聯(lián)招聘(在這里先聲明我不是為他們作廣告,只是把他們的網(wǎng)站來(lái)做例子作技術(shù)上的比較)
51job采用的是比較“先進(jìn)”的php技術(shù),而智聯(lián)用的是比較落后的asp.但我們可能會(huì)明顯的感覺(jué)到51job的反應(yīng)速度相比智聯(lián)招聘實(shí)在是太慢了,為什么會(huì)這樣?細(xì)心的人可能會(huì)察覺(jué)到了。智聯(lián)雖然用的是asp,但他采用了另一種更巧妙的技術(shù)--asp生成靜態(tài)頁(yè)技術(shù)。所有的動(dòng)態(tài)頁(yè)基本上都轉(zhuǎn)換成了html靜態(tài)頁(yè),不用訪問(wèn)數(shù)據(jù)庫(kù),當(dāng)然反應(yīng)快了。
下面我們討論一下jsp怎么轉(zhuǎn)換成html?
首先要做一個(gè)模板。后綴不限,但一般都用*.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>
做一個(gè)處理模板的類(lèi)或者jsp文件、(為說(shuō)明問(wèn)題我們從簡(jiǎn)單入手以一個(gè)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ù)時(shí)間得文件名
 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做模板。
經(jīng)過(guò)一翻調(diào)試,成功了。。呵
附上源碼。。

JDK 1.5 +ECLipSE +TOMCAT 5.0.28 + MySQL 5.0
數(shù)據(jù)庫(kù)TEST ,表名news
字段: id   int 自動(dòng)增長(zhǎng) , 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() 返回可以不受阻塞地從此文件輸入流中讀取的字節(jié)數(shù)。
 
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes); //read(byte[] b) 從此輸入流中將最多 b.length 個(gè)字節(jié)的數(shù)據(jù)讀入一個(gè)字節(jié)數(shù)組中。
fileinputstream.close();
//templateContent = new String(bytes);
String title;
String content;
String author;
while(Rs.next())
{
templateContent = new String(bytes);//如果不用這句,則替換一次之后,templateContent中就沒(méi)有#**#標(biāo)志了。所以要重新生成
 title = Rs.getString("Title");
 content = Rs.getString("Content");
 author = Rs.getString("Author");
out.println(title+"********"+content+"****"+author);
out.print("以下是模板內(nèi)容:<br>"+templateContent+"<br> 以下是置換以后的html內(nèi)容<br><hr>");
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#author#",author);//替換掉模塊中相應(yīng)的地方
templateContent=templateContent.replaceAll("#content#",content);
// 根據(jù)時(shí)間得文件名
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();
    }


  %>
//數(shù)據(jù)庫(kù)連接文件
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>

主站蜘蛛池模板: 国产精品久久久久久人妻精品18| 国产av导航大全精品| 色一情一乱一伦一区二区三区| 国产成人综合在线观看不卡| 九九久久精品国产波多野结衣| 国产成人片无码视频| 日本久久久久久久做爰片日本| 久久精品国产亚洲77777| 无码大潮喷水在线观看| 国产亚洲成av人片在线观看| 2019年国产精品手机视频| 国产成人人人97超碰超爽8| 一区二区免费高清观看国产丝瓜| 黑人上司粗大拔不出来电影| 国产1卡2卡三卡四卡精品| 热re99久久精品国产99热| 国产亚洲人成a在线v网站| 狼友av永久网站免费观看孕交| 老色鬼永久视频网站| 日韩精品内射视频免费观看| 97在线精品视频免费| 国产精品白浆一区二小说| 亚洲精品无码久久久久av老牛| 一本一本久久a久久综合精品蜜桃 波多野结衣av一区二区无码 | 少妇被粗大猛进进出出| 国产亚洲精品久久久久久小说| 久久久久久妓女精品影院| 男受被做哭激烈娇喘gv视频| 国产成人午夜精品福利视频| 专干老熟女视频在线观看| 亚洲人精品午夜射精日韩| 国产精品久久久久9999高清 | 久久天天躁狠狠躁夜夜av不卡| 国产丝袜在线精品丝袜| 亚洲男人第一av网站| 正在播放国产对白孕妇作爱| 免费无遮挡禁18污污网站| 成人av一本不卡二卡| 亚洲欧美日韩国产综合v| 国产日本精品视频在线观看| 玩弄放荡人妻少妇系列|