咱們開發電商網站的時候,須要把網站的一些訪問量比較大,頁面內容不須要更新的頁面進行靜態化處理來減小數據庫的壓力。php
咱們用的wget生成靜態頁面,可是這樣方式是要在服務器上執行命令,並在服務器上建一個定時器每隔一段時間去更新靜態頁面,使用頁面內容得以更新,這樣方式是一種較爲簡便的方法(腳主要靠腳本)腳本以下:css
#!/bin/bash ####################################################### # 頁面靜態緩存控制 pagecache.sh # ####################################################### # # $1 指定頁面 # pagecache.sh all #所有(默認) # pagecache.sh index.php #僅首頁 # # -h 指定網站根路徑(URL) # pagecache.sh -h http://b2b.ccb.com # [注意] 請保證本機可訪問相應域名或IP,可以使用本地hosts進行解析 # # -p 指定WEB路徑(webroot) # pagecache.sh -p /home/ecp/ecp/php/web # # -c 清除緩存(刪除html文件) # #------------------------------------------------------ # 須要靜態化的頁面集合 PAGES=("sparkant/sparkant.xhtml?h=165") HTMLPAGES=("sparkant.html") PAGE="all" SiteURL="http://127.0.0.1:8080/sparkant" SitePath="/data/app/tomcat-7.0.59/webapps/sparkant/static" WGeter="/usr/bin/wget" ClearPage=0 # 命令行參數 while [ $# -gt 0 ] do if [ "$1" == "-h" ]; then shift if [[ $# -gt 0 && "$1" != "" ]]; then SiteURL=$1 fi elif [ "$1" == "-p" ]; then shift if [[ $# -gt 0 && "$1" != "" ]]; then SitePath=$1 fi elif [ "$1" == "-c" ]; then ClearPage=1 else PAGE=$1 fi shift done # 檢查是否安裝了wget if [[ ! -x "$WGeter" ]]; then echo "wget not exist or not execute permission" exit; fi cd $SitePath #for P in ${PAGES[@]}; do for((i=0;i<${#HTMLPAGES[@]}; i++)) do if [[ "$P" == "$PAGE" || "$PAGE" == "all" ]]; then #HtmlPage=${P/\.jsp/\.htm} HtmlPage=${HTMLPAGES[i]} HtmlPage=${HtmlPage/_dyc/} if [ "$ClearPage" != "0" ]; then # 清除頁面靜態緩存 echo "...clear $HtmlPage..."; rm -rf $HtmlPage && echo "[ ok ]" continue; fi # 備份歸檔只保留7天 cutdate=`date +%Y%m%d --date='1 days ago'` tarfile="$HtmlPage.$cutdate.tar" if [ -f "$tarfile" ]; then rm -rf "$tarfile" echo "...delete $tarfile..." fi # 備份當前靜態緩存 echo "...backup $HtmlPage to $HtmlPage.$ctime..."; ctime=`date +"%Y%m%d_%H%M%S"` cdate=`date +"%Y%m%d"` rm -rf "$HtmlPage.$ctime" cp -f $HtmlPage "$HtmlPage.$ctime" if [ -f "$HtmlPage.$ctime" ]; then # 歸檔備份 tar rf "$HtmlPage.$cdate.tar" "$HtmlPage.$ctime" rm -rf "$HtmlPage.$ctime" fi # 生成靜態緩存 echo "...wget $HtmlPage to $HtmlPage.tmp..."; HtmlTmp="$HtmlPage.tmp" rm -rf $HtmlTmp # wget 動態頁面html內容到臨時文件 #$WGeter $SiteURL/$P -O $HtmlTmp || continue $WGeter $SiteURL/${PAGES[i]} -O $HtmlTmp || continue if [[ -f $HtmlTmp && -s $HtmlTmp ]]; then # 用臨時文件替換靜態頁面 echo "...copy $HtmlPage.tmp to $HtmlPage..."; cp -f $HtmlTmp $HtmlPage || continue chmod 0755 $HtmlPage echo "[ ok ]" fi fi done
可是若是我訪問的頁面不存在的時候,不能及時的生成靜態頁面。下面我本身簡單實現了一個靜態頁面引擎工具類。html
package cn.sparkant.utils; import org.apache.http.HttpEntity; import org.apache.http.StatusLine; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.log4j.Logger; import java.io.*; import java.net.HttpURLConnection; /** 靜態頁面引擎技術工具類 * Created by hjs on 16/5/18. */ public class HtmlGenerator { private static final Logger logger = Logger.getLogger(HtmlGenerator.class); public static boolean createHtmlPage(String url, String htmlFileName) { boolean status = false; try { //建立一個HttpClient實例充當模擬瀏覽器 CloseableHttpClient httpClient = HttpClients.createDefault(); //建立GET方法的實例 HttpGet get = new HttpGet(url); //設置Get方法提交參數時使用的字符集,以支持中文參數的正常傳遞 get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); //5.執行get方法獲得服務器的返回的全部的數據,都存到response中。 CloseableHttpResponse response = httpClient.execute(get); //6.httpclient 訪問服務器返回的表頭,包含http狀態碼 StatusLine statusLine = response.getStatusLine(); //7.獲得狀態碼 int code = statusLine.getStatusCode(); if(code== HttpURLConnection.HTTP_OK) {//若是鏈接成功 //8.得到數據實體 HttpEntity entity = response.getEntity(); //9.得到輸入流 InputStream is = entity.getContent(); //此方法默認會亂碼,通過長時期的摸索,下面的方法才能夠 BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sb = new StringBuffer(); String line = null; String page = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); page = sb.toString().replaceAll("/css","http://shop.zj96596.com/css") .replaceAll("/js","http://shop.zj96596.com/js"); //將解析結果寫入指定的靜態HTML文件中,實現靜態HTML生成 writeHtml(url, htmlFileName, page); status = true; }else { logger.info("靜態頁面引擎在解析" + url + "產生靜態頁面" + htmlFileName + "時出錯!"); } httpClient.close(); response.close(); } catch(Exception e){ logger.error("HtmlGenerator 生成靜態頁面失敗", e); } finally { } return status; } //將解析結果寫入指定的靜態HTML文件中 private synchronized static void writeHtml(String url,String htmlFileName,String content) throws Exception{ //BufferedWriter fw = new BufferedWriter(new FileWriter(htmlFileName)); OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(htmlFileName),"UTF-8"); fw.write(content); fw.close(); logger.info("靜態頁面引擎在解析" +url+ "產生靜態頁面" + htmlFileName + "成功"); } //測試方法 public static void main(String[] args){ HtmlGenerator h = new HtmlGenerator(); h.createHtmlPage("http://baidu.com","/Users/hjs/Desktop/a.html"); System.out.println("靜態頁面已經生成到c:/a.html"); } }
在配置攔截器,當訪問的頁面不存在返回404時,判斷該路徑是否是咱們要生成的靜態頁面。java
package cn.sparkant.webapp.fliter; import cn.sparkant.utils.HtmlGenerator; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /**404頁面錯誤 * 當訪問的指定靜態頁面不存在時,經過靜態頁面引擎技術工具類生成靜態頁面 * Created by hjs on 16/5/19. */ public class HtmlFliter implements Filter{ public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; String url = request.getRequestURI().toString(); HttpServletResponse response = (HttpServletResponse) servletResponse; filterChain.doFilter(request, response); int statusCode = response.getStatus(); if (statusCode == 404) { if (..) { //判斷url是不是咱們要生成的靜態頁面 HtmlGenerator.createHtmlPage("http://shop.zj96596.com", "/Users/hjs/Desktop/a.html"); response.setStatus(HttpServletResponse.SC_OK); //response.sendRedirect("http://shop..com"); } } } public void destroy() { } }
後面你們能夠本身優化改進, 有什麼不足的能夠指正。web