狂淘

          www.kuangtao.net

             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            6 隨筆 :: 185 文章 :: 68 評(píng)論 :: 0 Trackbacks

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

          1.buildhtml.jsp

          Jsp代碼
          <%@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>   
          <%   
          try{   
               //設(shè)置字符編碼    
               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);//替換掉模塊中相應(yīng)的地方   
             //out.print(templateContent);   
               // 根據(jù)時(shí)間得文件名   
             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{
               //設(shè)置字符編碼
               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);//替換掉模塊中相應(yīng)的地方
             //out.print(templateContent);
               // 根據(jù)時(shí)間得文件名
             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>
           為了將應(yīng)用進(jìn)行國(guó)際化,可以將頁(yè)面的編碼設(shè)為 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);//替換掉模塊中相應(yīng)的地方
           //out.print(templateContent);
           // 根據(jù)時(shí)間得文件名
           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>

          本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/fbysss/archive/2006/03/07/618041.aspx


          本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/hitop0609/archive/2009/09/16/4555758.aspx

          posted on 2009-10-20 12:25 狂淘 閱讀(151) 評(píng)論(0)  編輯  收藏 所屬分類: jsp項(xiàng)目 傳到服務(wù)器問題
          主站蜘蛛池模板: 花莲市| 郎溪县| 潮州市| 荆门市| 泊头市| 浙江省| 隆安县| 常德市| 古浪县| 西乡县| 文昌市| 洛阳市| 东海县| 江孜县| 中牟县| 朝阳区| 施甸县| 邵武市| 拉孜县| 崇义县| 长治市| 双峰县| 屯门区| 夏津县| 宜宾县| 五大连池市| 普兰店市| 双柏县| 河北省| 玉环县| 宜宾市| 南充市| 元朗区| 清原| 铁力市| 西乌珠穆沁旗| 乌恰县| 凤山市| 榆林市| 治县。| 含山县|