|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14395
- 金币
- 2483
- 威望
- 1647
- 贡献
- 1431
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
! n7 Y9 S Z, K3 ~" ]$ [像www.aspid.cn的主站就采用了TSYS生成html文件!
1 ]' v, i8 c" W所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
7 e7 H7 u& m6 S" R$ D, x5 s2 {% a+ J
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
: a" d- V8 n* G' W/ d) M( tfilename="test.htm" " T. T# ?. v) u
if request("body")<>"" then - K, ` I* a" N I# |5 \
set fso = Server.CreateObject("Scripting.FileSystemObject") . ]" I# ~! m/ h- f
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
8 K2 L3 S+ q. k, {1 dhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ( w9 k: n; f( l5 W
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
. Y0 e D1 C, T( Q4 lhtmlwrite.close
0 c2 R9 L1 w8 m6 ]set fout=nothing ; p( ?" O3 B0 V. _' f1 s# f o
set fso=nothing
$ d( ^' v( z* E0 mend if
# g. C4 E8 q+ r B, b" H%> 6 B6 \ w& M& n$ C) b
<form name="form" method="post" action=""> 1 X0 L: ?# H. m* F% v
<input name="title" value="Title" size=26> 5 ?1 p1 c$ {! |4 q% r4 X; Q/ H9 P
<br>
7 F- D% J! m* x; E<textarea name="body">Body</textarea>
/ c! F& ?- @' _ m& H( ]<br> % N" U# ~/ Y% e" t4 E* k) a
<br> * B& Q- B& g: b; m, z+ R
<input type="submit" name="Submit" value="生成html"> , K6 r9 _4 S1 K: A
</form>
; K: ~1 |: I3 P) n& ]! f% A+ r2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. i' p) \- B( v$ |1 N* J6 z
template.htm ' //模板文件 <html> 9 b& I: D& D1 {$ d# }
<head>
; }. y' y E# n) M/ H' U<title>$title$ by aspid.cn</title> z: @: C3 b' w, Y7 U
</head>
$ ]3 r% ~7 F( A0 ?<body>
1 C/ Z9 M/ `! t$ q @2 @$body$ 8 W: d# }1 \$ X J9 Q9 B
</body> : L: K* d3 }) Y. ?# b
</html> ?
# s5 l g9 S5 G' P: D3 S3 l+ E7 j' _4 }
TestTemplate.asp '// 生成Html <%
! P5 }5 Z t6 I3 i U! C! mDim fso,htmlwrite 3 G9 r W, A1 F# c. d+ q
Dim strTitle,strContent,strOut
) b3 N* |6 f% `6 ?& \1 C$ c'// 创建文件系统对象 / n5 i. h# l) Q6 k) \
Set fso=Server.CreateObject("Scripting.FileSystemObject")
& }$ R2 ]2 ~: Y'// 打开网页模板文件,读取模板内容 # ?& ], Z) W/ V( |7 R
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
4 @8 Y) T/ F4 F( T3 y- l6 PstrOut=f.ReadAll
! W" d3 B0 z4 c" c$ rhtmlwrite.close 8 n' P3 H; E- |' a5 P: l' O
Y3 } _# @5 g/ m# O8 @; [
strTitle="生成的网页标题" & c! R7 |$ v' Q: e5 x
strC : O& I D$ @% a& Y1 b, s
d x! _7 y7 g3 m: C" N! e
'// 用真实内容替换模板中的标记
* R; Q0 l) O [! g5 y- X2 ^4 hstrOut=Replace(strOut,"$title$",strTitle)
! C8 r$ I3 c; Y- D4 HstrOut=Replace(strOut,"$body$",strContent) : _2 x2 @& ^; ^+ F2 X
1 m( @/ b+ N5 @
'// 创建要生成的静态页 9 w6 |$ O9 ?: P) @' E5 K" @2 ?
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
/ _' B" A5 K s* U: ?
& i* o, P( ]% J' N, n4 F! C'// 写入网页内容 / O6 T. |- V% f7 c2 j q q3 }
htmlwrite.WriteLine strOut , J) _% N. e, D! M# p
htmlwrite.close
3 s+ A4 \/ `* G7 \; M
8 R+ j, W5 t0 a) W* h8 \, bResponse.Write "生成静态页成功!" 8 g, k L& Q1 l. y0 ~
' a& h8 z' I* e; x& a0 c
'// 释放文件系统对象
! ~" B) v& k0 g6 ~( L* jset htmlwrite=Nothing * n) ?& G$ g' ^4 N- b1 ^
set fso=Nothing
" Q' C1 R6 _, _: l%>
, ^* L& A2 e9 g" H: E: D
' k9 j: Y" [& w3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
% I5 l& u3 ^# p$ d, b% T<% ' G* M, e$ V4 s: S6 x! e8 O
2 c; F4 ^" q, D3 s% I: \' _" R
'常用函数
; z% ~. g- ] B/ M; g'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ! Z5 W$ u1 c1 g" g* B* [; _
function getHTTPPage(url)
5 b; }2 X5 r( U1 [dim Http
# P: r% ^$ X+ Lset Http=server.createobject("MSXML2.XMLHTTP") H; S! M% [ |! m
Http.open "GET",url,false
, w4 ?9 d% K& N7 T: D+ Q: K) J y7 NHttp.send() , \4 G6 j$ S( z: M) r4 C5 Z
if Http.readystate<>4 then / T9 l, P' G1 w2 W$ [
exit function
& l/ L: D/ L8 Jend if 3 F; Z# V7 c9 I% }* H) f4 X& v
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") $ y0 a: U& y) b7 q
set http=nothing # A2 }1 n7 z- h3 \& i
if err.number<>0 then err.Clear
" j3 V- a# }! e5 qend function $ ~ }0 r' W/ m4 n2 c
; ^7 e+ ?; S( J7 }( n* Y'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
# d; X6 n6 ?, d# ~Function BytesToBstr(body,Cset)
V4 F* Z' q, M& A2 Sdim objstream # J7 P- `2 n, b1 u2 M5 j
set objstream = Server.CreateObject("adodb.stream") ! {; G O. ?# p# M" A
objstream.Type = 1 9 i* o% N' W4 O/ M" K
objstream.Mode =3
( `3 A$ D1 y! N$ P! f$ j2 Gobjstream.Open 1 d1 B8 S( T* \+ ?
objstream.Write body
% D' @% O& [9 W5 jobjstream.Position = 0 3 O5 b5 F; V9 p1 [3 B2 ?4 K4 g3 K
objstream.Type = 2
) P" q p ~. C" }objstream.Charset = Cset " c7 K5 P8 a# d; S6 Z
BytesToBstr = objstream.ReadText
! d6 Y9 V( O8 s0 V; G7 Y0 \2 B9 Sobjstream.Close 5 y3 i! E* J3 W/ x. U' w3 T" X
set objstream = nothing ; k# _2 m# v% K0 }7 N
End Function
8 C" q% H) T2 I; S" u7 | c2 d* V' j3 Q# l! t6 g3 |
) g) n# r+ J3 Z2 C
txtURL=server.MapPath("../index.asp") * s+ `$ e8 S _7 h
) \- a' C" x/ B# ]
sText = getHTTPPage(txtURL)
8 I+ u" U2 @" b) o4 N: h$ l7 j
$ r. I5 p4 Y+ c F8 p- WSet FileObject=Server.CreateObject("Scripting.FileSystemObject") # j0 J) c) Z, d, p
filename="../index.htm"
9 `, J- g* Y: jSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 E9 A: Z* R* Y l: R/ `
openFile.writeline(sText)
4 b1 [4 |$ q+ C( ?4 A+ O+ vSet OpenFile=nothing 9 f" z( \ b6 @) K
) p0 W' g$ W% t3 {; _" N$ c%>
. y3 z% E; s8 F<script>
. I; m* S- i$ x- i+ |4 Salert("静态网页生成完毕"); ! J" V$ b* y* F, Y- \% O
history.back(); , b4 |5 v5 L8 K
</script> |
|