我們以兩個大型網站為例作比較:
51job和智聯招聘(先宣告我不是為他們作廣告,僅以他們為例作技術上的比較)
51job和智聯招聘(先宣告我不是為他們作廣告,僅以他們為例作技術上的比較)
51job採用的是比較“先進”的php技術,而智聯用的是比較落後的asp.但我們可能會明顯的感覺到51job的反應速度相比智聯招聘實在是太慢了,為什麼會這樣?細心的人可能會察覺到了。智聯雖然用的是asp,但他採用了另一種更巧妙的技術–asp生成靜態頁技術。所有的動態頁基本上都轉換成了html靜態頁,不用訪問資料庫,當然反應快了。
下面我們討論一下jsp怎麼轉換成html??
首先要做一個模板。字尾不限,但一般都用*.template例子
下面我們討論一下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>
<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檔案儲存路徑
<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做模板。
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)
欄位: 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”);
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”;
<%
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];
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);
//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檔案儲存路徑
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();
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();
}
{
conn.close();
}
if(stmt!=null)
{
stmt.close();
}
%>
//資料庫連線檔案
import java.sql.*;
public class DBconn {
public DBconn() {
// TODO Auto-generated constructor stub
}
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());
}
*/
}
{
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” “[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]“>
<html xmlns=”[url]http://www.w3.org/1999/xhtml[/url]“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />
<title>#title#</title>
</head>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]“>
<html xmlns=”[url]http://www.w3.org/1999/xhtml[/url]“>
<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>
<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>
為了減輕伺服器壓力,將原來的文章管理系統由JSP檔案的從資料庫中取資料顯示改為由jsp生成靜態html檔案後直接訪問html檔案。下面是一個簡單的示例
1.buildhtml.jsp
<%@ page contentType=”text/html; charset=gb2312″ import=”java.util.*,java.io.*”%>
<%
try{
String title=”This is Title”;
String content=”This is Content Area”;
String editer=”LaoMao”;
String filePath = “”;
filePath = request.getRealPath(“/”)+”test/template.htm”;
//out.print(filePath+”<br>”);
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(templateContent);
templateContent=templateContent.replaceAll(“###title###”,title);
templateContent=templateContent.replaceAll(“###content###”,content);
templateContent=templateContent.replaceAll(“###author###”,editer);//替換掉模組中相應的地方
//out.print(templateContent);
// 根據時間得檔名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +”.html”;
fileame = request.getRealPath(“/”)+fileame;//生成的html檔案儲存路徑
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立檔案輸出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}
<%
try{
String title=”This is Title”;
String content=”This is Content Area”;
String editer=”LaoMao”;
String filePath = “”;
filePath = request.getRealPath(“/”)+”test/template.htm”;
//out.print(filePath+”<br>”);
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(templateContent);
templateContent=templateContent.replaceAll(“###title###”,title);
templateContent=templateContent.replaceAll(“###content###”,content);
templateContent=templateContent.replaceAll(“###author###”,editer);//替換掉模組中相應的地方
//out.print(templateContent);
// 根據時間得檔名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +”.html”;
fileame = request.getRealPath(“/”)+fileame;//生成的html檔案儲存路徑
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立檔案輸出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}
%>
2. template.htm
<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>
<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>
<table width=”500″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”2″>
<tr>
<td align=”center”>###title###</td>
</tr>
<tr>
<td align=”center”>author:###author### </td>
</tr>
<tr>
<td>###content###
</td>
</tr>
<table width=”500″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”2″>
<tr>
<td align=”center”>###title###</td>
</tr>
<tr>
<td align=”center”>author:###author### </td>
</tr>
<tr>
<td>###content###
</td>
</tr>
</table>
</body>
</html>
</body>
</html>