zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

ASP页面静态化批量生成代码分享(多种方法)

ASP静态批量方法代码 分享 生成 页面
2023-06-13 09:14:28 时间
1、ASP两种简单的生成静态首页的方法

为什么要生成静态首页?
1、如果你首页读取的数据库次数比较多,速度很慢,而且占用很多服务器资源。使用静态页面访问速度当然快多了
2、搜索引擎容易搜索到
3、如果程序出问题,也能保证首页能访问。
4、其他的太多,自己想:)
应用方式:
如果你的首页是index.asp,你可以生成index.htm(默认访问顺序必须是index.htm,index.asp)。这样访问者第一次访问到你的网站的时候打开的是index.htm。你可以把网站首页的链接做成index.asp,这样从网站任何一个页面点击首页的链接出现的就是index.asp,这样保证的信息更新的及时性(毕竟index.htm需要每次手动更新)。
方法一:
直接将首页文件包含在表单文本框中,将首页代码最为数据提交,然后生成静态页面。
代码如下:
复制代码代码如下:

<%
"------------------------------------------------------------
"使用表单提交生成静态首页的代码
"确保你的空间支持FSO,且首页代码内容较少
"------------------------------------------------------------
dimcontent
content=Trim(Request.Form("content"))
ifcontent<>""then
callmakeindex()
endif
submakeindex()
SetFso=Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("index.htm")
SetSite_Config=FSO.CreateTextFile(Filen,true,False)
Site_Config.Writecontent
Site_Config.Close
SetFso=Nothing
Response.Write("<script>alert("已经成功生成首页!")</script>")
endsub
%>
<formname="form1"method="post"action="">
<textareaname="content">
<!--#includefile="index.asp"-->
</textarea>
<br>
<inputtype="submit"name="Submit"value="提交">
</form>

缺点:
1、如果首页中包括<@..>标记,会提示出错。
2、如果首页代码较长,用表单无法提交过去(表单数据长度有一定的限制)。
解决方案:
1、去掉index.asp中的<@>标记
2、使用eWebEditor,提交支持大数据(能自动分割)
优点:
可以在生成时对内容实时修改。
方法二:
直接使用XMLHTTP获取index.asp的代码

复制代码代码如下:

<%
"----------------------------------------------------------
"使用XMLHTTP生成静态首页的代码
"Curl为你的首页地址,确保你的空间支持FSO
"-----------------------------------------------------------
dimread,Curl,content
Curl="http://www.xx0123.com/index.asp"
read=getHTTPPage(Curl)
ifread<>""then
content=read
callmakeindex()
endif
submakeindex()
SetFso=Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("index.htm")
SetSite_Config=FSO.CreateTextFile(Filen,true,False)
Site_Config.Writecontent
Site_Config.Close
SetFso=Nothing
Response.Write("<script>alert("已经成功生成首页!")</script>")
endsub
FunctiongetHTTPPage(url)
dimhttp
sethttp=Server.createobject("Microsoft.XMLHTTP")
Http.open"GET",url,false
Http.send()
ifHttp.readystate<>4then
exitfunction
endif
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
sethttp=nothing
iferr.number<>0thenerr.Clear
Endfunction
FunctionBytesToBstr(body,Cset)
dimobjstream
setobjstream=Server.CreateObject("adodb.stream")
objstream.Type=1
objstream.Mode=3
objstream.Open
objstream.Writebody
objstream.Position=0
objstream.Type=2
objstream.Charset=Cset
BytesToBstr=objstream.ReadText
objstream.Close
setobjstream=nothing
EndFunction
%>


2、模板分离批量生成

模板文件中要替换的内容均以{...}括起来
为力求简洁,去掉了错误处理代码(replace中要来替换的字符串参数不能为null值,当然fso也应该做错误检查)。
复制代码代码如下:
<%
"---------------------------------------------------------------------------------------------------------------------
"出自:kevinfunghttp://www.yaotong.cn
"作者:kevinfung落伍者ID:kevin2008,转载时请保持原样
"时间:2006/07/05落伍者论坛首发
"----------------------------------------------------------------------------------------------------------------------
Dimstart"该变量为指针将要指向的记录集位置,通过参数动态获得
DimTemplate"模板文件将以字符串读入该变量
Dimcontent"替换后的字符串变量
DimobjConn"连接对象
DimConnStr"连接字符串
Dimsql"查询语句
Dimcnt:cnt=1"本轮循环计数器初始化
start=request("start")"获取本轮指针的开始位置
IfIsNumeric(start)Thenstart=CLng(start)Elsestart=1
Ifstart=0Thenstart=1"如果start
ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&Server.MapPath("DataBase.mdb")
sql="select*fromtable_name"
SetobjConn=Server.CreateObject("ADODB.Connection")
objConn.OpenConnStr
setrs=Server.CreateObject("ADODB.Recordset")
rs.opensql,objConn,1,1"打开数据集
rs.AbsolutePosition=start"最关键的一步,将指针指向start,start通过参数动态获得
Template=getTemplate(Server.MapPath("template.html"))"template.html为模板文件,通过函数getTemplate读入到字符串,模板文件中要替换的内容均以{...}括起来
WhileNotrs.eofAndcnt<=500"500是设定一次请求生成页面的循环次数,根据实际情况修改,如果太高了,记录集很多的时候会出现超时错误
content=Replace(Template,"{filed_name_1}",rs("filed_name_1"))"用字段值替换模板内容
content=Replace(content,"{filed_name_2}",rs("filed_name_2"))
......
content=Replace(content,"{filed_name_n}",rs("filed_name_n"))
genHtmlcontent,Server.MapPath("htmfiles/"&rs("id")&".html")"将替换之后的Template字符串生成HTML文档,htmfiles为存储静态文件的目录,请手动建立
cnt=cnt+1"计数器加1
start=start+1"指针变量递增
rs.movenext
wend
IfNotrs.eofThen"通过刷新的方式进行下一轮请求,并将指针变量start传递到下一轮
response.write"<metahttp-equiv="refresh"content="0;URL=?start="&start&"">"
Else
response.write"生成HTML文件完毕!"
Endif
rs.Close()
Setrs=Nothing
objConn.Close()
SetobjConn=Nothing
FunctiongetTemplate(template)"读取模板的函数,返回字符串,template为文件名
Dimfso,f
setfso=CreateObject("Scripting.FileSystemObject")
setf=fso.OpenTextFile(template)
getTemplate=f.ReadAll
f.close
setf=nothing
setfso=Nothing
EndFunction
SubgenHtml(content,filename)"将替换后的内容写入HTML文档,content为替换后的字符串,filename为生成的文件名
Dimfso,f
Setfso=Server.CreateObject("Scripting.FileSystemObject")
Setf=fso.CreateTextFile(filename,true)"如果文件名重复将覆盖旧文件
f.Writecontent
f.Close
Setf=Nothing
setfso=Nothing
EndSub
%>