當前位置:編程學習大全網 - 源碼下載 - 怎樣把ASP頁生成HTML頁?

怎樣把ASP頁生成HTML頁?

ASP生成靜態網頁的方法

隨著網站訪問量的加大,每次從數據庫讀取都是以效率作為代價的,很多用ACCESS作數據庫的更會深有體會,靜態頁加在搜索時,也會被優先考慮。互聯網上流行的做法是將數據源代碼寫入數據庫再從數據庫讀取生成靜態面,這樣無形間就加大了數據庫。將現有的ASP頁直接生成靜態頁,將會節省很多。

下面的例子是將、index.asp?id=1/index.asp?id=2/index.asp?id=3/這三個動態頁面,分別生成ndex1.htm,index2.htm,index3.htm存在根目錄下面:

<%

dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp

Html_Temp="<UL>"

For i=1 To 3

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

%>

非摸板生成靜態頁

目前已經有很多生成html的新聞系統,但是都是用的模板,本函數實現把asp頁面產生的html代碼保存成為壹個html文件,這樣就沒有必要改動原來的頁面就可以輕松完成壹個生成html的新聞系統了。^_^

由於代碼比較短,這裏就不進行註釋了

<%

'當目標頁面的包含文件即#include的頁面裏邊存在response.End()的時候本程序有問題

'註意:本文件壹定要放在filename指向的文件的同壹目錄下

dim hughchiu_rtcode

Function get_exe_code(filename)

dim execode

dim tmp_str

Dim re,re1,content,fso,f,aspStart,aspEnd

dim ms,m

execode = ""

set fso=CreateObject("Scripting.FileSystemObject")

set f=fso.OpenTextFile(server.mappath(filename))

content=f.ReadAll

f.close

set f=nothing

set fso=nothing

set re = new regexp

re.ignorecase = true

re.global = true

re.pattern = "\<\%\@[^\%]+\%\>"

content = re.replace(content,"")

re.global = false

re.pattern = "\<\!\-\-\s*\#include\s*file\s*=\s*\""([^\""]+)\""\s*\-\-\>"

do

set ms = re.execute(content)

if ms.count<>0 then

set m = ms(0)

tmp_str = get_exe_code(m.submatches(0))

content = re.replace(content, tmp_str)

else

exit do

end if

loop

set m = nothing

set ms = nothing

re.pattern="^\s*="

aspEnd=1

aspStart=inStr(aspEnd,content,"<%")+2

set re1=new RegExp

re1.ignorecase = true

re1.global = false

re1.pattern = "response\.Write(.+)"

do while aspStart>aspEnd+1

execode = execode&vbcrlf&" hughchiu_rtcode = hughchiu_rtcode&"""&replace( replace(Mid(content,aspEnd,aspStart-aspEnd-2),"""",""""""), vbcrlf, """&vbcrlf&""")&""""&vbcrlf

aspEnd=inStr(aspStart,content,"%\>")+2

tmp_str = Mid(content,aspStart,aspEnd-aspStart-2)

do

set ms = re1.execute(tmp_str)

if ms.count<>0 then

set m = ms(0)

tmp_str = re1.replace(tmp_str, " hughchiu_rtcode = hughchiu_rtcode&"&m.submatches(0))

else

exit do

end if

loop

set m = nothing

set ms = nothing

execode = execode& re.replace(tmp_str,"hughchiu_rtcode = hughchiu_rtcode&")

aspStart=inStr(aspEnd,content,"<%")+2

loop

set re1 = nothing

set re=nothing

execode = execode&vbcrlf&" hughchiu_rtcode = hughchiu_rtcode&"""&replace( replace(Mid(content,aspEnd), """", """"""), vbcrlf, """&vbcrlf&""" )&""""&vbcrlf

get_exe_code = "<%"&execode&"%\>"

End Function

function asp2html(filename)

dim code

code = replace( replace( replace( get_exe_code(filename), "hughchiu_rtcode = hughchiu_rtcode&"""""&vbcrlf, "" ), "<%", "" ), "%\>", "" )

'response.Write(code)

execute(code)

'response.Write( hughchiu_rtcode )

asp2html = hughchiu_rtcode

end function

%>

使用範例:

set fso=CreateObject("Scripting.FileSystemObject")

set f=fso.CreateTextFile( server.mappath( "youpage.htm" ), true )

f.WriteLine( asp2html("youpage.asp") )

f.close

set f = nothing

set fso = nothing

可見,雖然是新方法還是需要fso的支持

  • 上一篇:請問J2EE是什麽東西? 他和java有什麽關系?
  • 下一篇:懂周易的大家請進
  • copyright 2024編程學習大全網