您的位置:首页 > 编程语言 > ASP

ASP生成静态网页的多种方法

2008-12-12 00:04 501 查看
使用FSO生成

<%
'使用FSO生成
Set fs = CreateObject("Scripting.FileSystemObject")
NewFile=Server.MapPath("ud03/fso.htm")
'新建一文件fso.htm,若该文件已存在,则覆盖它
Set a = fs.CreateTextFile(NewFile, True)
Response.Write"新文件已建立!"
a.close
File=Server.MapPath("ud03/fso.htm")
Set txt=fs.OpenTextFile(File,8,True) '打开成可以在结尾写入数据的文件
data1="这句话是使用WriteLine方法写入的。!<Br>"
txt.WriteLine data1
data2="这句话是使用Write方法写入的。<Br>"
txt.Write data2
txt.Close
%>
使用XMLHTTP生成

<%
'使用XMLHTTP生成
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
'把下面的地址替换成你的首页的文件地址,一定要用http://开头的绝对路径,不能写相对路径
xml.Open "GET", "http://www.kinoko.name/ud03/", False
xml.Send
BodyText=xml.ResponseBody
BodyText=BytesToBstr(BodyText,"gb2312")
Set xml = Nothing
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile= fso.CreateTextFile(server.MapPath("ud03.htm"), True) '生成的html的文件名
MyFile.WriteLine(BodyText)
MyFile.Close

'使用Adodb.Stream处理二进制数据
Function BytesToBstr(strBody,CodeBase)
   dim objStream
   set objStream = Server.CreateObject("Adodb.Stream")
   objStream.Type = 1
   objStream.Mode =3
   objStream.Open
   objStream.Write strBody
   objStream.Position = 0
   objStream.Type = 2
   objStream.Charset = CodeBase
   BytesToBstr = objStream.ReadText
   objStream.Close
   set objStream = nothing
End Function
%>
使用XMLHTTP批量生成

<%
'使用XMLHTTP批量生成
dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
For i=1 To 30 '需要生成的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 = "http://www.kinoko.name/ud03/index.php" 'WEB路径
Do_Url = Do_Url&"?p="&Item_Classid 'WEB路径之后的ID
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
%>

 

 

 

 

 

ASP用标签替换的方法生成静态网页

大家都知道HTML静态网页更容易被搜索引擎收录索引,动态生成HTML网页,也可使网站的网页数量增多,搜索引擎收录的数量也可能多,再加下提高网页的质量也意未着什么呢?我想大家也知道。

为了这个,我决定了改变之前网站建设,网页设计的方法,经过多翻的研究及思考,对多种网页动态生成的方法,我比较喜欢用标签替换的方法成生网页。

标签替换法:这是我个人理解的定义,不知道别人怎么叫它的,呵呵!

标签替换法,就是在设计好了的网页模板中,放入自已设定的标签,然后用你需要显示出来的东东替换它。如

模板文件1

这个模板我们保存在数据库表中 temptable

<html>
<head>
<title>{$SiteName} </title>
</head>
<body>
{$Arc_List$}  
</body>
<html>
在以上模板中我放入了两个标签{$SiteName}网站名称和{$Arc_List$}文章列表,再来看下面的代码

<%
    dim rs,SiteName,Arc_List,fso,myFile,FilePath,html
    SiteName="我的第一个动态生成的HTML网页"
    FilePath = Server.MapPath("/html/index.html")
   set rs=server.createobject("adodb.recordset")
   rs.open"select [temp] from temptable,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$}标签
set rs=nothing
conn.close
set conn=nothing
   set fso=CreateObject("***ing.FileSystemObject")     '创建文件系统对象
   Set MyFile = fso.CreateTextFile(FilePath,True)               '创建文件
   MyFile.WriteLine(html)                                                         '把htm代码写入文件
   MyFile.close    '关闭文件
   Set MyFile = nothing    '释放文件对象
   set fso = nothing     '释放系统文件对象
   response.write "<*** language='java***'>window.alert('文件生成成功了');</***>"
response.end()
   Function get_ArcList()
         dim str,str1
         str1=""
         str = "<ul>{list}</ul>"
         rs.open"select Title,url from Arc"
         while not rs.eof
                str1 = str1 & "<li><a href="&rs("url")&">"&rs("Title")&"</a></li>"
        rs.movenext
        wend
        rs.close
       str = replace(str, "{list}", Str1)
       get_ArcList = str
%>
End Function
以上的方法是不是很简单,现在很多CMS都是采用这种方法生成静态网页的,这种方法使用比较灵活,只要你用心去设计一下你的系统,以后网做一个网站,只要设计模板就可以了。

 

 

 

 

 

 

asp生成静态网页 不用模板 直接传参数读取asp文件

<%
hps=50
indexmulu="/asp/00/pro"         '''''''''修改这里为本系统所在相对目录,以根据自己的程序修改
'**************************************************************
'函数名:htmll
'作   用:生成静态页面
'参   数:htmlmulu ----HTML模板(asp源文件)存放的目录
'     FileName ----生成的HTML文件名(不包括 .及扩展名)
'    filefrom ----生成的HTML文件 .及扩展名
'    ArrName ----参数的名称 数组
'    ArrContent ----对应参数的内容 数组
'**************************************************************
Function htmll(mulu,htmlmulu,FileName,filefrom,ArrName,ArrContent)
if mulu="" then mulu="/news/" '默认生成的HTML文件存放的目录
if htmlmulu="" then htmlmulu="/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
if IsArray(ArrName) then
   Do_Url=Do_Url&"?"&ArrName(0)&ArrContent(0)
   for i=1 to Ubound(ArrName)
    Do_Url=Do_Url&"&"&ArrName(i)&ArrContent(i)
   next
end if
strUrl=Do_Url
set objXmlHttp=Server.createObject("Microsoft.XMLHTTP")
objXmlHttp.open "GET",strUrl,false
objXmlHttp.send()
binFileData=objXmlHttp.responseBody
Set objXmlHttp=Nothing
set objAdoStream=Server.createObject("ADODB.Stream")
objAdoStream.Type=1
objAdoStream.Open()
objAdoStream.Write(binFileData)
objAdoStream.SaveToFile FilePath,2
objAdoStream.Close()
set objAdoStream=nothing
End Function
%>
 
 
 
 
 
 
 
 

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的支持
 

 

 

 

 

 

 

下面代码可以帮您生成静态页面,如:list.asp是读数据库的页面,要生在list.html静态页面,你的域名是flyso.net,可以用下面代码,使用方法:

if SaveFile("/html/list.html","http://www.flyso.net/list.asp") then
     Response.write "已生成"
else
     Response.write "没有生成"
end if

如生成失败,请把代码On Error Resume Next封了,查看具体错误信息

代码如下:


程序代码
<%
if SaveFile("/html/list.html","http://www.flyso.net/list.asp") then
     Response.write "已生成"
else
     Response.write "没有生成"
end if

function SaveFile(LocalFileName,RemoteFileUrl)
     Dim Ads, Retrieval, GetRemoteData
     On Error Resume Next
     Set Retrieval = Server.CreateObject("Microso" & "ft.XM" & "LHTTP")
     With Retrieval
         .Open "Get", RemoteFileUrl, False, "", ""
         .Send
         GetRemoteData = .ResponseBody
     End With
     Set Retrieval = Nothing
     Set Ads = Server.CreateObject("Ado" & "db.Str" & "eam")
     With Ads
         .Type = 1
         .Open
         .Write GetRemoteData
         .SaveToFile Server.MapPath(LocalFileName), 2
         .Cancel()
         .Close()
     End With
     Set Ads=nothing
     if err <> 0 then
         SaveFile = false
         err.clear
     else
         SaveFile = true
     end if
End function
%>
 

 

 

 

 

ASP生成静态网页各种方法收集整理

新闻系统、blog系统等都可能用到将动态页面生成静态页面的技巧来提高页面的访问速度,从而减轻服务器的压力,本文为大家搜集整理了ASP编程中常用的生成静态网页的方法,有使用fso的,也有使用到xmlhttp或者Adodb.Stream的。

1.使用FSO生成

<%
'使用FSO生成
Set fs = CreateObject("Scripting.FileSystemObject")
NewFile=Server.MapPath("ud03/fso.htm")
'新建一文件fso.htm,若该文件已存在,则覆盖它
Set a = fs.CreateTextFile(NewFile, True)
Response.Write"新文件已建立!"
a.close
File=Server.MapPath("ud03/fso.htm")
Set txt=fs.OpenTextFile(File,8,True) '打开成可以在结尾写入数据的文件
data1="这句话是使用WriteLine方法写入的。!<Br>"
txt.WriteLine data1
data2="这句话是使用Write方法写入的。<Br>"
txt.Write data2
txt.Close
%>

2.使用XMLHTTP生成

<%
'使用XMLHTTP生成
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
'把下面的地址替换成你的首页的文件地址,一定要用http://开头的绝对路径,不能写相对路径
xml.Open "GET", "http://www.kinoko.name/ud03/", False
xml.Send
BodyText=xml.ResponseBody
BodyText=BytesToBstr(BodyText,"gb2312")
Set xml = Nothing
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile= fso.CreateTextFile(server.MapPath("ud03.htm"), True) '生成的html的文件名
MyFile.WriteLine(BodyText)
MyFile.Close

'使用Adodb.Stream处理二进制数据
Function BytesToBstr(strBody,CodeBase)
    dim objStream
    set objStream = Server.CreateObject("Adodb.Stream")
    objStream.Type = 1
    objStream.Mode =3
    objStream.Open
    objStream.Write strBody
    objStream.Position = 0
    objStream.Type = 2
    objStream.Charset = CodeBase
    BytesToBstr = objStream.ReadText
    objStream.Close
    set objStream = nothing
End Function
%>

3.使用XMLHTTP批量生成

<%
'使用XMLHTTP批量生成
dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
For i=1 To 30 '需要生成的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 = "http://www.kinoko.name/ud03/index.php" 'WEB路径
Do_Url = Do_Url&"?p="&Item_Classid 'WEB路径之后的ID
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
%>

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
on error resume next
'***************************************************************
'*                         定义 从模板从读取首页 函数
'* 说明:模板文件名为:index_Template.asp
'***************************************************************
Function GetPage(url)
         Set Retrieval = CreateObject("Microsoft.XMLHTTP")
         With Retrieval
         .Open "Get", url, False, "", ""
         .Send
         GetPage = BytesToBstr(.ResponseBody)
         End With
         Set Retrieval = Nothing
End Function
Function BytesToBstr(body)
         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 = "GB2312"
         BytesToBstr = objstream.ReadText
         objstream.Close
         set objstream = nothing
End Function

'***************************************************************
'* 生页首页,文件名为:default.htm
'***************************************************************
dim Tstr
Tstr = GetPage("http://www.adhome.net/index_Template.asp")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(".")&"/default.htm")
fout.Write Tstr
fout.close
    Response.write"<script>alert(""生成首页成功!/n/n文件名为:default.htm"");location.href="http://www.adhome.net";</script>"
    Response.end
%>

5.将asp页面转换成htm页面

<%
Function GetPage(url)
'获得文件内容
dim Retrieval
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
With Retrieval
    .Open "Get", url, False ', "", ""
    .Send
    GetPage = BytesToBstr(.ResponseBody)
End With
Set Retrieval = Nothing
End Function
Function BytesToBstr(body)
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 = "GB2312"
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
on error resume next
Url="http://www.sina.com.cn"'要读取的页面地址
response.write "开始更新首页..."
wstr = GetPage(Url)
'response.write(wstr)
Set fs=Server.CreateObject("Scripting.FileSystemObject")
'if not MyFile.FolderExists(server.MapPath("/html/")) then
'MyFile.CreateFolder(server.MapPath("/html/"))'
'end if
'要存放的页面地址
dizhi=server.MapPath("index.htm")
If (fs.FileExists(dizhi)) Then
fs.DeleteFile(dizhi)
End If
Set CrFi=fs.CreateTextFile(dizhi)
Crfi.Writeline(wstr)
set CrFi=nothing
set fs=nothing
response.write "...<font color=red>更新完成!</font>"
%>

代码算是最简单的,直接保存成一个asp文件即可,只要把URL(要转化的asp地址)和dizhi(要保存的html地址)设置好就可以了,一般这两个文件在同一个目录,才能保证图片或者css、js起作用。

6.下面是利用XMLHTTP将动态网页生成静态网页的一段简单代码。

如一个正常的index.asp页面,并且用ASP代码调出数据库中的内容,另建一个makehtml.asp的页面,加入一个textarea域,假设为name="body",将index.asp在textarea里调出来,如:
<textarea name="body"><!--#include file="index.asp"--></textarea>

将这个textarea包含在表单中,在接收表单页用创建FSO对象,如下生成index.html文件!

<%
filename="../index.html"
if request("body")<>"" then
set fso = Server.CreateObject("Scripting.FileSystemObject")
set fout = fso.CreateTextFile(server.mappath(""&filename&""))
fout.write request.form("body")
fout.close
set fout=nothing
set fso=nothing
end if
%>

这样index.html文件就生成了,连模板都用不着,只要将正常情况下使用的ASP文件读取到textarea里就可以了,目前尚未发现问题!当然前提是服务器要支持FSO。

开启FSO权限 在 开始-“运行”中执行regsvr32.exe scrrun.dll即可。如想关闭FSO权限,在上述命令中加/u参数。注册表中的键值位置:HKEY_CLASS_BOOT/F.S.O .FSO中有个方法是CreateFolder,但是这个方法只能在其上一级文件夹存在的情况下创建新的文件夹,所以我就写了一个自动创建多级文件夹的函数,在生成静态页面等方面使用非常方便。函数:

’ --------------------------------
’ 自动创建指定的多级文件夹
’ strPath为绝对路径
Function AutoCreateFolder(strPath) ’ As Boolean
         On Error Resume Next
         Dim astrPath, ulngPath, i, strTmpPath
         Dim objFSO
         If InStr(strPath, "/") <=0 Or InStr(strPath, ":") <= 0 Then
                 AutoCreateFolder = False
                 Exit Function
         End If
         Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
         If objFSO.FolderExists(strPath) Then
                 AutoCreateFolder = True
                 Exit Function
         End If
         astrPath = Split(strPath, "/")
         ulngPath = UBound(astrPath)
         strTmpPath = ""
         For i = 0 To ulngPath
                 strTmpPath = strTmpPath & astrPath(i) & "/"
                 If Not objFSO.FolderExists(strTmpPath) Then
                         ’ 创建
                         objFSO.CreateFolder(strTmpPath)
                 End If
         Next
         Set objFSO = Nothing
         If Err = 0 Then
                 AutoCreateFolder = True
         Else
                 AutoCreateFolder = False
         End If
End Function 调用方法:

MyPath = "C:/a/b/c/"
If AutoCreateFolder(MyPath) Then
         Response.Write "创建文件夹成功"
Else
         Response.Write "创建文件夹失败"
End If
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  asp html function url list exe