asp批量生成html静态页面方法
??? 隨著網(wǎng)站訪問量的加大,每次從數(shù)據(jù)庫讀取都是以效率作為代價的,很多用ACCESS作數(shù)據(jù)庫的更會深有體會,靜態(tài)頁加在搜索時,也會被優(yōu)先考慮。互聯(lián)網(wǎng)上流行的做法是將數(shù)據(jù)源代碼寫入數(shù)據(jù)庫再從數(shù)據(jù)庫讀取生成靜態(tài)面,這樣無形間就加大了數(shù)據(jù)庫。將現(xiàn)有的ASP頁直接生成靜態(tài)頁,將會節(jié)省很多。
???
方法一(其中的i=1 to 5 可以根據(jù)你需要生成ID的值修改):
??? 下面的例子是將、index.asp?id=1/index.asp?id=2/index.asp?id=3/這三個動態(tài)頁面,分別生成ndex1.htm,index2.htm,index3.htm存在根目錄下面
<%
??? dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
??? Html_Temp="<UL>"
??? For i=1 To 5
??? Html_Temp = Html_Temp&"<LI>"
??? Item_Classid = i
??? FileName = "Index"&Item_Classid&".htm"
??? FilePath = Server.MapPath("/")&"/"&FileName
??? Html_Temp = Html_Temp&FilePath&"</LI>"
??? Do_Url = "http://"
??? Do_Url = Do_Url&Request.ServerVariables("SERVER_NAME")&"/main/index.asp"
??? Do_Url = Do_Url&"?Item_Classid="&Item_Classid
??? strUrl = Do_Url
??? dim objXmlHttp
??? set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
??? objXmlHttp.open "GET",strUrl,false
??? objXmlHttp.send()
??? Dim binFileData
??? binFileData = objXmlHttp.responseBody
??? Dim objAdoStream
??? set objAdoStream = Server.CreateObject("ADODB.Stream")
??? objAdoStream.Type = 1
??? objAdoStream.Open()
??? objAdoStream.Write(binFileData)
??? objAdoStream.SaveToFile FilePath,2
??? objAdoStream.Close()
??? Next
??? Html_Temp = Html_Temp&"<UL>"
??? %>
??? <%
??? Response.Write ( "成功生成文件:" )
??? Response.Write ( "<BR>" )
??? Response.Write Html_Temp
??? %>
?
方法二:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Dim myconn
myconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("數(shù)據(jù)庫地址")
set conn=server.createobject("ADODB.connection")
conn.open myconn
?? set rs=Server.CreateObject("ADODB.Recordset")
?? SQL= "SELECT * FROM 產(chǎn)品"
???? rs.Open sql,conn,1,1
dim read,Curl,content
do while not rs.eof
Curl="http://localhost/post/proziliao.asp?pro_id="&rs("pro_id")&""?????? '這里是循環(huán)地址
read=getHTTPPage(Curl)
if read<>"" then
content=read
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("../post/proziliao_"&rs("pro_id")&".html")?? '這里是生成的HTML
Set Site_Config=FSO.CreateTextFile(Filen,true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
end if
rs.movenext
loop
Function getHTTPPage(url)
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "post",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
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
%>
?
?
?
1、ASP兩種簡單的生成靜態(tài)首頁的方法?
為什么要生成靜態(tài)首頁?1、如果你首頁讀取的數(shù)據(jù)庫次數(shù)比較多,速度很慢,而且占用很多服務(wù)器資源。使用靜態(tài)頁面訪問速度當(dāng)然快多了
2、搜索引擎容易搜索到
3、如果程序出問題,也能保證首頁能訪問。
4、其他的太多,自己想:) 應(yīng)用方式:
如果你的首頁是index.asp,你可以生成index.htm (默認(rèn)訪問順序必須是index.htm,index.asp)。這樣訪問者第一次訪問到你的網(wǎng)站的時候打開的是index.htm 。你可以把網(wǎng)站首頁的鏈接做成index.asp,這樣從網(wǎng)站任何一個頁面點擊首頁的鏈接出現(xiàn)的就是index.asp,這樣保證的信息更新的及時性(畢竟index.htm需要每次手動更新)。 方法一:
直接將首頁文件包含在表單文本框中,將首頁代碼最為數(shù)據(jù)提交,然后生成靜態(tài)頁面。
代碼如下: <%
'------------------------------------------------------------
'使用表單提交生成靜態(tài)首頁的代碼
'確保你的空間支持FSO,且首頁代碼內(nèi)容較少
'------------------------------------------------------------ dim content content=Trim(Request.Form("content"))
if content<>"" then
call makeindex()
end if sub makeindex()
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("index.htm")
Set Site_Config=FSO.CreateTextFile(Filen,true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
Response.Write("<script>alert('已經(jīng)成功生成首頁!')</script>")
end sub
%>
<form name="form1" method="post" action="">
<textarea name="content">
<!-- #i nclude file="index.asp" -->
</textarea>
<br>
<input type="submit" name="Submit" value="提交">
</form> 缺點:
1、如果首頁中包括<@ ..>標(biāo)記,會提示出錯。
2、如果首頁代碼較長,用表單無法提交過去(表單數(shù)據(jù)長度有一定的限制)。
解決方案:
1、去掉index.asp中的<@ >標(biāo)記
2、使用eWebEditor,提交支持大數(shù)據(jù)(能自動分割)
優(yōu)點:
可以在生成時對內(nèi)容實時修改。 方法二:
直接使用XMLHTTP獲取index.asp的代碼
<%
'----------------------------------------------------------
'使用XMLHTTP生成靜態(tài)首頁的代碼
'Curl 為你的首頁地址,確保你的空間支持FSO
'----------------------------------------------------------- dim read,Curl,content Curl="http://www.xx0123.com/index.asp"
read=getHTTPPage(Curl) if read<>"" then
content=read
call makeindex()
end if sub makeindex()
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("index.htm")
Set Site_Config=FSO.CreateTextFile(Filen,true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
Response.Write("<script>alert('已經(jīng)成功生成首頁!')</script>")
end sub Function getHTTPPage(url)
dim http
set http=Server.createobject("Microsoft.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 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
%>
2、模板分離批量生成
模板文件中要替換的內(nèi)容均以{...}括起來
為力求簡潔,去掉了錯誤處理代碼(replace中要來替換的字符串參數(shù)不能為null值,當(dāng)然fso也應(yīng)該做錯誤檢查)。
<%
' ---------------------------------------------------------------------------------------------------------------------
' 出自: kevin fung http://www.yaotong.cn
' 作者: kevin fung 落伍者ID:kevin2008,轉(zhuǎn)載時請保持原樣
' 時間: 2006/07/05落伍者論壇首發(fā)
' ----------------------------------------------------------------------------------------------------------------------
Dim start '該變量為指針將要指向的記錄集位置,通過參數(shù)動態(tài)獲得
Dim Template '模板文件將以字符串讀入該變量
Dim content '替換后的字符串變量
Dim objConn '連接對象
Dim ConnStr '連接字符串
Dim sql '查詢語句
Dim cnt:cnt = 1 '本輪循環(huán)計數(shù)器初始化
start = request("start") '獲取本輪指針的開始位置
If IsNumeric(start) Then start = CLng(start) Else start=1
If start=0 Then start = 1 '如果start
ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath("DataBase.mdb")
sql = "select * from table_name"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open ConnStr
set rs = Server.CreateObject("ADODB.Recordset")
rs.open sql,objConn,1,1 '打開數(shù)據(jù)集
rs.AbsolutePosition = start '最關(guān)鍵的一步,將指針指向start,start通過參數(shù)動態(tài)獲得
Template = getTemplate(Server.MapPath("template.html"))' template.html為模板文件,通過函數(shù)getTemplate讀入到字符串,模板文件中要替換的內(nèi)容均以{...}括起來
While Not rs.eof And cnt<= 500 '500是設(shè)定一次請求生成頁面的循環(huán)次數(shù),根據(jù)實際情況修改,如果太高了,記錄集很多的時候會出現(xiàn)超時錯誤
content = Replace(Template,"{filed_name_1}",rs("filed_name_1")) '用字段值替換模板內(nèi)容
content = Replace(content,"{filed_name_2}",rs("filed_name_2"))
......
content = Replace(content,"{filed_name_n}",rs("filed_name_n"))
genHtml content,Server.MapPath("htmfiles/"&rs("id")&".html") '將替換之后的Template字符串生成HTML文檔,htmfiles為存儲靜態(tài)文件的目錄,請手動建立
cnt = cnt + 1 '計數(shù)器加1
start = start + 1 '指針變量遞增
rs.movenext
wend
If Not rs.eof Then '通過刷新的方式進行下一輪請求,并將指針變量start傳遞到下一輪
response.write "<meta http-equiv='refresh' content='0;URL=?start="&start&"'>"
Else
response.write "生成HTML文件完畢!"
End if
rs.Close()
Set rs = Nothing
objConn.Close()
Set objConn = Nothing
Function getTemplate(template)'讀取模板的函數(shù),返回字符串,template為文件名
Dim fso,f
set fso=CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile(template)
getTemplate=f.ReadAll
f.close
set f=nothing
set fso=Nothing
End Function
Sub genHtml(content,filename)'將替換后的內(nèi)容寫入HTML文檔,content為替換后的字符串,filename為生成的文件名
Dim fso,f
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateTextFile(filename,true)'如果文件名重復(fù)將覆蓋舊文件
f.Write content
f.Close
Set f = Nothing
set fso=Nothing
End Sub
%>
?
?
總結(jié)
以上是生活随笔為你收集整理的asp批量生成html静态页面方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP之防止外部数据提交的脚本
- 下一篇: 动态asp网页批量生成静态html网页问