ASP生成静态页面的6种方法_第1页
ASP生成静态页面的6种方法_第2页
ASP生成静态页面的6种方法_第3页
ASP生成静态页面的6种方法_第4页
ASP生成静态页面的6种方法_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论