<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項目 傳到服務器問題
    主站蜘蛛池模板: 亚洲精品成人无限看| 亚洲冬月枫中文字幕在线看| 亚洲综合精品香蕉久久网| 中文字幕日本人妻久久久免费| 亚洲毛片在线免费观看| 亚洲 综合 国产 欧洲 丝袜| 亚洲爆乳无码精品AAA片蜜桃| 亚洲精品无码av天堂| 4虎1515hh永久免费| 色欲色欲天天天www亚洲伊| 永久在线毛片免费观看| 亚洲另类小说图片| 午夜亚洲福利在线老司机| 99久久99热精品免费观看国产| 亚洲丶国产丶欧美一区二区三区| 久久亚洲国产成人影院网站| 国产92成人精品视频免费| 亚洲AV无码专区在线亚| 亚洲国产精品一区二区三区久久| 一区二区三区在线观看免费| 中文字幕亚洲电影| 免费做爰猛烈吃奶摸视频在线观看| wwwxxx亚洲| 亚洲国产精彩中文乱码AV| 最近中文字幕免费2019| 免费夜色污私人影院网站| 亚洲一区二区三区精品视频| 久久久无码精品亚洲日韩软件 | 1000部拍拍拍18勿入免费视频下载 | 亚洲免费观看网站| 中文字幕免费在线观看动作大片| 亚洲国产成人精品无码区在线观看| 成人免费无码大片A毛片抽搐色欲| 性无码免费一区二区三区在线| 亚洲一级毛片在线观| 国产亚洲综合久久系列| 免费日本黄色网址| 中文字幕一区二区三区免费视频| 亚洲成a人无码亚洲成av无码| 亚洲图片中文字幕| 亚洲国产精品毛片av不卡在线 |