模板頁modle.html
<!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=utf-8" />
<title>無標題文檔</title>
</head>
<table width="200" border="1">
<tr>
<td>標題:###title###</td>
</tr>
<tr>
<td>來源:###source###</td>
</tr>
<tr>
<td>發布時間:###addtime###</td>
</tr>
<tr>
<td>文章:###article###</td>
</tr>
</table>
<body>
</body>
</html>
addNews.jsp (注意這裡編碼是GB2312)
<%@ page language="java" contentType="text/html;charset=GB2312" errorPage=""%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'addNews.jsp' starting page</title>
</head>
<body>
<form action="add_do.jsp" method="post">
<table>
<tr>
<td>標題:</td>
<td><input type="text" name="title"/> </td>
</tr>
<tr>
<td>來源:</td>
<td><input type="text" name="source"/> </td>
</tr>
<tr>
<td>內容:</td>
<td><textarea rows="10" cols="40" name="article"></textarea> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"/> <input type="reset" value="取消"/></td>
</tr>
</table>
</body>
</html>
產生新聞靜態頁 add_do.jsp
<%@ page language="java" contentType="text/html;charset=utf-8" errorPage=""%>
<%@ page import="java.util.*,java.io.*,java.text.*"%>
<%
String title=new String(request.getParameter("title").getBytes("iso-8859-1"),"GB2312");
// System.out.print(title);
String source =new String(request.getParameter("source").getBytes("iso-8859-1"),"GB2312");
String article =new String(request.getParameter("article").getBytes("iso-8859-1"),"GB2312");
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
String nowTime=format.format(new Date());
String fileame="";
int a = 0;
try{
String filePath = "",path="";
filePath = request.getRealPath("/")+"modle.html";
filePath = filePath.replaceAll("////","/");
String templateContent="";
//讀取hmtl模板檔案
FileInputStream fis = new FileInputStream(filePath);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = null;
while ((line = br.readLine()) != null)
content.append(line + "/n");
br.close();
in.close();
fis.close();
//替換模板裡面的相關部分
templateContent = new String(content);
templateContent=templateContent.replaceAll("###title###",title);
templateContent=templateContent.replaceAll("###source###",source);
templateContent=templateContent.replaceAll("###addtime###",nowTime);
templateContent=templateContent.replaceAll("###article###",article);//替換掉模組中相應的地方
System.out.println(templateContent);
Calendar calendar = Calendar.getInstance();
fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
//建立新聞頁面存放目錄
path= request.getRealPath("/")+nowTime+"/";
File d=new File(path);//建立代表Sub目錄的File對象,並得到它的一個引用
if(!d.exists()){//檢查Sub目錄是否存在
d.mkdir();//建立Sub目錄
}
File f=new File(path,fileame);
if(!f.exists()){//檢查File.txt是否存在
f.createNewFile();//在目前的目錄下建立一個名為File.txt的檔案
}
fileame = request.getRealPath("/")+nowTime+"/"+fileame;//產生的html檔案儲存路徑
//替換後的內容寫入到檔案
FileOutputStream fos = new FileOutputStream(fileame);
Writer output = new OutputStreamWriter(fos, "UTF-8");
output.write(templateContent);
output.close();
fos.close();
out.println(" <script>window.alert('產生靜態新聞頁成功');window.location.href='addNews.jsp'</script>");
}catch(Exception e){
System.out.print(e.toString());
}
%>
最後結果 組建檔案 2008-09-11/1247651927156.html
2、資料庫中同時儲存檔案標題、檔案名稱、日期等資訊,新聞內容等其他的資訊就沒有必要保留了;
3、前台調用直接從資料庫中取XXX.htm檔案名稱、檔案標題就行了;