比較ASP產生靜態HTML檔案的幾種方法

來源:互聯網
上載者:User

將動態網頁面轉換產生靜態Html檔案有許多好處,比如產生html網頁有利於被搜尋引擎收錄(特別是對接受動態參數的頁面)。前台訪問時,脫離了資料訪問,減輕對資料庫訪問的壓力,加快網頁開啟速度。

當然,凡事有利必有弊,產生HTML頁面無形中也耗費大量的磁碟空間以存放這些靜態檔案,在編輯頁面過程中除讀寫資料庫外,也要讀寫伺服器磁碟,頁面樣式的改動必須重建全部HTML檔案,等等。

像很多搜尋引擎,都可以提交網站的頁面地址清單,動態檔案的收錄問題已經不算是個問題了(如google sitemap)。得失就要自己衡量把握了,但無論如何,我們還是要懂得如何操作的。這裡就引用一下別人的文章說明幾種常見的產生思路,供大家參考參考。

1、下面這個例子直接利用FSO把html代碼寫入到檔案中然後產生.html格式的檔案 。這是最原始的,優點是簡單,缺點是頁面的修改不方便,我一般用到的地方是利用它產生整站參數檔案。(通常網站如標題,名稱等配置儲存在資料庫,我將它產生config.asp儲存這些變數調用,避免頻繁訪問資料庫)

以下為引用的內容:

<%
filename="test.htm"
if request("body")<>"" then
set fso = Server.CreateObject("Scripting.FileSystemObject")
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
htmlwrite.write "<body>輸出Title內容: " & request.form("title") & "<br /> 輸出Body內容:" & request.form("body")& "</body></html>"
htmlwrite.close
set fout=nothing
set fso=nothing
end if
%>
<form name="form" method="post" action="">
<input name="title" value="Title" size=26>
<br>
<textarea name="body">Body</textarea>
<br>
<br>
<input type="submit" name="Submit" value="產生html">
</form>

2、但是按照上面的方法產生html檔案非常不方便,第二種方法就是利用模板技術,將模板中特殊代碼的值替換為從表單或是資料庫欄位中接受過來的值,完成模板功能,將最終替換過的所有模板代碼產生HTML檔案。這種技術採用得比較多,大部分的CMS都是使用這類方法。

以下為引用的內容:

template.htm ' //模板檔案
<html>
<head>
<title>$title$ by webjx.com</title>
</head>
<body>
$body$
</body>
</html>
TestTemplate.asp '// 產生Html
<%
Dim fso,htmlwrite
Dim strTitle,strContent,strOut
'// 建立檔案系統對象
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'// 開啟網頁模板檔案,讀模數板內容
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
strOut=f.ReadAll
htmlwrite.close
strTitle="產生的網頁標題"
strContent="產生的網頁內容"
'// 用真實內容替換模板中的標記
strOut=Replace(strOut,"$title$",strTitle)
strOut=Replace(strOut,"$body$",strContent)
'// 建立要產生的靜態頁
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
'// 寫入網頁內容
htmlwrite.WriteLine strOut
htmlwrite.close
Response.Write "產生靜態頁成功!"
'// 釋放檔案系統對象
set htmlwrite=Nothing
set fso=Nothing
%>

3、第三種方法就是用XMLHTTP擷取動態網頁產生的HTML內容,再用ADODB.Stream或者Scripting.FileSystemObject儲存成html檔案。找到一段XMLHTTP產生Html的代碼參考一下。

以下為引用的內容:

<%
'常用函數
'1、輸入url目標網頁地址,傳回值getHTTPPage是目標網頁的html代碼
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
end function
 
'2、轉換亂瑪,直接用xmlhttp調用有中文字元的網頁得到的將是亂瑪,可以通過adodb.stream組件進行轉換
 
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
txtURL=server.MapPath("../index.asp")
sText = getHTTPPage(txtURL)
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
filename="../index.htm"
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true為不存在自行建立
openFile.writeline(sText)
Set OpenFile=nothing
%>
<script>
alert("靜態網頁產生完畢");
history.back();
</script>

小結,這三種方式是比較常用的產生HTML檔案方法,我個比較喜歡使用第三種方式,因為頁面改動時非常方便,就算動態網頁改動多大都好,只要重新用XMLHTTP讀取產生一次即可。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.