JSP實現靜態頁產生的例子

來源:互聯網
上載者:User
 

   

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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.