国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

用JSP生成靜態頁面

2024-09-05 00:19:18
字體:
來源:轉載
供稿:網友

主要原理請參考http://blog.hunan2s.com/article.asp?id=233
經過一翻調試,成功了。。呵
附上源碼。。

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>

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 楚雄市| 南召县| 左云县| 廉江市| 班玛县| 淳安县| 伽师县| 当涂县| 宜宾县| 桓仁| 五常市| 安阳县| 隆子县| 雷波县| 恩施市| 绿春县| 资源县| 渑池县| 平顺县| 伊川县| 凤城市| 布尔津县| 桃园市| 镇江市| 阳江市| 昌都县| 曲周县| 集贤县| 宁国市| 胶南市| 亚东县| 应城市| 蕲春县| 游戏| 吴旗县| 明水县| 宣化县| 新巴尔虎左旗| 晴隆县| 晴隆县| 晴隆县|