我要啦免费统计

          微藍(lán)領(lǐng)域

          我的學(xué)習(xí)檔案館
          posts - 19, comments - 57, trackbacks - 0, articles - 57
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Jsp生成靜態(tài)頁面

          Posted on 2007-08-18 09:16 hilor 閱讀(253) 評論(0)  編輯  收藏 所屬分類: J2EE

          為了減輕服務(wù)器壓力,將原來的文章管理系統(tǒng)由JSP文件的從數(shù)據(jù)庫中取數(shù)據(jù)顯示改為由jsp生成靜態(tài)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);//替換掉模塊中相應(yīng)的地方
           //out.print(templateContent);
           // 根據(jù)時間得文件名
           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>

          <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###&nbsp;&nbsp;</td>
            </tr>
            <tr>
              <td>###content###
           </td>
           
            </tr>

          </table>
          </body>
          </html>

          主站蜘蛛池模板: 固镇县| 承德市| 康定县| 高邑县| 额济纳旗| 平乐县| 阿尔山市| 游戏| 阳曲县| 北票市| 蛟河市| 芦山县| 塘沽区| 兰州市| 宜宾县| 木兰县| 甘德县| 临朐县| 时尚| 昆山市| 乌恰县| 葫芦岛市| 峨眉山市| 乐陵市| 皮山县| 嘉义县| 绍兴市| 垦利县| 玉林市| 湘乡市| 昌邑市| 平舆县| 鱼台县| 沅陵县| 长葛市| 铜鼓县| 高邑县| 凌云县| 慈溪市| 疏附县| 齐河县|