- 浏览: 719842 次
- 性别:
- 来自: 湖北
文章分类
最新评论
-
SE_XiaoFeng:
用mysqldump命令行导出。这个报错唉。错误提示信息如下: ...
linux下如何导入导出MySQL数据库 -
SE_XiaoFeng:
文章写的干脆了当,我喜欢!
linux下如何导入导出MySQL数据库 -
niky6688:
网站咋打不开呢
beckham herms birki ...
【原创】上周给公司新做了一个网站,请大家审阅! -
niky6688:
哈哈
new chanel bags 2012
burbe ...
今天我抢了一个咪咪??? -
ydsakyclguozi:
...
jsp资源管理器也可能是木马
需转载,请注明转载出处!本文地址:
http://blog.csdn.net/xxd851116/archive/2009/06/24/4293239.aspx
第一次做项目需要生成静态页面,网上很多大牛对将网页生成静态页面有很多异议。说一下我的看法。
不外乎有以下因素:
1、从页面加载时间来看:静态页面不需要与数据库建立连接,尤其是访问数据量较大的页面,这种页面大多要查很多结果集,因此建立连接次数就增多了,时间不可观,而静态页面则省去了这些时间。
2、从便于搜索引擎抓取的角度来讲:搜索引擎更喜欢静态的网页,静态网页与动态网页相比,搜索引擎更喜欢静的,更便于抓取,搜索引擎SEO排名更容易提高,一些大门户站页面大多都采用静态或伪静态网页来显示,更便于搜索引擎抓取与排名。
3、从安全性来看:静态网页不宜遭到黑客攻击,因为黑客不知道你的网站的后台、网站采用程序、数据库的地址。
4、从稳定性来看:哪天数据库服务器挂了,动态网页就拜拜了!而要运行一个静态网页的发布服务器,相信大家都知道配置不是太高也行的吧?呵呵。
因此,我认为,生成静态页面具有可行性。
那么怎么把动态网页的代码生成静态网页呢?又存在哪呢?原理其实很简单。
1、利用Freemark模板生成静态页面,网上搜一下大把大把的代码随你挑,我就不在这里啰嗦了。
我很讨厌这种方式,因为对于一个数据量较大的页面来讲工作量太大,要写模板,语法又比较怪异,不流行!
2、也是我偶尔想起来的。用Java中URLConnection抓取某个URL网页源码(这是原理核心)生成html文件,就是这么简单!就是这么Easy!
代码奉上!
1)、以下是捕捉网页源码程序:
view plaincopy to clipboardprint?
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");
}
}
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/)
view plaincopy to clipboardprint?
/**
* generite html source code
*
* @author Xing,XiuDong
* @date 2009.06.22
* @param request
* @param url
* @param toWebRoot
* @param encoding
* @throws IOException
*/
public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {
if (null == url) {
url = request.getRequestURL().toString();
}
String contextPath = request.getContextPath();
String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);
String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
if (!ctxPath.endsWith(File.separator)) {
ctxPath += File.separator;
}
String filePath = StringUtils.substringAfter(url, contextPath);
filePath = filePath.replaceAll("\\.(do|jsp|html|shtml)$", ".html");
String savePath = "";
String autoCreatedDateDir = "";
if (!toWebRoot) {
savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);
String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));
filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
}
File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
}
/**
* generite html source code
*
* @author Xing,XiuDong
* @date 2009.06.22
* @param request
* @param url
* @param toWebRoot
* @param encoding
* @throws IOException
*/
public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {
if (null == url) {
url = request.getRequestURL().toString();
}
String contextPath = request.getContextPath();
String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);
String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
if (!ctxPath.endsWith(File.separator)) {
ctxPath += File.separator;
}
String filePath = StringUtils.substringAfter(url, contextPath);
filePath = filePath.replaceAll("\\.(do|jsp|html|shtml)$", ".html");
String savePath = "";
String autoCreatedDateDir = "";
if (!toWebRoot) {
savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);
String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));
filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
}
File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
}
发表于 @ 2009年06月24
http://blog.csdn.net/xxd851116/archive/2009/06/24/4293239.aspx
第一次做项目需要生成静态页面,网上很多大牛对将网页生成静态页面有很多异议。说一下我的看法。
不外乎有以下因素:
1、从页面加载时间来看:静态页面不需要与数据库建立连接,尤其是访问数据量较大的页面,这种页面大多要查很多结果集,因此建立连接次数就增多了,时间不可观,而静态页面则省去了这些时间。
2、从便于搜索引擎抓取的角度来讲:搜索引擎更喜欢静态的网页,静态网页与动态网页相比,搜索引擎更喜欢静的,更便于抓取,搜索引擎SEO排名更容易提高,一些大门户站页面大多都采用静态或伪静态网页来显示,更便于搜索引擎抓取与排名。
3、从安全性来看:静态网页不宜遭到黑客攻击,因为黑客不知道你的网站的后台、网站采用程序、数据库的地址。
4、从稳定性来看:哪天数据库服务器挂了,动态网页就拜拜了!而要运行一个静态网页的发布服务器,相信大家都知道配置不是太高也行的吧?呵呵。
因此,我认为,生成静态页面具有可行性。
那么怎么把动态网页的代码生成静态网页呢?又存在哪呢?原理其实很简单。
1、利用Freemark模板生成静态页面,网上搜一下大把大把的代码随你挑,我就不在这里啰嗦了。
我很讨厌这种方式,因为对于一个数据量较大的页面来讲工作量太大,要写模板,语法又比较怪异,不流行!
2、也是我偶尔想起来的。用Java中URLConnection抓取某个URL网页源码(这是原理核心)生成html文件,就是这么简单!就是这么Easy!
代码奉上!
1)、以下是捕捉网页源码程序:
view plaincopy to clipboardprint?
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");
}
}
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/)
view plaincopy to clipboardprint?
/**
* generite html source code
*
* @author Xing,XiuDong
* @date 2009.06.22
* @param request
* @param url
* @param toWebRoot
* @param encoding
* @throws IOException
*/
public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {
if (null == url) {
url = request.getRequestURL().toString();
}
String contextPath = request.getContextPath();
String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);
String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
if (!ctxPath.endsWith(File.separator)) {
ctxPath += File.separator;
}
String filePath = StringUtils.substringAfter(url, contextPath);
filePath = filePath.replaceAll("\\.(do|jsp|html|shtml)$", ".html");
String savePath = "";
String autoCreatedDateDir = "";
if (!toWebRoot) {
savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);
String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));
filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
}
File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
}
/**
* generite html source code
*
* @author Xing,XiuDong
* @date 2009.06.22
* @param request
* @param url
* @param toWebRoot
* @param encoding
* @throws IOException
*/
public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {
if (null == url) {
url = request.getRequestURL().toString();
}
String contextPath = request.getContextPath();
String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);
String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
if (!ctxPath.endsWith(File.separator)) {
ctxPath += File.separator;
}
String filePath = StringUtils.substringAfter(url, contextPath);
filePath = filePath.replaceAll("\\.(do|jsp|html|shtml)$", ".html");
String savePath = "";
String autoCreatedDateDir = "";
if (!toWebRoot) {
savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);
String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));
filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
}
File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
}
发表于 @ 2009年06月24
发表评论
-
属性文件的读取 Java
2010-12-11 08:51 1161import java.util.ResourceBu ... -
【珍藏版】微软等数据结构+算法面试100题全部出炉[100题首次完整亮相]
2010-12-06 16:33 2835转至: http://blog.csdn.net/v_JULY ... -
【原创】Log4j 日志文件 使用
2010-12-02 11:37 1189log4j.properties 代码 log4j.ro ... -
【精华】保护自己的网站资源
2010-11-19 08:03 1166禁止用“另存为”保存页面 <head> < ... -
【精华】Window.Open()网页弹出窗口大全
2010-11-16 18:01 1509【1、最基本的弹出窗口代码】 <!--<br ... -
【精华】页面右下角弹出类似QQ或MSN的消息提示
2010-11-05 16:36 982具体实现见 附件 主要是 针对 在框架中 如何实现 后下 ... -
【原创】苦恼了1个星期的DIV 滚动条置底, 今天终于实现了!!!
2010-11-05 10:58 3410<script type="text/ja ... -
【急救......】Could not find the main class.program will exit
2010-11-01 17:23 1593Java中Could not find the main cl ... -
屏蔽 F5 刷新键
2010-10-28 11:09 735<script language="JavaS ... -
web页面中按ESC关闭窗口
2010-10-28 11:04 1370<script language="jav ... -
web页面中如果想让用户复制页面内容粘贴时加入一些自定义信息
2010-10-28 11:04 797如果让用户复制你网站上的信息到别处粘贴时,附加自定义信息可以采 ... -
防止页面被多次提交
2010-10-28 11:03 18311. javascript ,设置一个变量,只允许提交一 ... -
Javascript打印代码
2010-10-18 14:38 863Javascript打印代码 实现打印功能的javasc ... -
【原创】struts+mysql+jsp 分页源码
2010-09-16 10:42 875具体代码见附件 一起学习,xf.zhouwenjun@163 ... -
【原创】struts 动态 设置radio 与 select 的默认值
2010-08-06 13:10 1605运行效果见 附件,有好的解决方法请分享xf.zhouwenju ... -
【原创】平时编码中积累的,非常好用,且很全的【时间工具类】
2010-07-21 17:42 862具体java类 请下载附件!烦请大家一起完善完善,请给我也分享 ... -
【原创】苦苦寻找的: ajax 定时读取数据,无刷新在页面显示
2010-07-19 17:17 988demo 见附件 -
【绝】在web工程下 调用js 日历控件 报错【missing ( before function parameters)
2010-07-09 11:28 1064报错代码 function document.onclick( ... -
【精髓】非常好用的日历控件,可以自由的选择日期 时分秒
2010-07-09 10:57 17111、Calendar.js代码如下 省略! 具体内容 ... -
JS获取Select多选的值
2010-07-07 15:50 3530JS获取Select多选的值 DropDownLis ...
相关推荐
java CMS 生成静态页面 通过后台发布来改变网站内容
经常在网上搜东西都搜不到,更有人上传点没用了,现在我解决了就把解决方法上传上吧.
java 生成静态页面 工具类java 生成静态页面 工具类
完整的asp生成静态页面 asp生成静态页面,包括数据的添加、删除、修改,webeditor的使用和一些简单的js的使用根目录下index.htm页面为生成的首页asp文件夹 index.htm为后台操作页面 createhtml.asp为生成所有新闻静态...
java整站静态生成方案一 实例工程源码java整站静态生成方案一 实例工程源码java整站静态生成方案一 实例工程源码java整站静态生成方案一 实例工程源码java整站静态生成方案一 实例工程源码java整站静态生成方案一 ...
动态页面生成静态页面实例动态页面生成静态页面实例动态页面生成静态页面实例动态页面生成静态页面实例
从数据库取出数据直接生成静态页 /* * ToHtml.java * author : stone * 动态生成静态插件V1.0 * 返回0 表示生成成功 * 返回-1表示生成失败 * savePath 就是tomcat中的webapp文件夹所在的目录 */ 调用例子: ...
java/jsp生成静态页面html/htm
Freemark+struts2 Freemark Java项目Freemark生成静态页面及语法
Java 生成静态页 代码 把动态页生成静态页
java动态生成静态网页技术.
Java生成静态页面
这个程序利用java抓取网页内容,然后生成生成静态页面
JAVA根据HTML模板生成静态页面静态页面静态页面
java实现的静态页面技术,含源码和数据库.
jsp生成静态页面 生成静态页面 完全可以运行的例子!!!!
PHP生成静态页面详解
伪静态页面生成技术|静态页面|生成技术|让你做网能更快的被收录!seo优化
自动生成静态页面
生成静态页面并有分页效果 源码 生成静态页面并有分页效果 源码