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

不外乎有以下因素: 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. import org.apache.commons.io.FileUtils;
  11. import org.apache.commons.lang.StringUtils;
  12. /**
  13. * @author Xing,XiuDong
  14. */
  15. public class HTMLGenerator {
  16. public static final String generate(final String url) {
  17. if (StringUtils.isBlank(url)) {
  18. return null;
  19. }
  20. Pattern pattern = Pattern.compile("(http://|https://){1}[//w//.//-/:]+");
  21. Matcher matcher = pattern.matcher(url);
  22. if (!matcher.find()) {
  23. return null;
  24. }
  25. StringBuffer sb = new StringBuffer();
  26. try {
  27. URL _url = new URL(url);
  28. URLConnection urlConnection = _url.openConnection();
  29. BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  30. String inputLine;
  31. while ((inputLine = in.readLine()) != null) {
  32. sb.append(inputLine);
  33. }
  34. } catch (MalformedURLException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. return sb.toString();
  40. }
  41. /**
  42. * Test Code
  43. * Target : http://www.google.cn/
  44. */
  45. public static void main(String[] args) throws IOException {
  46. String src = HTMLGenerator.generate("http://www.google.cn/");
  47. File file = new File("C:" + File.separator + "index.html");
  48. FileUtils.writeStringToFile(file, src, "UTF-8");
  49. }
  50. }

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
* @author Xing,XiuDong
*/
public class HTMLGenerator {

public static final String generate(final String url) {
if (StringUtils.isBlank(url)) {
return null;
}

Pattern pattern = Pattern.compile("(http://|https://){1}[//w//.//-/:]+");
Matcher matcher = pattern.matcher(url);
if (!matcher.find()) {
return null;
}

StringBuffer sb = new StringBuffer();

try {
URL _url = new URL(url);
URLConnection urlConnection = _url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return sb.toString();
}

/**
* Test Code
* Target : http://www.google.cn/
*/
public static void main(String[] args) throws IOException {
String src = HTMLGenerator.generate("http://www.google.cn/");

File file = new File("C:" + File.separator + "index.html");
FileUtils.writeStringToFile(file, src, "UTF-8");
}

}

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. if (null == url) {
  14. url = request.getRequestURL().toString();
  15. }
  16. String contextPath = request.getContextPath();
  17. String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);
  18. String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
  19. if (!ctxPath.endsWith(File.separator)) {
  20. ctxPath += File.separator;
  21. }
  22. String filePath = StringUtils.substringAfter(url, contextPath);
  23. filePath = filePath.replaceAll("//.(do|jsp|html|shtml)$", ".html");
  24. String savePath = "";
  25. String autoCreatedDateDir = "";
  26. if (!toWebRoot) {
  27. savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);
  28. String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
  29. autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));
  30. filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
  31. }
  32. File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
  33. FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
  34. }

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

Java项目生成静态页面的更多相关文章

  1. 浅谈php生成静态页面

    一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...

  2. C#根据网址生成静态页面

    HoverTree开源项目中HoverTreeWeb.HVTPanel的Index.aspx文件 是后台管理的首页. 包含生成留言板首页,以及显示用户名,退出等功能. 根据网址生成页面的方法: boo ...

  3. asp.net配置全局应用程序类 巧妙达到定时生成静态页面

    //在项目里添加一个"全局应用程序类(Global Application Class)",在里面写这样的代码: public class Global : System.Web. ...

  4. Django之使用celery和NGINX生成静态页面实现性能优化

    性能优化原理: 当我们要给client浏览器返回一个页面时,我们需要去数据库查询数据并将数据和基本页面模板渲染形成页面返回给客户端,但如果每一个用户访问时都去查询一次首页的的数据时,当日访问量很大时那 ...

  5. 过滤器为JSP文件生成静态页面

    用过滤器为JSP文件生成静态页面,这只是一个简单的实例,客户端浏览器第一次请求JSP页面时,服务器将生成对应的HTML文件,以后访问同一JSP文件,将转为访问生成的HTML文件.一.过滤器 packa ...

  6. ThinkPHP 3.2 生成静态页面

    1:在根目录下的全局index.php中加下面这行: define('HTML_PATH', './htm');//生成静态页面的文件位置 2:在项目的配置文件config.php中加下面这行: 'H ...

  7. .NET生成静态页面并分页

    因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序. 主要的原理就是替换模板里的特殊字符. 1.静态 ...

  8. .net 生成 静态页面

    .net 生成 静态页面 <!--Main.Aspx--> <%@ page language="C#" %> <%@ import namespac ...

  9. 用 Smarty 生成静态页面入门介绍

    why Smarty? 随着公司首页(以下简称首页)流量越来越大,最近开始考虑使用后台语言生成静态页面的技术. 我们知道,一个简单页面一般是一个 .html(或者 .htm ..shtml)后缀的文件 ...

随机推荐

  1. 第十二章——SQLServer统计信息(3)——发现过期统计信息并处理

    原文:第十二章--SQLServer统计信息(3)--发现过期统计信息并处理 前言: 统计信息是关于谓词中的数据分布的主要信息源,如果不知道具体的数据分布,优化器不能获得预估的数据集,从而不能统计需要 ...

  2. capturing self strongly in this block is likely to lead to a retain cycle

    一个用Block实例变量语法,当有一个参考的实例变量,常引起retain cycle. capturing self strongly in this block is likely to lead ...

  3. onethink和phpwind共享

    将onethink和phpwind数据库安装在一起.使用公用表前缀. 将onethink的member表点phpwind有user表 这是onethink在根文件夹的安装,phpwind安装在bbs的 ...

  4. git-push(1) Manual Page

    git-push(1) Manual Page NAME git-push - Update remote refs along with associated objects SYNOPSIS gi ...

  5. js 网上见到的动画函数 备份

    <script> function startMove(obj,json,fn){ clearInterval(obj.timer); obj.timer = setInterval(fu ...

  6. python基础课程_学习笔记13:标准库:有些收藏夹——sys

    标准库:有些收藏夹 sys sys这个模块可以让你访问和python解释器联系紧密的变量和函数. sys模块中一些重要的函数和变量 函数/变量 描写叙述 argv 命令行參数,包含脚本名称 exit( ...

  7. Preemption Context Switches 和 Synchronization Context Switches

    Preemption Context Switches测量操作系统任务调度线程处理器上执行的次数,以及切换到较高-priority螺纹,数. Synchronization context switc ...

  8. [wxWidgets]_[0基础]_[经常更新进度条程序]

    场景: 1. 非常根据程序的进展需要处理业务,以更新进度条,进度条的目的是为了让用户知道业务流程的进度.一个进度条程序更友好,让用户知道在程序执行.不是没有反应. 2. 现在更新见过这两种方法的进展. ...

  9. 查询(Query)和标识(Identify)

    查询(Query)和标识(Identify) 相关文章:RESTful API URI 设计的一些总结. 问题场景:删除一个资源(Resources),URI 该如何设计? 应用示例:删除名称为 iP ...

  10. CentOS 7 下安装 LEMP 服务(nginx、MariaDB/MySQL 和 php)

    原文 CentOS 7 下安装 LEMP 服务(nginx.MariaDB/MySQL 和 php) LEMP 组合包是一款日益流行的网站服务组合软件包,在许多生产环境中的核心网站服务上起着强有力的作 ...