【asp源码栏目提醒】:网学会员在asp源码频道为大家收集整理了“ASP页面静态化批量生成代码 - 技术总结“提供大家参考,希望对大家有所帮助!
ASP页面静态化批量生成代码 1、
ASP两种简单的生成静态首页的方法 为什么要生成静态首页1、如果你首页读取的数据库次数比较多速度很慢而且占用很多服务器资源。
使用静态页面访问速度当然快多了2、搜索引擎容易搜索到3、如果程序出问题也能保证首页能访问。
4、其他的太多自己想应用方式如果你的首页是index.
asp你可以生成index.htm默认访问顺序必须是index.htmindex.
asp。
这样访问者第一次访问到你的网站的时候打开的是index.htm。
你可以把网站首页的链接做成index.
asp这样从网站任何一个页面点击首页的链接出现的就是index.
asp这样保证的信息更新的及时性毕竟index.htm需要每次手动更新。
方法一直接将首页文件包含在表单文本框中将首页代码最为数据提交然后生成静态页面。
代码如下 --使用表单提交生成静态首页的代码确保你的空间支持FSO且首页代码内容较少--dim content contentTrimRequest.Formcontentif contentthen call makeindexend if sub makeindexSet FsoServer.CreateObjectScripting.FileSystemObjectFilenServer.MapPathindex.htmSet Site_ConfigFSO.CreateTextFileFilentrueFalseSite_Config.Write content Site_Config.Close Set FsoNothing Response.Writescript alert已经成功生成首页/scriptend subform nameform1methodpostactiontextarea namecontent--i nclude fileindex.
asp--/textarea br input typesubmitnameSubmitvalue提交/form缺点1、如果首页中包括.标记会提示出错。
2、如果首页代码较长用表单无法提交过去表单数据长度有一定的限制。
解决方案1、去掉index.
asp中的标记2、使用eWebEditor提交支持大数据能自动分割优点可以在生成时对内容实时修改。
方法二直接使用XMLHTTP获取index.
asp的代码 --使用XMLHTTP生成静态首页的代码Curl为你的首页地址确保你的空间支持FSO---dim readCurlcontent CurlreadgetHTTPPageCurlif readthen contentread call makeindexend if sub makeindexSet FsoServer.CreateObjectScripting.FileSystemObjectFilenServer.MapPathindex.htmSet Site_ConfigFSO.CreateTextFileFilentrueFalseSite_Config.Write content Site_Config.Close Set FsoNothing Response.Writescript alert已经成功生成首页/scriptend sub Function getHTTPPageurldim http set httpServer.createobjectMicrosoft.XMLHTTPHttp.openGETurlfalse Http.sendif Http.readystate 4then exit function end if getHTTPPagebytesToBSTRHttp.responseBodyGB2312set httpnothing if err.number 0then err.Clear End function Function BytesToBstrbodyCsetdim objstream set objstreamServer.CreateObjectadodb.streamobjstream.Type1 objstream.Mode3 objstream.Open objstream.Write body objstream.Position0 objstream.Type2 objstream.CharsetCset BytesToBstrobjstream.ReadText objstream.Close set objstreamnothing End Function 2、模板分离批量生成 模板文件中要替换的内容均以.括起来 为力求简洁去掉了错误处理代码replace中要来替换的字符串参数不能为null值当然fso也应该做错误检查。
---出自kevin fung作者kevin fung落伍者IDkevin2008转载时请保持原样时间2006/07/05落伍者论坛首发--Dim start该变量为指针将要指向的记录集位置通过参数动态获得Dim Template模板文件将以字符串读入该变量Dim content替换后的字符串变量Dim objConn连接对象Dim ConnStr连接字符串Dim sql查询语句Dim cntcnt1本轮循环计数器初始化 startrequeststart获取本轮指针的开始位置If IsNumericstartThen startCLngstartElse start1 If start0 Then start1如果start ConnStrProviderMicrosoft.Jet.OLEDB.4.0Data SourceServer.MapPathDataBase.mdbsqlselectfrom table_name Set objConnServer.CreateObjectADODB.ConnectionobjConn.Open ConnStr set rsServer.CreateObjectADODB.Recordsetrs.open sqlobjConn11打开数据集rs.AbsolutePositionstart最关键的一步将指针指向startstart通过参数动态获得 TemplategetTemplateServer.MapPathtemplate.htmltemplate.html为模板文件通过函数getTemplate读入到字符串模板文件中要替换的内容均以.括起来 While Not rs.eof And cnt500500是设定一次请求生成页面的循环次数根据实际情况修改如果太高了记录集很多的时候会出现超时错误contentReplaceTemplatefiled_name_1rsfiled_name_1用字段值替换模板内容contentReplacecontentfiled_name_2rsfiled_name_2.contentReplacecontentfiled_name_nrsfiled_name_n genHtml contentServer.MapPathhtmfiles/rsid.html将替换之后的Template字符串生成HTML文档htmfiles为存储静态文件的目录请手动建立 cntcnt1计数器加1 startstart1指针变量递增rs.movenext wend If Not rs.eof Then通过刷新的方式进行下一轮请求并将指针变量start传递到下一轮response.writemeta http-equivrefreshcontent0URLstartstartElse response.write生成HTML文件完毕End if rs.CloseSet rsNothing objConn.CloseSet objConnNothing Function getTemplatetemplate读取模板的函数返回字符串template为文件名Dim fsof set fsoCreateObjectScripting.FileSystemObjectset ffso.OpenTextFiletemplategetTemplatef.ReadAll f.close set fnothing set fsoNothing End Function Sub genHtmlcontentfilename将替换后的内容写入HTML文档content为替换后的字符串filename为生成的文件名Dim fsof Set fsoServer.CreateObjectScripting.FileSystemObjectSet ffso.CreateTextFilefilenametrue如果文件名重复将覆盖旧文件f.Write content f.Close Set fNothing set fsoNothing End Sub 该文章出自《HuswOnRoad在路上》原文链接转载时间2009-06-15 015549请务必保留此链接谢谢