轉自:http://www.pconline.com.cn/pcedu/sj/wz/html/0704/992983_1.html
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全。前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度。
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件
<
%
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
>
2、但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
template.htm ’
//
模板文件
<
html
>
<
head
>
<
title
>
$title$ by aspid.cn
</
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
%
>
3、第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
<
%
’
//
常用函数
’
//
1
、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
function
getHTTPPage(url)
dim
Http
set
Http
=
server.createobject(
"
MSXML2.XMLHTTP
"
) '//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
'
//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(
"http://www.hinet.net
"
) '//此處一點是http路徑
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
>