电竞比分网-中国电竞赛事及体育赛事平台

分享

ASP生成靜態(tài)Html網(wǎng)頁的幾種方法


網(wǎng)頁生成靜態(tài)Html文件有許多好處,比如生成html網(wǎng)頁有利于被搜索引擎收錄,不僅被收錄的快還收錄的全.前臺脫離了數(shù)據(jù)訪問,減輕對數(shù)據(jù)庫訪問的壓力,加快網(wǎng)頁打開速度. 所以吟清最近對生成html比較感興趣,看了不少文章,也有一點(diǎn)點(diǎn)收獲.
1,下面這個例子直接利用FSO把html代碼寫入到文件中然后生成.html格式的文件
<%
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內(nèi)容: " & request.form("title") & "<br /> 輸出Body內(nèi)容:" & 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文件非常不方便,第二種方法就是利用模板技術(shù),將模板中特殊代碼的值替換為從表單或是數(shù)據(jù)庫字段中接受過來的值,完成模板功能;將最終替換過的所有模板代碼生成HTML文件.這種技術(shù)采用得比較多,大部分的CMS都是使用這類方法.
template.htm ' //模板文件 <html>

<head>  
<title>$title$ by aspid.cn</title>  
</head>  
<body>  
$body$  
</body>  
</html> ?  
TestTemplate.asp '// 生成Html <%  
Dim fso,htmlwrite  
Dim strTitle,strContent,strOut  
'// 創(chuàng)建文件系統(tǒng)對象  
Set fso=Server.CreateObject("Scripting.FileSystemObject")  
'// 打開網(wǎng)頁模板文件,讀取模板內(nèi)容  
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))  
strOut=f.ReadAll  
htmlwrite.close  
strTitle="生成的網(wǎng)頁標(biāo)題"  
strC  
'// 用真實(shí)內(nèi)容替換模板中的標(biāo)記  
strOut=Replace(strOut,"$title$",strTitle)  
strOut=Replace(strOut,"$body$",strContent)  
'// 創(chuàng)建要生成的靜態(tài)頁  
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)  
'// 寫入網(wǎng)頁內(nèi)容  
htmlwrite.WriteLine strOut  
htmlwrite.close  
Response.Write "生成靜態(tài)頁成功!"  
'// 釋放文件系統(tǒng)對象  
set htmlwrite=Nothing  
set fso=Nothing  
%>
3、第三種方法就是用XMLHTTP獲取動態(tài)頁生成的HTML內(nèi)容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。這句話是在藍(lán)色理想上看到的,對XMLHTTP吟清還不熟悉正在找資料了解.找到一段XMLHTTP生成Html的代碼參考一下.

<%  
'常用函數(shù)  
'1、輸入url目標(biāo)網(wǎng)頁地址,返回值getHTTPPage是目標(biāo)網(wǎng)頁的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、轉(zhuǎn)換亂瑪,直接用xmlhttp調(diào)用有中文字符的網(wǎng)頁得到的將是亂瑪,可以通過adodb.stream組件進(jìn)行轉(zhuǎn)換

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("靜態(tài)網(wǎng)頁生成完畢");  
history.back();  
</script>

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多