我要啦免费统计

          微藍領域

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

          Jsp生成靜態頁面

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

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

          主站蜘蛛池模板: 遵义市| 新余市| 敖汉旗| 邛崃市| 中江县| 城市| 密云县| 嘉祥县| 周口市| 龙山县| 淮北市| 肥东县| 北宁市| 泾源县| 安福县| 商河县| 宁安市| 中宁县| 上虞市| 扶风县| 溧阳市| 紫阳县| 哈尔滨市| 团风县| 山阴县| 抚州市| 赤水市| 安顺市| 荣昌县| 绿春县| 道孚县| 台江县| 军事| 西青区| 杭锦旗| 米脂县| 毕节市| 诸城市| 托克托县| 兴安盟| 定边县|