Asp生成HTML方法大全收集
将动态页面转换生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录(特别是对接受动态参数的页面)。前台访问时,脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度。
当然,凡事有利必有弊,生成HTML页面无形中也耗费大量的磁盘空间以存放这些静态文件,在编辑页面过程中除读写数据库外,也要读写服务器磁盘,页面样式的改动必须重新生成全部HTML文件,等等。
像很多搜索引擎,都可以提交网站的页面地址列表,动态文件的收录问题已经不算是个问题了(如google sitemap)。得失就要自己衡量把握了,但无论如何,我们还是要懂得如何操作的。这里就引用一下别人的文章说明几种常见的生成思路,供大家参考参考。
一、下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件。这是最原始的,优点是简单,缺点是页面的修改不方便,我一般用到的地方是利用它生成整站参数文件。(通常网站如标题,名称等配置保存在数据库,我将它生成config.asp保存这些变量调用,避免频繁访问数据库)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <% filename="test.htm" if request("body")<>"" then set fso = Server.CreateObject("Scripting.FileSystemObject") set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" htmlwrite.close set fout=nothing set fso=nothing end if %> <form name="form" method="post" action=""> <input name="title" value="Title" size=26> <br> <textarea name="body">Body</textarea> <br> <br> <input type="submit" name="Submit" value="生成html"> </form> |
二、但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能,将最终替换过的所有模板代码生成HTML文件。这种技术采用得比较多,大部分的CMS都是使用这类方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | template.htm ' //模板文件 <html> <head> <title>$title$ by webjx.com</title> </head> <body> $body$ </body> </html> TestTemplate.asp '// 生成Html <% Dim fso,htmlwrite Dim strTitle,strContent,strOut '// 创建文件系统对象 Set fso=Server.CreateObject("Scripting.FileSystemObject") '// 打开网页模板文件,读取模板内容 Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) strOut=f.ReadAll htmlwrite.close strTitle="生成的网页标题" strContent="生成的网页内容" '// 用真实内容替换模板中的标记 strOut=Replace(strOut,"$title$",strTitle) strOut=Replace(strOut,"$body$",strContent) '// 创建要生成的静态页 Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) '// 写入网页内容 htmlwrite.WriteLine strOut htmlwrite.close Response.Write "生成静态页成功!" '// 释放文件系统对象 set htmlwrite=Nothing set fso=Nothing %> |
三、第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。找到一段XMLHTTP生成Html的代码参考一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <% '常用函数 '1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 function getHTTPPage(url) dim Http set Http=server.createobject("MSXML2.XMLHTTP") Http.open "GET",url,false Http.send() if Http.readystate<>4 then exit function end if getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") set http=nothing if err.number<>0 then err.Clear end function '2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 Function BytesToBstr(body,Cset) 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 = Cset BytesToBstr = objstream.ReadText objstream.Close set objstream = nothing End Function txtURL=server.MapPath("../index.asp") sText = getHTTPPage(txtURL) Set FileObject=Server.CreateObject("Scripting.FileSystemObject") filename="../index.htm" Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 openFile.writeline(sText) Set OpenFile=nothing %> <script> alert("静态网页生成完毕"); history.back(); </script> |
四、ASP两种简单的生成静态首页的方法
为什么要生成静态首页?
1、如果你首页读取的数据库次数比较多,速度很慢,而且占用很多服务器资源。使用静态页面访问速度当然快多了
2、搜索引擎容易搜索到
3、如果程序出问题,也能保证首页能访问。
4、其他的太多,自己想:)
应用方式:
如果你的首页是index.asp,你可以生成index.htm (默认访问顺序必须是index.htm,index.asp)。这样访问者第一次访问到你的网站的时候打开的是index.htm 。你可以把网站首页的链接做成index.asp,这样从网站任何一个页面点击首页的链接出现的就是index.asp,这样保证的信息更新的及时性(毕竟index.htm需要每次手动更新)。
方法一:
直接将首页文件包含在表单文本框中,将首页代码最为数据提交,然后生成静态页面。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <% '------------------------------------------------------------ '使用表单提交生成静态首页的代码 '确保你的空间支持FSO,且首页代码内容较少 '------------------------------------------------------------ dim content content=Trim(Request.Form("content")) if content<>"" then call makeindex() end if sub makeindex() Set Fso = Server.CreateObject("Scripting.FileSystemObject") Filen=Server.MapPath("index.htm") Set Site_Config=FSO.CreateTextFile(Filen,true, False) Site_Config.Write content Site_Config.Close Set Fso = Nothing Response.Write("<script>alert('已经成功生成首页!')</script>") end sub %> <form name="form1" method="post" action=""> <textarea name="content"> <!-- #i nclude file="index.asp" --> </textarea> <br> <input type="submit" name="Submit" value="提交"> </form> |
缺点:
1、如果首页中包括<@ ..>标记,会提示出错。
2、如果首页代码较长,用表单无法提交过去(表单数据长度有一定的限制)。
解决方案:
1、去掉index.asp中的<@ >标记
2、使用eWebEditor,提交支持大数据(能自动分割)
优点:
可以在生成时对内容实时修改。
方法二:
直接使用XMLHTTP获取index.asp的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <% '---------------------------------------------------------- '使用XMLHTTP生成静态首页的代码 'Curl 为你的首页地址,确保你的空间支持FSO '----------------------------------------------------------- dim read,Curl,content Curl="http://www.xx0123.com/index.asp" read=getHTTPPage(Curl) if read<>"" then content=read call makeindex() end if sub makeindex() Set Fso = Server.CreateObject("Scripting.FileSystemObject") Filen=Server.MapPath("index.htm") Set Site_Config=FSO.CreateTextFile(Filen,true, False) Site_Config.Write content Site_Config.Close Set Fso = Nothing Response.Write("<script>alert('已经成功生成首页!')</script>") end sub Function getHTTPPage(url) dim http set http=Server.createobject("Microsoft.XMLHTTP") Http.open "GET",url,false Http.send() if Http.readystate<>4 then exit function end if getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") set http=nothing if err.number<>0 then err.Clear End function Function BytesToBstr(body,Cset) 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 = Cset BytesToBstr = objstream.ReadText objstream.Close set objstream = nothing End Function %> |
五、模板分离批量生成
模板文件中要替换的内容均以{…}括起来
为力求简洁,去掉了错误处理代码(replace中要来替换的字符串参数不能为null值,当然fso也应该做错误检查)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <% ' --------------------------------------------------------------------------------------------------------------------- Dim start '该变量为指针将要指向的记录集位置,通过参数动态获得 Dim Template '模板文件将以字符串读入该变量 Dim content '替换后的字符串变量 Dim objConn '连接对象 Dim ConnStr '连接字符串 Dim sql '查询语句 Dim cnt:cnt = 1 '本轮循环计数器初始化 start = request("start") '获取本轮指针的开始位置 If IsNumeric(start) Then start = CLng(start) Else start=1 If start=0 Then start = 1 '如果start ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath("DataBase.mdb") sql = "select * from table_name" Set objConn = Server.CreateObject("ADODB.Connection") objConn.Open ConnStr set rs = Server.CreateObject("ADODB.Recordset") rs.open sql,objConn,1,1 '打开数据集 rs.AbsolutePosition = start '最关键的一步,将指针指向start,start通过参数动态获得 Template = getTemplate(Server.MapPath("template.html"))' template.html为模板文件,通过函数getTemplate读入到字符串,模板文件中要替换的内容均以{...}括起来 While Not rs.eof And cnt<= 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")) genHtml content,Server.MapPath("htmfiles/"&rs("id")&".html") '将替换之后的Template字符串生成HTML文档,htmfiles为存储静态文件的目录,请手动建立 cnt = cnt + 1 '计数器加1 start = start + 1 '指针变量递增 rs.movenext wend If Not rs.eof Then '通过刷新的方式进行下一轮请求,并将指针变量start传递到下一轮 response.write "<meta http-equiv='refresh' content='0;URL=?start="&start&"'>" Else response.write "生成HTML文件完毕!" End if rs.Close() Set rs = Nothing objConn.Close() Set objConn = Nothing Function getTemplate(template)'读取模板的函数,返回字符串,template为文件名 Dim fso,f set fso=CreateObject("Scripting.FileSystemObject") set f = fso.OpenTextFile(template) getTemplate=f.ReadAll f.close set f=nothing set fso=Nothing End Function Sub genHtml(content,filename)'将替换后的内容写入HTML文档,content为替换后的字符串,filename为生成的文件名 Dim fso,f Set fso = Server.CreateObject("Scripting.FileSystemObject") Set f = fso.CreateTextFile(filename,true)'如果文件名重复将覆盖旧文件 f.Write content f.Close Set f = Nothing set fso=Nothing End Sub %> |
Tags: asp asp生成html