我要啦免费统计

          微藍領域

          我的學習檔案館
          posts - 19, comments - 57, trackbacks - 0, articles - 57
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          JSP生成靜態頁面(連接數據庫)

          Posted on 2007-08-18 09:39 hilor 閱讀(432) 評論(0)  編輯  收藏 所屬分類: J2EE
          主要原理請參考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>
          主站蜘蛛池模板: 灵台县| 叙永县| 察隅县| 禄丰县| 鹿邑县| 和平县| 台东县| 伊吾县| 龙州县| 新干县| 松原市| 左云县| 宣威市| 梁平县| 宜阳县| 乐都县| 临沭县| 八宿县| 修水县| 红原县| 芜湖县| 荔波县| 太保市| 句容市| 大关县| 邢台市| 土默特右旗| 呼伦贝尔市| 汝南县| 金塔县| 宁蒗| 宁城县| 长乐市| 蓬莱市| 清水县| 陵水| 东乌| 岳池县| 阳朔县| 延津县| 太原市|