ASP开发之生成静态HTML文件的方法比较

在ASP中,将动态页面转换生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录(特别是对接受动态参数的页面)。前台访问时,脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度。
当然,凡事有利必有弊,生成HTML页面无形中也耗费大量的磁盘空间以存放这些静态文件,在编辑页面过程中除读写数据库外,也要读写服务器磁盘,页面样式的改动必须重新生成全部HTML文件,等等。
像很多搜索引擎,都可以提交网站的页面地址列表,动态文件的收录问题已经不算是个问题了(如google sitemap)。得失就要自己衡量把握了,但无论如何,我们还是要懂得如何操作的。这里就引用一下别人的文章说明几种常见的生成思路,供大家参考参考。
1、下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 。
这是最原始的,优点是简单,缺点是页面的修改不方便,我一般用到的地方是利用它生成整站参数文件。(通常网站如标题,名称等配置保存在数据库,我将它生成config.asp保存这些变量调用,避免频繁访问数据库)
以下为引用的内容:

双击代码全选

1

<ol class="dp-xml" style="margin-top:0px !important;margin-right:0px !important;margin-bottom:1px !important;color:#5c5c5c;list-style-position:initial;list-style-image:initial;word-wrap:break-word;word-break:normal;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;background-image:initial;background-attachment:initial;background-origin:initial;background-clip:initial;background-color:#f7f7f7;padding:5px 0px;margin-left:55px;"><li><p><%   </p></li><li><p>filename="test.htm"   </p></li><li><p>if request("body")<>"" then   </p></li><li><p>set fso = Server.CreateObject("Scripting.FileSystemObject")   </p></li><li><p>set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))   </p></li><li><p>htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"   </p></li><li><p>htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> </p></li><li><p>输出Body内容:" & request.form("body")& "</body></html>"   </p></li><li><p>htmlwrite.close   </p></li><li><p>set fout=nothing   </p></li><li><p>set fso=nothing   </p></li><li><p>end if   </p></li><li><p>%>   </p></li><li><p><form name="form" method="post" action="">   </p></li><li><p><input name="title" value="Title" size=26>   </p></li><li><p><br>   </p></li><li><p><textarea name="body">Body</textarea>   </p></li><li><p><br>   </p></li><li><p><br>   </p></li><li><p><input type="submit" name="Submit" value="生成html">   </p></li><li><p></form>  </p></li></ol>

 

2、但是按照上面的方法生成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

35

<pre class="brush:java;toolbar:false;">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  

%></pre><p>

</p>

 

3、第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。找到一段XMLHTTP生成Html的代码参考一下。
以下为引用的内容:
常用函数:
1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码

双击代码全选

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<pre class="brush:java;toolbar:false;"><%  

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 % ></pre><p>

</p>

 

2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换

双击代码全选

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

<pre class="brush:java;toolbar:false;"><% 

nction 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></pre><p>

</p>

 

小结,这三种方式是比较常用的生成HTML文件方法,我个比较喜欢使用第三种方式,因为页面改动时非常方便,就算动态页改动多大都好,只要重新用XMLHTTP读取生成一次即可。