Java 項目產生靜態頁面的代碼

來源:互聯網
上載者:User

不外乎有以下因素:
1、從頁面載入時間來看:靜態頁面不需要與資料庫建立串連,尤其是訪問資料量較大的頁面,這種頁面大多要查很多結果集,因此建立串連次數就增多了,時間不可觀,而靜態頁面則省去了這些時間。
2、從便於搜尋引擎抓取的角度來講:搜尋引擎更喜歡靜態網頁,靜態網頁與動態網頁相比,搜尋引擎更喜歡靜的,更便於抓取,搜尋引擎SEO排名更容易提高,一些大門戶站頁面大多都採用靜態或偽靜態網頁來顯示,更便於搜尋引擎抓取與排名。
3、從安全性來看:靜態網頁不宜遭到駭客攻擊,因為駭客不知道你的網站的後台、網站採用程式、資料庫的地址。
4、從穩定性來看:哪天資料庫伺服器掛了,動態網頁就拜拜了!而要運行一個靜態網頁的發行伺服器,相信大家都知道配置不是太高也行的吧?呵呵。

因此,我認為,產生靜態頁面具有可行性。

那麼怎麼把動態網頁的代碼產生靜態網頁呢?又存在哪呢?原理其實很簡單。
1、利用Freemark模板產生靜態頁面,網上搜一下大把大把的代碼隨你挑,我就不在這裡囉嗦了。
我很討厭這種方式,因為對於一個資料量較大的頁面來講工作量太大,要寫模板,文法又比較怪異,不流行!
2、也是我偶爾想起來的。用Java中URLConnection抓取某個URL網頁源碼(這是原理核心)產生html檔案,就是這麼簡單!就是這麼Easy!

代碼奉上!

1)、以下是捕捉網頁源碼程式: 複製代碼 代碼如下: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檔案,這個需要根據使用者的需求了,我根據我項目中遇到的情況寫了以下代碼:複製代碼 代碼如下: /**
* 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);
}

來源:http://blog.csdn.net/xxd851116

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.