  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14191
- 金币
- 2386
- 威望
- 1647
- 贡献
- 1334
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
" R j5 o: f# f* a2 i. @) C- i像www.aspid.cn的主站就采用了TSYS生成html文件! : a! T. ?( @- o0 o) U
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
! w7 N" P' [1 b! O% m% S. W2 f3 {
& v1 ^- _! Y; Y' _. i, V& V- R* w1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
& o0 L. {, C( G& V1 V3 J* ]filename="test.htm" 3 x/ @/ u9 A/ p) T8 K" H: [1 b5 ]
if request("body")<>"" then
! D n) e' f" }0 y; c5 @( ~; T2 Rset fso = Server.CreateObject("Scripting.FileSystemObject")
+ F' T2 F4 n f) V$ ]3 C8 R, u8 vset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ' Q7 B" }; D9 [0 s$ m8 y+ O
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
1 O4 @' W7 n( ghtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
b8 J) G! k0 w) Qhtmlwrite.close # B' T/ v7 y2 [$ C' h
set fout=nothing
, ?" p; x# g! W/ N3 W% `set fso=nothing $ h$ a( E( c1 H3 E; b7 }
end if
; A. t4 O! b8 i%>
/ ^! B, u5 J! s9 c e" b. L<form name="form" method="post" action="">
1 Y* F) ^+ ^1 ]9 w8 f( H! k) q& V0 @<input name="title" value="Title" size=26>
+ O9 s3 ~7 v5 P$ y<br>
/ d% X3 }' p4 M" z! h( ]5 f<textarea name="body">Body</textarea>
, d3 {' P4 @. S7 J1 z+ k' s; `<br> . _' m/ F' a" i( a/ M/ ]. v2 @
<br> 4 D+ ]. T" f) b, o
<input type="submit" name="Submit" value="生成html">
7 F$ h1 d2 ~$ X+ j @: D- A) K</form>
! b' \0 ^ T# U5 Y9 O! X" t0 T2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
; b5 j1 v/ e9 R0 L" Htemplate.htm ' //模板文件 <html> 1 B# F7 g+ |4 e D. y/ B
<head>
1 a- l( G( y9 c& m8 M<title>$title$ by aspid.cn</title> $ G! I+ \& ]3 m( }% O" t6 N1 J$ F! `
</head> + M C ~5 i3 Y; k/ ?; e/ J' d
<body> " x6 r5 d3 l* ?7 j1 ^8 Z7 t
$body$
3 J; o; N: e2 P' r% W</body>
5 ~3 z, M6 T3 Q+ R+ K1 W</html> ?
. b* ^2 v" o1 o/ }' U7 `5 [+ B5 q& f
9 ?7 a/ U# b4 c/ O7 S2 m+ ~TestTemplate.asp '// 生成Html <%
4 `1 M8 Z5 m- zDim fso,htmlwrite
% J0 s9 e, r8 g7 CDim strTitle,strContent,strOut
( c6 P0 P2 E1 d$ N'// 创建文件系统对象
, q& o5 z6 y4 o- e+ p) uSet fso=Server.CreateObject("Scripting.FileSystemObject") 1 x) |9 u7 M1 Y) R8 N
'// 打开网页模板文件,读取模板内容
/ d8 N* e) v5 \Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
% n+ F3 j# {7 D4 [strOut=f.ReadAll
) u* f1 j S1 S( \1 ehtmlwrite.close 2 S8 s: E1 l7 w
8 t) r/ e- Y: G3 i f% H2 R
strTitle="生成的网页标题"
& C: q/ r$ }# |$ ?- @1 `! ~& pstrC ' ?+ J& X/ t- W) N8 i% K# I
, d1 I2 h! g3 z1 u/ X
'// 用真实内容替换模板中的标记
: R4 ?+ s& {" ]/ s* S' ~- ^" g! v, cstrOut=Replace(strOut,"$title$",strTitle) ; x( k1 `6 T8 x' Z- B. R
strOut=Replace(strOut,"$body$",strContent)
/ l9 K% a3 _3 D2 Z# n2 d. x
( k; d. j2 H2 w' H2 z'// 创建要生成的静态页
' x1 n \, ]+ c: i! O: A3 WSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) / N- P3 S1 D4 S; l
/ r. E% t, d: ?' a; ['// 写入网页内容 / d- \) Q/ a" x/ ~
htmlwrite.WriteLine strOut & }* ?5 t$ o+ C) I5 A- i& L* x
htmlwrite.close & z" ~ I# b$ |+ g
' q) v; E4 h3 i" | E! I8 E2 IResponse.Write "生成静态页成功!" # B. `4 c' i% l3 ^; E4 H
% n- j0 D$ c9 f
'// 释放文件系统对象
1 r. c% t1 I6 l- e$ p% c. Kset htmlwrite=Nothing 1 [3 a- h7 F; \' e9 k! }2 N& W, A/ _
set fso=Nothing + b" ^; S! W8 n6 `
%> * |# Z9 v- O% m- L9 o8 X: x2 W
$ g$ C* ?: _/ v2 S) f$ U3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. + q/ B+ K ]5 v4 C8 G. m+ z
<% ! q$ v/ }; o) J4 I
4 C6 T& d' d9 n9 E$ F Z'常用函数 : c w, H9 @( V# D
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 : }" M2 x) u7 H$ T3 S- @$ V7 K
function getHTTPPage(url)
( M. q5 X1 J; Y7 o3 ~7 Vdim Http
% F# P) B+ W) M& e$ oset Http=server.createobject("MSXML2.XMLHTTP")
5 q! k4 ^' v( ^- FHttp.open "GET",url,false 2 z0 f. @% r+ b' U. W* W* g+ a
Http.send()
$ m/ A$ n+ [- [, S$ Uif Http.readystate<>4 then
6 K# |3 o" G' O( l4 I3 b9 _* v& a Uexit function
. ?4 A0 g+ z, k" s I9 j1 Oend if
& N5 d5 [6 l) |; d/ e$ BgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
! x6 D5 E$ ~# c0 G Yset http=nothing ' Y4 @" B1 {% R: m0 }* t
if err.number<>0 then err.Clear 4 ?3 B* R+ V$ n1 q6 @) L1 M
end function
) V4 S: l9 Q0 i6 G3 ]% }& {6 q7 l8 V0 \3 a' r
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 7 b' U' z/ A& ~$ ]: X! J
Function BytesToBstr(body,Cset)
. A8 H0 w6 ?2 Z$ ~dim objstream
# [. m7 f1 ^3 {# d9 s; kset objstream = Server.CreateObject("adodb.stream")
% b# w) q+ x- ?+ j% C# R5 Sobjstream.Type = 1 # R3 d0 A/ }1 P0 A0 V8 w1 Z+ L; n
objstream.Mode =3
9 [! b4 A( e, Y! g' Bobjstream.Open , k9 {. ^3 n# R8 O& h! Y4 `
objstream.Write body 2 E# z% p7 n$ L8 j: F
objstream.Position = 0
. t# A& z8 _4 `- ?' C! D7 Bobjstream.Type = 2
& a* q+ m" r# `4 U; m! Dobjstream.Charset = Cset 2 B7 }" n. {1 H( h# z1 {" e
BytesToBstr = objstream.ReadText
; S& U, k9 K# Q% H7 A/ i! yobjstream.Close
! Z' y) F" i( G( k( Qset objstream = nothing 6 w, N [3 w$ ]" j' u& Q
End Function
- p g% q1 r- I4 c, A* s( N' B( N$ n3 R* T! H; Y' |
/ [$ s, R; B" b7 J: s' Z0 i. StxtURL=server.MapPath("../index.asp")
y. L5 r$ e& x% ]) K% p. O
3 s5 Z% D# y' d' VsText = getHTTPPage(txtURL) 5 y$ U ]" C$ ^' N/ p6 z; P
8 I8 m+ h% E% S# E3 p; A
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
3 I7 K/ R, j; |filename="../index.htm"
( w2 h& \ a" [5 [; d7 L3 C$ M! kSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ; ~; ], }" ?1 v d# i
openFile.writeline(sText)
; g) z, r/ m) `Set OpenFile=nothing
\6 K' s, C* N7 v+ J4 q/ j0 }& s0 e
4 p3 ^) N, h Q e%> 9 O4 p: A& `, T/ j8 E" S
<script> 1 ^* J- }+ ~' W8 R r1 v9 e7 }
alert("静态网页生成完毕");
: H, Q; j6 d, f& @) Ohistory.back();
. Y5 g7 O! s2 |1 }0 F9 K6 X</script> |
|