ASP頁面靜態(tài)化批量生成代碼分享(多種方法)
更新時間:2011年05月19日 00:30:18 作者:
最近朋友網(wǎng)站需要將網(wǎng)站生成靜態(tài),但有時候生成靜態(tài)會出現(xiàn)問題,特多整理了一些方法, 大家可以根據(jù)自己網(wǎng)站需要選擇。
1、ASP兩種簡單的生成靜態(tài)首頁的方法
為什么要生成靜態(tài)首頁?
1、如果你首頁讀取的數(shù)據(jù)庫次數(shù)比較多,速度很慢,而且占用很多服務(wù)器資源。使用靜態(tài)頁面訪問速度當然快多了
2、搜索引擎容易搜索到
3、如果程序出問題,也能保證首頁能訪問。
4、其他的太多,自己想:)
應(yīng)用方式:
如果你的首頁是index.asp,你可以生成index.htm (默認訪問順序必須是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、如果首頁中包括<@ ..>標記,會提示出錯。
2、如果首頁代碼較長,用表單無法提交過去(表單數(shù)據(jù)長度有一定的限制)。
解決方案:
1、去掉index.asp中的<@ >標記
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值,當然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.Write content
f.Close
Set f = Nothing
set fso=Nothing
End Sub
%>
為什么要生成靜態(tài)首頁?
1、如果你首頁讀取的數(shù)據(jù)庫次數(shù)比較多,速度很慢,而且占用很多服務(wù)器資源。使用靜態(tài)頁面訪問速度當然快多了
2、搜索引擎容易搜索到
3、如果程序出問題,也能保證首頁能訪問。
4、其他的太多,自己想:)
應(yīng)用方式:
如果你的首頁是index.asp,你可以生成index.htm (默認訪問順序必須是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、如果首頁中包括<@ ..>標記,會提示出錯。
2、如果首頁代碼較長,用表單無法提交過去(表單數(shù)據(jù)長度有一定的限制)。
解決方案:
1、去掉index.asp中的<@ >標記
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值,當然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.Write content
f.Close
Set f = Nothing
set fso=Nothing
End Sub
%>
您可能感興趣的文章:
- 比較詳細的Asp偽靜態(tài)化方法及Asp靜態(tài)化探討
- 不用mod_rewrite直接用php實現(xiàn)偽靜態(tài)化頁面代碼
- 網(wǎng)頁的HTML靜態(tài)化_網(wǎng)站優(yōu)化之談
- 通用大型網(wǎng)站頁面靜態(tài)化解決方案
- php 靜態(tài)化實現(xiàn)代碼
- asp 網(wǎng)站靜態(tài)化函數(shù)代碼html
- nginx 偽靜態(tài)化rewrite規(guī)則
- php靜態(tài)化頁面 htaccess寫法詳解(htaccess怎么寫?)
- smarty 緩存控制前的頁面靜態(tài)化原理
- aspx文件格式使用URLRewriter實現(xiàn)靜態(tài)化變成html
- php頁碼形式分頁函數(shù)支持靜態(tài)化地址及ajax分頁
- 使用ob系列函數(shù)實現(xiàn)PHP網(wǎng)站頁面靜態(tài)化
- 分享常見的幾種頁面靜態(tài)化的方法
- 如何對ASP.NET網(wǎng)站實現(xiàn)靜態(tài)化
相關(guān)文章
asp中獲取內(nèi)容中所有圖片與獲取內(nèi)容中第一個圖片的代碼
用asp獲取內(nèi)容中的圖片與獲取內(nèi)容中的第一個圖片地址,主要是cms中保存內(nèi)容中的圖片需要用得到,使用的是正則的方法。2011-01-01獲取一個數(shù)字的個位、十位、百位的函數(shù)代碼
獲取一個數(shù)字的個位、十位、百位的代碼,需要的朋友可以參考下。2010-12-12asp access數(shù)據(jù)庫并生成XML文件范例
簡單asp加載access數(shù)據(jù)庫,并生成XML,然后再將XML數(shù)據(jù)加載進LIST組件范例學習。2009-04-04asp下用datediff實現(xiàn)計算兩個時間差的函數(shù)
asp下用datediff實現(xiàn)計算兩個時間差的函數(shù)...2007-11-11用asp實現(xiàn)訪問遠程計算機上MDB access數(shù)據(jù)庫文件的方法
用asp實現(xiàn)訪問遠程計算機上MDB access數(shù)據(jù)庫文件的方法...2007-11-11