




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
ASP生成靜態網頁的多種方法使用FSO生成<%
'使用FSO生成
Setfs=CreateObject("Scripting.FileSystemObject")
NewFile=Server.MapPath("ud03/fso.htm")
'新建一文件fso.htm,若該文件已存在,則覆蓋它
Seta=fs.CreateTextFile(NewFile,True)
Response.Write"新文件已建立!"
a.close
File=Server.MapPath("ud03/fso.htm")
Settxt=fs.OpenTextFile(File,8,True)'打開成可以在結尾寫入數據的文件
data1="這句話是使用WriteLine方法寫入的。!<Br>"
txt.WriteLinedata1
data2="這句話是使用Write方法寫入的。<Br>"
txt.Writedata2
txt.Close
%>
使用XMLHTTP生成
<%
'使用XMLHTTP生成
Setxml=Server.CreateObject("Microsoft.XMLHTTP")
'把下面的地址替換成你的首頁的文件地址,一定要用http://開頭的絕對路徑,不能寫相對路徑
xml.Open"GET","/ud03/",False
xml.Send
BodyText=xml.ResponseBody
BodyText=BytesToBstr(BodyText,"gb2312")
Setxml=Nothing
Dimfso,MyFile
Setfso=CreateObject("Scripting.FileSystemObject")
SetMyFile=fso.CreateTextFile(server.MapPath("ud03.htm"),True)'生成的html的文件名
MyFile.WriteLine(BodyText)
MyFile.Close'使用Adodb.Stream處理二進制數據
FunctionBytesToBstr(strBody,CodeBase)
dimobjStream
setobjStream=Server.CreateObject("Adodb.Stream")
objStream.Type=1
objStream.Mode=3
objStream.Open
objStream.WritestrBody
objStream.Position=0
objStream.Type=2
objStream.Charset=CodeBase
BytesToBstr=objStream.ReadText
objStream.Close
setobjStream=nothing
EndFunction
%>
使用XMLHTTP批量生成
<%
'使用XMLHTTP批量生成
dimstrUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
Fori=1To30'需要生成的id:1到30
Html_Temp=Html_Temp&"<LI>"
Item_Classid=i
FileName="Archives_"&Item_Classid&".htm"'生成的html文件名
FilePath=Server.MapPath("/")&"\"&FileName
Html_Temp=Html_Temp&FilePath&"</LI>"
Do_Url="/ud03/index.php"'WEB路徑
Do_Url=Do_Url&"?p="&Item_Classid'WEB路徑之后的ID
strUrl=Do_Url
dimobjXmlHttp
setobjXmlHttp=Server.CreateObject("Microsoft.XMLHTTP")
objXmlHttp.open"GET",strUrl,false
objXmlHttp.send()
DimbinFileData
binFileData=objXmlHttp.responseBody
DimobjAdoStream
setobjAdoStream=Server.CreateObject("ADODB.Stream")
objAdoStream.Type=1
objAdoStream.Open()
objAdoStream.Write(binFileData)
objAdoStream.SaveToFileFilePath,2
objAdoStream.Close()
Next
Html_Temp=Html_Temp&"<UL>"
%>
<%
Response.Write("成功生成文件:")
Response.Write("<BR>")
Response.WriteHtml_Temp
%>ASP用標簽替換的方法生成靜態網頁
大家都知道HTML靜態網頁更容易被搜索引擎收錄索引,動態生成HTML網頁,也可使網站的網頁數量增多,搜索引擎收錄的數量也可能多,再加下提高網頁的質量也意未著什么呢?我想大家也知道。為了這個,我決定了改變之前網站建設,網頁設計的方法,經過多翻的研究及思考,對多種網頁動態生成的方法,我比較喜歡用標簽替換的方法成生網頁。標簽替換法:這是我個人理解的定義,不知道別人怎么叫它的,呵呵!標簽替換法,就是在設計好了的網頁模板中,放入自已設定的標簽,然后用你需要顯示出來的東東替換它。如模板文件1這個模板我們保存在數據庫表中temptable<html>
<head>
<title>{$SiteName}</title>
</head>
<body>
{$Arc_List$}
</body>
<html>
在以上模板中我放入了兩個標簽{$SiteName}網站名稱和{$Arc_List$}文章列表,再來看下面的代碼<%
dimrs,SiteName,Arc_List,fso,myFile,FilePath,html
SiteName="我的第一個動態生成的HTML網頁"
FilePath=Server.MapPath("/html/index.html")
setrs=server.createobject("adodb.recordset")
rs.open"select[temp]fromtemptable,conn,1,1
html=rs("temp")
'讀取網頁模板
rs.close
html=replace(html,"{$SiteName}",SiteName)
'用自定義的SiteName替換{$SiteName}標簽
html=html&replace(html,"{$Arc_List$}",get_ArcList())
'用自定義的get_ArcList()函數替換{$Arc_List$}標簽
setrs=nothing
conn.close
setconn=nothing
setfso=CreateObject("***ing.FileSystemObject")
'創建文件系統對象
SetMyFile=fso.CreateTextFile(FilePath,True)
'創建文件
MyFile.WriteLine(html)
'把htm代碼寫入文件
MyFile.close
'關閉文件
SetMyFile=nothing
'釋放文件對象
setfso=nothing
'釋放系統文件對象
response.write"<***language='java***'>window.alert('文件生成成功了');</***>"
response.end()
Functionget_ArcList()
dimstr,str1
str1=""
str="<ul>{list}</ul>"
rs.open"selectTitle,urlfromArc"
whilenotrs.eof
str1=str1&"<li><ahref="&rs("url")&">"&rs("Title")&"</a></li>"
rs.movenext
wend
rs.close
str=replace(str,"{list}",Str1)
get_ArcList=str
%>
EndFunction
以上的方法是不是很簡單,現在很多CMS都是采用這種方法生成靜態網頁的,這種方法使用比較靈活,只要你用心去設計一下你的系統,以后網做一個網站,只要設計模板就可以了。asp生成靜態網頁不用模板直接傳參數讀取asp文件
<%
hps=50
indexmulu="/asp/00/pro"
'''''''''修改這里為本系統所在相對目錄,以根據自己的程序修改
'**************************************************************
'函數名:htmll
'作
用:生成靜態頁面
'參
數:htmlmuluHTML模板(asp源文件)存放的目錄
'
FileName生成的HTML文件名(不包括.及擴展名)
'
filefrom生成的HTML文件.及擴展名
'
ArrName參數的名稱數組
'
ArrContent對應參數的內容數組
'**************************************************************
Functionhtmll(mulu,htmlmulu,FileName,filefrom,ArrName,ArrContent)
ifmulu=""thenmulu="/news/"'默認生成的HTML文件存放的目錄
ifhtmlmulu=""thenhtmlmulu="/html/"'默認HTML模板存放的目錄
mulu=indexmulu&mulu
htmlmulu=indexmulu&htmlmulu
FilePath=Server.MapPath(mulu)&"\"&FileName
Do_Url="http://"
Do_Url=Do_Url&Request.ServerVariables("SERVER_NAME")&htmlmulu&filefrom
ifIsArray(ArrName)then
Do_Url=Do_Url&"?"&ArrName(0)&ArrContent(0)
fori=1toUbound(ArrName)
Do_Url=Do_Url&"&"&ArrName(i)&ArrContent(i)
next
endif
strUrl=Do_Url
setobjXmlHttp=Server.createObject("Microsoft.XMLHTTP")
objXmlHttp.open"GET",strUrl,false
objXmlHttp.send()
binFileData=objXmlHttp.responseBody
SetobjXmlHttp=Nothing
setobjAdoStream=Server.createObject("ADODB.Stream")
objAdoStream.Type=1
objAdoStream.Open()
objAdoStream.Write(binFileData)
objAdoStream.SaveToFileFilePath,2
objAdoStream.Close()
setobjAdoStream=nothing
EndFunction
%>
ASP生成靜態網頁的方法
下面的例子是將、index.asp?id=1/index.asp?id=2/index.asp?id=3/這三個動態頁面,分別生成index1.htm,index2.htm,index3.htm存在根目錄下面:<%
dimstrUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
Fori=1To3
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
dimobjXmlHttp
setobjXmlHttp=Server.CreateObject("Microsoft.XMLHTTP")
objXmlHttp.open"GET",strUrl,false
objXmlHttp.send()
DimbinFileData
binFileData=objXmlHttp.responseBody
DimobjAdoStream
setobjAdoStream=Server.CreateObject("ADODB.Stream")
objAdoStream.Type=1
objAdoStream.Open()
objAdoStream.Write(binFileData)
objAdoStream.SaveToFileFilePath,2
objAdoStream.Close()
Next
Html_Temp=Html_Temp&"<UL>"
%>
<%
Response.Write("成功生成文件:")
Response.Write("<BR>")
Response.WriteHtml_Temp
%>非摸板生成靜態頁目前已經有很多生成html的新聞系統,但是都是用的模板,本函數實現把asp頁面產生的html代碼保存成為一個html文件,這樣就沒有必要改動原來的頁面就可以輕松完成一個生成html的新聞系統了。^_^
由于代碼比較短,這里就不進行注釋了
<%
'當目標頁面的包含文件即#include的頁面里邊存在response.End()的時候本程序有問題
'注意:本文件一定要放在filename指向的文件的同一目錄下
dimhughchiu_rtcode
Functionget_exe_code(filename)
dimexecode
dimtmp_str
Dimre,re1,content,fso,f,aspStart,aspEnd
dimms,m
execode=""
setfso=CreateObject("Scripting.FileSystemObject")
setf=fso.OpenTextFile(server.mappath(filename))
content=f.ReadAll
f.close
setf=nothing
setfso=nothingsetre=newregexp
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
setms=re.execute(content)
ifms.count<>0then
setm=ms(0)
tmp_str=get_exe_code(m.submatches(0))
content=re.replace(content,tmp_str)
else
exitdo
endif
loop
setm=nothing
setms=nothingre.pattern="^\s*="
aspEnd=1
aspStart=inStr(aspEnd,content,"<%")+2setre1=newRegExp
re1.ignorecase=true
re1.global=false
re1.pattern="response\.Write(.+)"dowhileaspStart>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
setms=re1.execute(tmp_str)
ifms.count<>0then
setm=ms(0)
tmp_str=re1.replace(tmp_str,"hughchiu_rtcode=hughchiu_rtcode&"&m.submatches(0))
else
exitdo
endif
loopsetm=nothing
setms=nothingexecode=execode&re.replace(tmp_str,"hughchiu_rtcode=hughchiu_rtcode&")aspStart=inStr(aspEnd,content,"<%")+2
loopsetre1=nothing
setre=nothingexecode=execode&vbcrlf&"hughchiu_rtcode=hughchiu_rtcode&"""&replace(replace(Mid(content,aspEnd),"""",""""""),vbcrlf,"""&vbcrlf&""")&""""&vbcrlf
get_exe_code="<%"&execode&"%\>"
EndFunctionfunctionasp2html(filename)
dimcode
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
endfunction
%>使用范例:
setfso=CreateObject("Scripting.FileSystemObject")
setf=fso.CreateTextFile(server.mappath("youpage.htm"),true)
f.WriteLine(asp2html("youpage.asp"))
f.close
setf=nothing
setfso=nothing可見,雖然是新方法還是需要fso的支持下面代碼可以幫您生成靜態頁面,如:list.asp是讀數據庫的頁面,要生在list.html靜態頁面,你的域名是,可以用下面代碼,使用方法:ifSaveFile("/html/list.html","/list.asp")then
Response.write"已生成"
else
Response.write"沒有生成"
endif如生成失敗,請把代碼OnErrorResumeNext封了,查看具體錯誤信息代碼如下:程序代碼
<%
ifSaveFile("/html/list.html","/list.asp")then
Response.write"已生成"
else
Response.write"沒有生成"
endiffunctionSaveFile(LocalFileName,RemoteFileUrl)
DimAds,Retrieval,GetRemoteData
OnErrorResumeNext
SetRetrieval=Server.CreateObject("Microso"&"ft.XM"&"LHTTP")
WithRetrieval
.Open"Get",RemoteFileUrl,False,"",""
.Send
GetRemoteData=.ResponseBody
EndWith
SetRetrieval=Nothing
SetAds=Server.CreateObject("Ado"&"db.Str"&"eam")
WithAds
.Type=1
.Open
.WriteGetRemoteData
.SaveToFileServer.MapPath(LocalFileName),2
.Cancel()
.Close()
EndWith
SetAds=nothing
iferr<>0then
SaveFile=false
err.clear
else
SaveFile=true
endif
Endfunction
%>ASP生成靜態網頁各種方法收集整理
新聞系統、blog系統等都可能用到將動態頁面生成靜態頁面的技巧來提高頁面的訪問速度,從而減輕服務器的壓力,本文為大家搜集整理了ASP編程中常用的生成靜態網頁的方法,有使用fso的,也有使用到xmlhttp或者Adodb.Stream的。1.使用FSO生成<%
'使用FSO生成
Setfs=CreateObject("Scripting.FileSystemObject")
NewFile=Server.MapPath("ud03/fso.htm")
'新建一文件fso.htm,若該文件已存在,則覆蓋它
Seta=fs.CreateTextFile(NewFile,True)
Response.Write"新文件已建立!"
a.close
File=Server.MapPath("ud03/fso.htm")
Settxt=fs.OpenTextFile(File,8,True)'打開成可以在結尾寫入數據的文件
data1="這句話是使用WriteLine方法寫入的。!<Br>"
txt.WriteLinedata1
data2="這句話是使用Write方法寫入的。<Br>"
txt.Writedata2
txt.Close
%>2.使用XMLHTTP生成<%
'使用XMLHTTP生成
Setxml=Server.CreateObject("Microsoft.XMLHTTP")
'把下面的地址替換成你的首頁的文件地址,一定要用http://開頭的絕對路徑,不能寫相對路徑
xml.Open"GET","/ud03/",False
xml.Send
BodyText=xml.ResponseBody
BodyText=BytesToBstr(BodyText,"gb2312")
Setxml=Nothing
Dimfso,MyFile
Setfso=CreateObject("Scripting.FileSystemObject")
SetMyFile=fso.CreateTextFile(server.MapPath("ud03.htm"),True)'生成的html的文件名
MyFile.WriteLine(BodyText)
MyFile.Close'使用Adodb.Stream處理二進制數據
FunctionBytesToBstr(strBody,CodeBase)
dimobjStream
setobjStream=Server.CreateObject("Adodb.Stream")
objStream.Type=1
objStream.Mode=3
objStream.Open
objStream.WritestrBody
objStream.Position=0
objStream.Type=2
objStream.Charset=CodeBase
BytesToBstr=objStream.ReadText
objStream.Close
setobjStream=nothing
EndFunction
%>3.使用XMLHTTP批量生成<%
'使用XMLHTTP批量生成
dimstrUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
Fori=1To30'需要生成的id:1到30
Html_Temp=Html_Temp&"<LI>"
Item_Classid=i
FileName="Archives_"&Item_Classid&".htm"'生成的html文件名
FilePath=Server.MapPath("/")&"\"&FileName
Html_Temp=Html_Temp&FilePath&"</LI>"
Do_Url="/ud03/index.php"'WEB路徑
Do_Url=Do_Url&"?p="&Item_Classid'WEB路徑之后的ID
strUrl=Do_Url
dimobjXmlHttp
setobjXmlHttp=Server.CreateObject("Microsoft.XMLHTTP")
objXmlHttp.open"GET",strUrl,false
objXmlHttp.send()
DimbinFileData
binFileData=objXmlHttp.responseBody
DimobjAdoStream
setobjAdoStream=Server.CreateObject("ADODB.Stream")
objAdoStream.Type=1
objAdoStream.Open()
objAdoStream.Write(binFileData)
objAdoStream.SaveToFileFilePath,2
objAdoStream.Close()
Next
Html_Temp=Html_Temp&"<UL>"
%>
<%
Response.Write("成功生成文件:")
Response.Write("<BR>")
Response.WriteHtml_Temp
%>4.自動按模板生成網站首頁
<%
Response.Expires=0
Response.expiresabsolute=Now()-1
Response.addHeader"pragma","no-cache"
Response.addHeader"cache-control","private"
Response.CacheControl="no-cache"
Response.Buffer=True
Response.Clear
Server.ScriptTimeOut=999999999
onerrorresumenext
'***************************************************************
'*
定義從模板從讀取首頁函數
'*說明:模板文件名為:index_Template.asp
'***************************************************************
FunctionGetPage(url)
SetRetrieval=CreateObject("Microsoft.XMLHTTP")
WithRetrieval
.Open"Get",url,False,"",""
.Send
GetPage=BytesToBstr(.ResponseBody)
EndWith
SetRetrieval=Nothing
EndFunction
FunctionBytesToBstr(body)
dimobjstream
setobjstream=Server.CreateObject("adodb.stream")
objstream.Type=1
objstream.Mode=3
objstream.Open
objstream.Writebody
objstream.Position=0
objstream.Type=2
objstream.Charset="GB2312"
BytesToBstr=objstream.ReadText
objstream.Close
setobjstream=nothing
EndFunction'***************************************************************
'*生頁首頁,文件名為:default.htm
'***************************************************************
dimTstr
Tstr=GetPage("/index_Template.asp")
Setfso=Server.CreateObject("Scripting.FileSystemObject")
Setfout=fso.CreateTextFile(Server.MapPath(".")&"/default.htm")
fout.WriteTstr
fout.close
Response.write"<script>alert(""生成首頁成功!\n\n文件名為:default.htm"");location.href=";</script"";</script>"
Response.end
%>5.將asp頁面轉換成htm頁面<%
FunctionGetPage(url)
'獲得文件內容
dimRetrieval
SetRetrieval=CreateObject("Microsoft.XMLHTTP")
WithRetrieval
.Open"Get",url,False',"",""
.Send
GetPage=BytesToBstr(.ResponseBody)
EndWith
SetRetrieval=Nothing
EndFunction
FunctionBytesToBstr(body)
dimobjstream
setobjstream=Server.CreateObject("adodb.stream")
objstream.Type=1
objstream.Mode=3
objstream.Open
objstream.Writebody
objstream.Position=0
objstream.Type=2
objstream.Charset="GB2312"
BytesToBstr=objstream.ReadText
objstream.Close
setobjstream=nothing
EndFunction
onerrorresumenext
Url="/""'要讀取的頁面地址
response.write"開始更新首頁..."
wstr=GetPage(Url)
'response.write(wstr)
Setfs=Server.CreateObject("Scripting.FileSystemObject")
'ifnotMyFile.FolderExists(server.MapPath("/html/"))then
'MyFile.CreateFolder(server.MapPath("/html/"))'
'endif
'要存放的頁面地址
dizhi=server.MapPath("index.htm")
If(fs.FileExists(dizhi))Then
fs.DeleteFile(dizhi)
EndIf
SetCrFi=fs.CreateTextFile(dizhi)
Crfi.Writeline(wstr)
setCrFi=nothing
setfs=nothing
response.write"...<fontcolor=red>更新完成!</font>"
%>代碼算是最簡單的,直接保存成一個asp文件即可,只要把URL(要轉化的asp地址)和dizhi(要保存的html地址)設置好就可以了,一般這兩個文件在同一個目錄,才能保證圖片或者css、js起作用。
6.下面是利用XMLHTTP將動態網頁生成靜態網頁的一段簡單代碼。如一個正常的index.asp頁面,并且用ASP代碼調出數據庫中的內容,另建一個makehtml.asp的頁面,加入一個textarea域,假設為name="body",將index.asp在textarea里調出來,如:
<textareaname="body"><!--#includefile="index.asp"--></textarea>將這個textarea包含在表單中,在接收表單頁用創建FSO對象,如下生成index.html文件!<%
filename="../index.html"
ifrequest("body")<>""then
setfso=Server.CreateObject("Scriptin
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年頁巖氣開采技術商業化進程中的技術創新動力與市場拓展研究報告
- 新能源汽車驅動電機電機轉子優化設計研究報告
- 新時代背景下文化產業金融支持政策優化策略及融資渠道拓展分析報告
- 2025年環保產業園循環經濟模式下的廢棄物處理設施運行管理與優化報告
- 工業互聯網平臺傳感器網絡自組網技術在2025年的產業鏈分析報告
- 文化場館建設2025年社會穩定風險評估與風險評估趨勢報告
- 新時代2025年城市生活垃圾分類處理長效機制與公眾參與度分析報告
- 2025年房地產市場調控政策對房價與房地產市場調控政策適應性分析與優化報告
- 2025年廢棄礦井資源再利用技術探索與產業轉型升級路徑優化報告
- icu鎮靜鎮痛考試試題及答案
- 工廠管理制度制度
- 餐飲服務食品安全監督量化分級管理制度
- 2023年中國財稅服務行業市場全景評估及未來投資趨勢預測
- 醫療衛生事業單位面試題型及答題技巧
- 腫瘤科運用PDCA循環提高疼痛評估的規范率品管圈成果匯報
- 管道安全檢查表
- 心理劇比賽點評金句
- 校園網站的設計與實現-畢業論文
- 石油石化職業技能鑒定操作試題集-輸氣工中級
- 辦公樓工程臨時用電專項施工方案
- GB/T 18705-2002裝飾用焊接不銹鋼管
評論
0/150
提交評論