<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    狂淘

    www.kuangtao.net

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      6 隨筆 :: 185 文章 :: 68 評論 :: 0 Trackbacks

    為了減輕服務器壓力,將原來的文章管理系統由JSP文件的從數據庫中取數據顯示改為由jsp生成靜態html文件后直接訪問html文件。首先應創建一個模板文件,文件名和文件后綴可以隨意,但我一般常用的還是 *.template ,因此,這里就以 template.template 為例( 將模板文件放入 /WEB-INF/templates/ 文件夾下 ):下面是一個簡單的示例

    1.buildhtml.jsp

    Jsp代碼
    <%@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>   
    <%   
    try{   
         //設置字符編碼    
         request.setCharacterEncoding( "gb2312" );    
         response.setCharacterEncoding( "gb2312" );    
      
         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";   
         //生成的html文件保存路徑   
       fileame = request.getRealPath("/")+fileame;   
         //建立文件輸出流   
         FileOutputStream fileoutputstream = new FileOutputStream(fileame);   
       byte tag_bytes[] = templateContent.getBytes();   
         fileoutputstream.write(tag_bytes);   
         fileoutputstream.close();   
      
    }catch(Exception e){   
         out.print(e.toString());   
    }   
      
      
    %>  
    <%@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>
    <%
    try{
         //設置字符編碼
         request.setCharacterEncoding( "gb2312" );
         response.setCharacterEncoding( "gb2312" );

         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";
         //生成的html文件保存路徑
       fileame = request.getRealPath("/")+fileame;
         //建立文件輸出流
         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.template

    Html代碼
    <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>  
    <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>
     為了將應用進行國際化,可以將頁面的編碼設為 UTF-8

    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>

    本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/fbysss/archive/2006/03/07/618041.aspx


    本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/hitop0609/archive/2009/09/16/4555758.aspx

    posted on 2009-10-20 12:25 狂淘 閱讀(148) 評論(0)  編輯  收藏 所屬分類: jsp項目 傳到服務器問題
    主站蜘蛛池模板: 久久久久久a亚洲欧洲AV| 亚洲免费日韩无码系列| 亚洲影院在线观看| 无码中文字幕av免费放dvd| 亚洲精品无码专区在线在线播放| 特级毛片aaaa级毛片免费| 又大又硬又爽免费视频| 青青青视频免费观看| 亚洲精品成人片在线观看精品字幕 | 免费人成在线观看网站品爱网日本| 亚洲成av人在线观看网站| 全免费A级毛片免费看网站| jizzjizz亚洲日本少妇| 亚洲国产精品国产自在在线| 国产日韩AV免费无码一区二区三区| 久久亚洲AV永久无码精品| 精品一卡2卡三卡4卡免费视频| 亚洲成色在线影院| 成人免费的性色视频| 亚洲国产精品无码久久久秋霞1| 国产成人在线观看免费网站| 一本一道dvd在线观看免费视频 | 亚洲午夜福利717| 99视频在线看观免费| 亚洲人成网站在线在线观看| 亚洲精品国产电影| 一级毛片免费毛片一级毛片免费| 亚洲男女一区二区三区| 日本免费无遮挡吸乳视频电影| 亚洲免费视频一区二区三区| 亚洲视频在线播放| 日本成人免费在线| 免费无码又爽又刺激网站| 亚洲制服在线观看| 中文字幕亚洲日本岛国片| 麻花传媒剧在线mv免费观看| 美女的胸又黄又www网站免费| 亚洲AV永久无码精品成人| 嫩草影院在线免费观看| 国产V片在线播放免费无码 | 精品国产麻豆免费网站|