• Java项目生成静态页面


    第一次做项目需要生成静态页面,网上很多大牛对将网页生成静态页面有很多异议。说一下我的看法。

    不外乎有以下因素: 1、从页面加载时间来看:静态页面不需要与数据库建立连接,尤其是访问数据量较大的页面,这种页面大多要查很多结果集,因此建立连接次数就增多了,时间不可观,而静态页面则省去了这些时间。 2、从便于搜索引擎抓取的角度来讲:搜索引擎更喜欢静态的网页,静态网页与动态网页相比,搜索引擎更喜欢静的,更便于抓取,搜索引擎SEO排名更容易提高,一些大门户站页面大多都采用静态或伪静态网页来显示,更便于搜索引擎抓取与排名。 3、从安全性来看:静态网页不宜遭到黑客攻击,因为黑客不知道你的网站的后台、网站采用程序、数据库的地址。 4、从稳定性来看:哪天数据库服务器挂了,动态网页就拜拜了!而要运行一个静态网页的发布服务器,相信大家都知道配置不是太高也行的吧?呵呵。

    因此,我认为,生成静态页面具有可行性。

    那么怎么把动态网页的代码生成静态网页呢?又存在哪呢?原理其实很简单。 1、利用Freemark模板生成静态页面,网上搜一下大把大把的代码随你挑,我就不在这里啰嗦了。 我很讨厌这种方式,因为对于一个数据量较大的页面来讲工作量太大,要写模板,语法又比较怪异,不流行! 2、也是我偶尔想起来的。用Java中URLConnection抓取某个URL网页源码(这是原理核心)生成html文件,就是这么简单!就是这么Easy!

    代码奉上!

    1)、以下是捕捉网页源码程序:

    1. import java.io.BufferedReader;  
    2. import java.io.File;  
    3. import java.io.IOException;  
    4. import java.io.InputStreamReader;  
    5. import java.net.MalformedURLException;  
    6. import java.net.URL;  
    7. import java.net.URLConnection;  
    8. import java.util.regex.Matcher;  
    9. import java.util.regex.Pattern;  
    10.   
    11. import org.apache.commons.io.FileUtils;  
    12. import org.apache.commons.lang.StringUtils;  
    13.   
    14. /** 
    15.  * @author Xing,XiuDong 
    16.  */  
    17. public class HTMLGenerator {  
    18.   
    19.     public static final String generate(final String url) {  
    20.         if (StringUtils.isBlank(url)) {  
    21.             return null;  
    22.         }  
    23.   
    24.         Pattern pattern = Pattern.compile("(http://|https://){1}[//w//.//-/:]+");  
    25.         Matcher matcher = pattern.matcher(url);  
    26.         if (!matcher.find()) {  
    27.             return null;  
    28.         }  
    29.   
    30.         StringBuffer sb = new StringBuffer();  
    31.   
    32.         try {  
    33.             URL _url = new URL(url);  
    34.             URLConnection urlConnection = _url.openConnection();  
    35.             BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));  
    36.   
    37.             String inputLine;  
    38.             while ((inputLine = in.readLine()) != null) {  
    39.                 sb.append(inputLine);  
    40.             }  
    41.         } catch (MalformedURLException e) {  
    42.             e.printStackTrace();  
    43.         } catch (IOException e) {  
    44.             e.printStackTrace();  
    45.         }  
    46.   
    47.         return sb.toString();  
    48.     }  
    49.   
    50.     /** 
    51.      * Test Code 
    52.      * Target : http://www.google.cn/ 
    53.      */  
    54.     public static void main(String[] args) throws IOException {  
    55.         String src = HTMLGenerator.generate("http://www.google.cn/");  
    56.   
    57.         File file = new File("C:" + File.separator + "index.html");  
    58.         FileUtils.writeStringToFile(file, src, "UTF-8");  
    59.     }  
    60.   
    61. }  

    2)、将源码写入Html文件,这个需要根据用户的需求了,我根据我项目中遇到的情况写了以下代码:(附测试程序:http://www.google.cn/

    1. /** 
    2.  * generite html source code 
    3.  *  
    4.  * @author Xing,XiuDong 
    5.  * @date 2009.06.22 
    6.  * @param request 
    7.  * @param url 
    8.  * @param toWebRoot 
    9.  * @param encoding 
    10.  * @throws IOException 
    11.  */  
    12. public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {  
    13.   
    14.     if (null == url) {  
    15.         url = request.getRequestURL().toString();  
    16.     }  
    17.   
    18.     String contextPath = request.getContextPath();  
    19.     String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);  
    20.   
    21.     String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);  
    22.     if (!ctxPath.endsWith(File.separator)) {  
    23.         ctxPath += File.separator;  
    24.     }  
    25.   
    26.     String filePath = StringUtils.substringAfter(url, contextPath);  
    27.     filePath = filePath.replaceAll("//.(do|jsp|html|shtml)$", ".html");  
    28.   
    29.     String savePath = "";  
    30.     String autoCreatedDateDir = "";  
    31.     if (!toWebRoot) {  
    32.         savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);  
    33.   
    34.         String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };  
    35.         autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));  
    36.   
    37.         filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";  
    38.     }  
    39.   
    40.     File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);  
    41.     FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);  
    42. }  

    文章出处:http://blog.csdn.net/xxd851116/archive/2009/06/24/4293239.aspx

  • 相关阅读:
    水平垂直居中的四种方法
    对js数组方法的部分了解以及面试内容
    第三阶段——jS——webApi编程——BOM
    对于VUE中slot的理解与学习
    第三阶段——jS——webApi编程——DOM
    初学node,关于使用req.body,打印的值为空解决方法
    第三阶段:javaScript 网页编程基本内容
    第三阶段——jS——webApi编程——移动端事件+JQuery+js面向对象
    2005129考勤登记
    200525考勤登记
  • 原文地址:https://www.cnblogs.com/dreamzhiya/p/3907936.html
Copyright © 2020-2023  润新知