小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Java 項(xiàng)目生成靜態(tài)頁面的代碼

 CevenCheng 2011-03-14
第一次做項(xiàng)目需要生成靜態(tài)頁面,網(wǎng)上很多大牛對(duì)將網(wǎng)頁生成靜態(tài)頁面有很多異議。說一下我的看法。
不外乎有以下因素: 
1、從頁面加載時(shí)間來看:靜態(tài)頁面不需要與數(shù)據(jù)庫建立連接,尤其是訪問數(shù)據(jù)量較大的頁面,這種頁面大多要查很多結(jié)果集,因此建立連接次數(shù)就增多了,時(shí)間不可觀,而靜態(tài)頁面則省去了這些時(shí)間。 
2、從便于搜索引擎抓取的角度來講:搜索引擎更喜歡靜態(tài)的網(wǎng)頁,靜態(tài)網(wǎng)頁與動(dòng)態(tài)網(wǎng)頁相比,搜索引擎更喜歡靜的,更便于抓取,搜索引擎SEO排名更容易提高,一些大門戶站頁面大多都采用靜態(tài)或偽靜態(tài)網(wǎng)頁來顯示,更便于搜索引擎抓取與排名。 
3、從安全性來看:靜態(tài)網(wǎng)頁不宜遭到攻擊,因?yàn)椴恢滥愕木W(wǎng)站的后臺(tái)、網(wǎng)站采用程序、數(shù)據(jù)庫的地址。 
4、從穩(wěn)定性來看:哪天數(shù)據(jù)庫服務(wù)器掛了,動(dòng)態(tài)網(wǎng)頁就拜拜了!而要運(yùn)行一個(gè)靜態(tài)網(wǎng)頁的發(fā)布服務(wù)器,相信大家都知道配置不是太高也行的吧?呵呵。 

因此,我認(rèn)為,生成靜態(tài)頁面具有可行性。 

那么怎么把動(dòng)態(tài)網(wǎng)頁的代碼生成靜態(tài)網(wǎng)頁呢?又存在哪呢?原理其實(shí)很簡單。 
1、利用Freemark模板生成靜態(tài)頁面,網(wǎng)上搜一下大把大把的代碼隨你挑,我就不在這里啰嗦了。 
我很討厭這種方式,因?yàn)閷?duì)于一個(gè)數(shù)據(jù)量較大的頁面來講工作量太大,要寫模板,語法又比較怪異,不流行! 
2、也是我偶爾想起來的。用Java中URLConnection抓取某個(gè)URL網(wǎng)頁源碼(這是原理核心)生成html文件,就是這么簡單!就是這么Easy! 

代碼奉上! 

1)、以下是捕捉網(wǎng)頁源碼程序: 
復(fù)制代碼 代碼如下:

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文件,這個(gè)需要根據(jù)用戶的需求了,我根據(jù)我項(xiàng)目中遇到的情況寫了以下代碼:
復(fù)制代碼 代碼如下:

/** 
* 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); 
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多