采用JSP生成html网站页面

以下为转自:http://blog.csdn.net/ty110119120/archive/2009/02/13/3887277.aspx的一篇文章,留下来以备以后用。

通常是采用JSP生成内容可变的网站页面,这种访问方式就意味着每访问一次页面会调用后台重新生成HTML页面,这样就造成了效率低下。

    今天遇到一种技术,也是目前很多网站系统都采用的技术:先将JSP页面生成的HTML缓存起来,IE用户访问这些HTML页面就比访问JSP效率高很多。而这些HTML页面只需要通过后台调用方法更新,就能实现对前台被请求页面的更新。

    方法大致是通过HttpURLConnection类请求特定的页面资源并获取流,将流写入文件流生成.HTML文件。

下面贴一下JAVA代码:

 view plaincopy to clipboardprint?
package com.hndoc.util;  
 
import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.OutputStreamWriter;  
import java.net.HttpURLConnection;  
import java.net.URL;  
 
public class ContentOperation {  
      
    final static Object lock = new Object();  
    final static Object jsLock = new Object();  
      
    /** 
     * 将JSP转为HTML 
     * @param page      用于生成html页面的jsp的路径 
 
     * @param filePath   生成html文件的路径 
 
     * @param chartset   字符串编码 
 
     * @throws IOException 
     */ 
    public static void makeHtml(String page, String filePath, String chartset)  
            throws IOException {  
        synchronized (lock) {  
            HttpURLConnection huc = null;  
            BufferedReader br = null;  
            BufferedWriter bw = null;  
 
            huc = (HttpURLConnection) new URL(page).openConnection();  
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");  
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");  
            huc.connect();  
            InputStream stream = huc.getInputStream();  
            bw = new BufferedWriter(new OutputStreamWriter(  
                    new FileOutputStream(filePath), chartset));  
            br = new BufferedReader(new InputStreamReader(stream, chartset));  
            String line;  
            while ((line = br.readLine()) != null) {  
                if (line.trim().length() > 0) {  
                    bw.write(line);  
                    bw.newLine();  
                }  
            }  
            br.close();  
            bw.close();  
            huc.disconnect();  
        }  
    }  
 
    /** 
     * 产生栏目对应的JS脚本 
     * @param str 
     * @param filePath  文件路径 
     * @param chartset 字符编码 
     * @throws IOException 
     */ 
    public static void makeJs(String str , String filePath, String chartset)  
    throws IOException {  
        synchronized (jsLock) {  
            BufferedWriter bw = null;  
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), chartset));  
            if ((str!=null) && (str.trim().length() > 0)) {  
                bw.write(str);  
            }  
            bw.close();  
        }  
    }  
 

package com.hndoc.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class ContentOperation {
 
 final static Object lock = new Object();
 final static Object jsLock = new Object();
 
 /**
  * 将JSP转为HTML
  * @param page      用于生成html页面的jsp的路径

  * @param filePath   生成html文件的路径

  * @param chartset   字符串编码

  * @throws IOException
  */
 public static void makeHtml(String page, String filePath, String chartset)
   throws IOException {
  synchronized (lock) {
   HttpURLConnection huc = null;
   BufferedReader br = null;
   BufferedWriter bw = null;

   huc = (HttpURLConnection) new URL(page).openConnection();
   System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
   System.setProperty("sun.net.client.defaultReadTimeout", "30000");
   huc.connect();
   InputStream stream = huc.getInputStream();
   bw = new BufferedWriter(new OutputStreamWriter(
     new FileOutputStream(filePath), chartset));
   br = new BufferedReader(new InputStreamReader(stream, chartset));
   String line;
   while ((line = br.readLine()) != null) {
    if (line.trim().length() > 0) {
     bw.write(line);
     bw.newLine();
    }
   }
   br.close();
   bw.close();
   huc.disconnect();
  }
 }

 /**
  * 产生栏目对应的JS脚本
  * @param str
  * @param filePath  文件路径
  * @param chartset 字符编码
  * @throws IOException
  */
 public static void makeJs(String str , String filePath, String chartset)
 throws IOException {
  synchronized (jsLock) {
   BufferedWriter bw = null;
   bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), chartset));
   if ((str!=null) && (str.trim().length() > 0)) {
    bw.write(str);
   }
   bw.close();
  }
 }

}

    在生成HTML文件后,页面整体框架不会变,但可以通过JS控制页面数据。之后改变JS文件即可更改页面显示的数据。

JAVASCRIPT代码:

view plaincopy to clipboardprint?
var _article_array_1101=[{"title":"/u7F8E/u672F/u54C1/u7ECF/u8425/u5355/u4F4D/u5907/u6848/u60C5/u51B5/u767B/u8BB0/u8868","substring_title":"/u7F8E/u672F/u54C1/u7ECF/u8425/u5355/u4F4D/u5907/u6848/u60C5/u51B5/u767B/u8BB0/u8868","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000001","pub_time":"2009-02-09"},{"title":"/u8BBE/u7ACB/u4E92/u8054/u7F51/u6587/u5316/u5355/u4F4D/u7533/u8BF7","substring_title":"/u8BBE/u7ACB/u4E92/u8054/u7F51/u6587/u5316/u5355/u4F4D/u7533/u8BF7","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000002","pub_time":"2009-02-09"},{"title":"/u6E56/u5357/u6F14/u51FA/u7ECF/u7EAA/u673A/u6784/u7533/u62A5/u8868","substring_title":"/u6E56/u5357/u6F14/u51FA/u7ECF/u7EAA/u673A/u6784/u7533/u62A5/u8868","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000003","pub_time":"2009-02-09"},{"title":"/u5916/u56FD/u6587/u827A/u8868/u6F14/u56E2/u4F53/u6216/u4E2A/u4EBA/u6765/u6E58/u8FDB/u884C/u8425/u4E1A/u6027/u6F14/u51FA/u7533/u62A5/u8868","substring_title":"/u5916/u56FD/u6587/u827A/u8868/u6F14/u56E2/u4F53/u6216/u4E2A/u4EBA/u6765/u6E58/u8FDB...","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000004","pub_time":"2009-02-09"},{"title":"/u7F8E/u672F/u54C1/u7ECF/u8425/u5355/u4F4D/u5907/u6848/u60C5/u51B5/u767B/u8BB0/u8868","substring_title":"/u7F8E/u672F/u54C1/u7ECF/u8425/u5355/u4F4D/u5907/u6848/u60C5/u51B5/u767B/u8BB0/u8868","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000005","pub_time":"2009-02-09"},{"title":"/u4E2D/u5916/u5408/u8D44/u3001/u4E2D/u5916/u5408/u4F5C/u6F14/u51FA/u573A/u6240/u7ECF/u8425/u5355/u4F4D/u5E74/u5EA6/u62A5/u8868","substring_title":"/u4E2D/u5916/u5408/u8D44/u3001/u4E2D/u5916/u5408/u4F5C/u6F14/u51FA/u573A/u6240/u7ECF...","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000006","pub_time":"2009-02-09"},{"title":"/u8BBE/u7ACB/u4E92/u8054/u7F51/u6587/u5316/u5355/u4F4D/u7533/u8BF7","substring_title":"/u8BBE/u7ACB/u4E92/u8054/u7F51/u6587/u5316/u5355/u4F4D/u7533/u8BF7","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000007","pub_time":"2009-02-09"}] 
var _article_array_1101=[{"title":"/u7F8E/u672F/u54C1/u7ECF/u8425/u5355/u4F4D/u5907/u6848/u60C5/u51B5/u767B/u8BB0/u8868","substring_title":"/u7F8E/u672F/u54C1/u7ECF/u8425/u5355/u4F4D/u5907/u6848/u60C5/u51B5/u767B/u8BB0/u8868","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000001","pub_time":"2009-02-09"},{"title":"/u8BBE/u7ACB/u4E92/u8054/u7F51/u6587/u5316/u5355/u4F4D/u7533/u8BF7","substring_title":"/u8BBE/u7ACB/u4E92/u8054/u7F51/u6587/u5316/u5355/u4F4D/u7533/u8BF7","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000002","pub_time":"2009-02-09"},{"title":"/u6E56/u5357/u6F14/u51FA/u7ECF/u7EAA/u673A/u6784/u7533/u62A5/u8868","substring_title":"/u6E56/u5357/u6F14/u51FA/u7ECF/u7EAA/u673A/u6784/u7533/u62A5/u8868","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000003","pub_time":"2009-02-09"},{"title":"/u5916/u56FD/u6587/u827A/u8868/u6F14/u56E2/u4F53/u6216/u4E2A/u4EBA/u6765/u6E58/u8FDB/u884C/u8425/u4E1A/u6027/u6F14/u51FA/u7533/u62A5/u8868","substring_title":"/u5916/u56FD/u6587/u827A/u8868/u6F14/u56E2/u4F53/u6216/u4E2A/u4EBA/u6765/u6E58/u8FDB...","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000004","pub_time":"2009-02-09"},{"title":"/u7F8E/u672F/u54C1/u7ECF/u8425/u5355/u4F4D/u5907/u6848/u60C5/u51B5/u767B/u8BB0/u8868","substring_title":"/u7F8E/u672F/u54C1/u7ECF/u8425/u5355/u4F4D/u5907/u6848/u60C5/u51B5/u767B/u8BB0/u8868","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000005","pub_time":"2009-02-09"},{"title":"/u4E2D/u5916/u5408/u8D44/u3001/u4E2D/u5916/u5408/u4F5C/u6F14/u51FA/u573A/u6240/u7ECF/u8425/u5355/u4F4D/u5E74/u5EA6/u62A5/u8868","substring_title":"/u4E2D/u5916/u5408/u8D44/u3001/u4E2D/u5916/u5408/u4F5C/u6F14/u51FA/u573A/u6240/u7ECF...","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000006","pub_time":"2009-02-09"},{"title":"/u8BBE/u7ACB/u4E92/u8054/u7F51/u6587/u5316/u5355/u4F4D/u7533/u8BF7","substring_title":"/u8BBE/u7ACB/u4E92/u8054/u7F51/u6587/u5316/u5355/u4F4D/u7533/u8BF7","url":"OaOnline.do?action=OaOnlineDetail&oa_id=000000000000000007","pub_time":"2009-02-09"}]

HTML代码:

 view plaincopy to clipboardprint?
<script src="./contentjs/portal/1101.js"></script>                          <script>                          if("undefined" != typeof _article_array_1101)                           for(i=0;i<7;i++)                         {                               var dd=_article_array_1101[i];                              if("undefined" == typeof dd){                                   break;                              }                               document.write("<li><a href='"+dd['url']+"' target='_blank' >·"+dd['substring_title']);                             document.write("</a></li>");                            }                           </script> 
<script src="./contentjs/portal/1101.js"></script>       <script>       if("undefined" != typeof _article_array_1101)       for(i=0;i<7;i++)       {        var dd=_article_array_1101[i];        if("undefined" == typeof dd){         break;        }        document.write("<li><a href='"+dd['url']+"' target='_blank' >·"+dd['substring_title']);        document.write("</a></li>");       }       </script>  

-------------------------------------分割线-----------------------------------------------

    后台调用方法,生成装载数据的JS文件,即可完成页面数据更新。 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ty110119120/archive/2009/02/13/3887277.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值