  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14171
- 金币
- 2376
- 威望
- 1647
- 贡献
- 1324
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
) m5 @/ Q) |) X* E: F像www.aspid.cn的主站就采用了TSYS生成html文件! ( q, d" g s6 E4 L
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
" |- B) T' x3 b) K
& d/ b% ~6 t$ [6 q4 F) f2 f1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 5 T l/ V. Z1 G) H6 ?& R# g+ ?
filename="test.htm" N6 k0 H( ?! Y, }+ T
if request("body")<>"" then
1 V2 |: T6 I' M$ I: W: Vset fso = Server.CreateObject("Scripting.FileSystemObject") 3 T5 s, _0 {! z. v& Y0 K
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
/ C; U' _1 f! _# W9 ?+ K |" Qhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
f9 d% ]& C' n; J( Mhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
8 B+ J1 R- ?) h2 shtmlwrite.close
/ Z4 D. b, q9 e, B m5 Hset fout=nothing ' p, b/ S6 s! C6 l
set fso=nothing
! s$ e6 b% U2 A/ U3 G' }end if 6 l* b$ O8 u9 e; e9 t+ r5 u) s* H
%>
: f. A3 {# z9 r/ V* |<form name="form" method="post" action=""> ' f, V" o* X* s0 v. d' c" \
<input name="title" value="Title" size=26> ; T: M4 _/ d9 c# h0 z
<br> 5 Q1 b1 ]( U6 i" B2 l: ~
<textarea name="body">Body</textarea> 0 I- w( P L, q$ Z# x
<br>
+ \) A( i. ~1 v5 a8 {- E+ Q% ~<br>
- P) ]4 R! w2 R. `$ b7 u% @<input type="submit" name="Submit" value="生成html"> 7 F; v# @( |# E/ Y0 }) B- T
</form> 6 @& R( o" \5 V3 r- ?
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
# O, j l% V, e$ C* ~2 qtemplate.htm ' //模板文件 <html>
" K7 T8 ^1 f6 V: M6 T8 i<head> 5 D" w1 F- \- w' S" J3 e
<title>$title$ by aspid.cn</title>
: m$ X; W/ q2 x' ?. Q, T</head> + G/ e7 Y' R8 ^7 u* T$ ^
<body>
( U( ^8 X! y/ u- C+ H$body$ 2 E( z; Z+ R% o8 |
</body>
1 h( ]5 o! }2 T- n3 L8 f7 D$ k</html> ? 9 l6 }0 H! z Z% K$ j9 ~+ w
7 s0 k4 p2 o% G/ }TestTemplate.asp '// 生成Html <% # u5 N2 T+ L" U6 K1 ~
Dim fso,htmlwrite 2 y$ Y- W$ z, B9 W( Y! w) j
Dim strTitle,strContent,strOut
0 z5 c+ h# b. W, |'// 创建文件系统对象
) @4 }/ g; `0 E( V& xSet fso=Server.CreateObject("Scripting.FileSystemObject")
S) W# P7 @7 G7 B! f'// 打开网页模板文件,读取模板内容 - B# G) R7 ^0 m, A1 M0 w+ E& U
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
7 p2 v0 R0 `/ e2 v4 istrOut=f.ReadAll
0 {# [2 I b2 f+ Ihtmlwrite.close + V& Z2 o9 C: n3 i' c% Z
$ Y$ b! u, h+ f9 }
strTitle="生成的网页标题" e/ a. M& C: W+ ^& G
strC
/ K$ W, r; u) R" n. y- k; T4 @# L9 ]& |1 H
'// 用真实内容替换模板中的标记
# T" z: b# w) A. p4 m7 X5 H! D4 h; QstrOut=Replace(strOut,"$title$",strTitle)
- c. l: v3 o9 u( AstrOut=Replace(strOut,"$body$",strContent) * \9 }: p! j4 T$ x. {
1 U4 g3 [8 Z3 ?, d# u+ ?) H
'// 创建要生成的静态页 7 P. _ w. T! \& w d' O; C
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 2 W( ^) @) _; `# l1 o- \6 b9 v* ]
# I- ]5 j" N# K/ A# E& j
'// 写入网页内容 0 d6 N3 D' V. \, H$ c, [
htmlwrite.WriteLine strOut
0 D( V2 u" \( G) r, r/ c7 }9 thtmlwrite.close
+ J: i2 `% l0 |4 ?% F# q: i* _, L8 Y
Response.Write "生成静态页成功!"
- b: j9 q4 r$ L( h1 N, b# |) }! L% s
'// 释放文件系统对象 0 ]; f5 |$ W# K6 c
set htmlwrite=Nothing 9 |1 ~* u: p- J2 F0 n2 O4 c
set fso=Nothing * R; V" R8 W2 C6 E
%>
) C0 p, x! @+ p7 q
# z0 |4 |; ~. c$ J2 f3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
7 W# K2 J1 p: T0 z7 l/ ]' ~<% ! e* e) P \' G' @- ^
* v$ Q9 D: Y3 q% H'常用函数 ) q, R$ ?- h. ]9 S5 K0 x
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
6 i! Z. y3 ?7 N5 l& cfunction getHTTPPage(url)
; m. [: Q" N7 b5 B( Adim Http
/ A' {/ V; f& j% g3 ~3 b" t H! sset Http=server.createobject("MSXML2.XMLHTTP")
, q- U% l/ p4 y4 HHttp.open "GET",url,false $ G4 \0 }: w, W: O( g$ N. n' p
Http.send()
9 k n. y1 B. Fif Http.readystate<>4 then
. S% I, V6 |2 H7 t3 k- rexit function
5 S! K8 Z! [* Aend if # d6 D( L* J6 }1 l6 z
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 8 l6 [& m0 N9 L/ [5 s
set http=nothing ) @' O7 p2 R. {$ g P, _$ d- c
if err.number<>0 then err.Clear
1 v+ B6 D3 _3 z9 j. `. vend function ' R) \( a- l$ i$ h- k
2 D i3 H* W8 v. g
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 / p/ x& p$ q' {- o7 i* W) x
Function BytesToBstr(body,Cset)
; u( D3 T$ o G1 z9 O0 C* l$ Ldim objstream 5 J7 e# Q/ t! A8 n+ Y
set objstream = Server.CreateObject("adodb.stream")
3 R8 C3 h3 j \3 ?5 v! N# }; hobjstream.Type = 1
( _6 P; Z* l8 { j- `objstream.Mode =3
! C" y3 t3 \$ i* }: b( ~+ V' kobjstream.Open
^3 ~* x' g) G2 Eobjstream.Write body
# p# b! x/ n6 z9 F# u, Bobjstream.Position = 0 ' C( C+ h2 O9 C
objstream.Type = 2 # q4 g( @1 `0 [* `, a' L( `! ~
objstream.Charset = Cset . I: T2 b4 [ `& }5 g) K
BytesToBstr = objstream.ReadText
: v/ i% z' Y, Q/ e; uobjstream.Close
5 E1 f; s% |' x1 D6 ]% N. r) ?set objstream = nothing 8 } z' P# C: Q* x, i& D9 z
End Function & m: Z: R+ @0 `$ T6 K5 N5 B4 u
, }, j5 q) n1 Q$ J
& ]- l5 q2 W0 Q6 StxtURL=server.MapPath("../index.asp")
, K3 {& k. @* e1 N7 p
+ A& T3 l5 c& L# ?sText = getHTTPPage(txtURL)
: W6 y% r! n8 A6 X3 {( t' {
% v# \6 Y& d# sSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
: _" f) S+ |0 nfilename="../index.htm" : S6 ^' L0 d( ~" Y
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
" W* P; P; }' z3 I5 q1 O" {openFile.writeline(sText)
# [! M: u7 v% o+ BSet OpenFile=nothing
( a. B0 }! h6 P$ w
2 S2 L: y3 A" n" C1 o, _$ @%> x# w0 |# g6 m- F6 b, L5 N
<script> , b4 v5 e ~1 P' } n5 I1 |* n4 o" W6 Q
alert("静态网页生成完毕"); 7 ?- T- s, J/ x
history.back(); % N- c7 t9 L" @& R9 l9 o
</script> |
|