  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14247
- 金币
- 2414
- 威望
- 1647
- 贡献
- 1362
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. / D$ r8 v) Y: S9 V% h
像www.aspid.cn的主站就采用了TSYS生成html文件! B" f& c8 X2 T2 }. K
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
$ _4 d; x1 U7 s' \5 A( i3 `8 u' X7 c8 T4 b L% L# Q: _
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
: Z) T% n2 Q. ^3 ^filename="test.htm" - | U* ?+ w n; K2 B; w3 p W
if request("body")<>"" then ( b% e3 T5 F2 K0 T+ j6 I
set fso = Server.CreateObject("Scripting.FileSystemObject") 8 D+ s9 ~ o! K- S1 h7 F$ c; y2 _
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
0 \1 V4 v: ~% }# Y4 ~0 F' Zhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
7 ~* H4 P" @2 R0 c4 b" W( {htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 9 `' S6 A* _, @# B4 i' v
htmlwrite.close 7 K6 e" c& Z. w5 ^, O( w* ^( `! Q
set fout=nothing 8 z: F7 P$ \4 [% |. w2 d% g
set fso=nothing , N! B) e) F) i# @: }( n% e @
end if
5 E4 j% L, x5 I4 l( H @; c9 N%> 4 o! g; c( i# t& M9 j: }5 |) b
<form name="form" method="post" action=""> " X) ]+ f) L9 F) ^$ L
<input name="title" value="Title" size=26> ( q% w5 l0 D, k3 C7 K- [
<br> ( P! ^9 A1 n% D7 Q- C
<textarea name="body">Body</textarea>
( |& P' w2 K) q5 r0 M8 z% N<br>
2 d2 W: T% t9 z& E<br>
- D5 _) Y( M3 \* _$ C<input type="submit" name="Submit" value="生成html">
* O. `% Z" `+ J2 C& Y4 d: Y( M3 I</form> ) S6 K3 }; F3 C! ]% Q m1 ~& @6 H; V
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
! I- G2 I$ a5 d5 i( ztemplate.htm ' //模板文件 <html>
. m! h* k) e* d# e$ z<head>
4 Y2 C5 G3 {- }3 Z<title>$title$ by aspid.cn</title>
, j# D$ \1 u0 X" N5 Q1 j k% _0 x</head> . d! P) A+ V- p8 ~
<body>
. M& V! @. M5 l: Q( y R/ R$body$
6 F4 y+ R; K. @3 p l6 F</body> 2 ~% o, r) i% M9 s" m! J
</html> ?
& t- A1 l* H; j# I2 ?8 d2 _3 F% s( l$ B
TestTemplate.asp '// 生成Html <%
/ _. [* ^: o5 H. c& {Dim fso,htmlwrite
8 E$ q6 }3 p2 ~, FDim strTitle,strContent,strOut ' m9 u* T) C3 h1 B! Q2 H# V! c
'// 创建文件系统对象
- F! y4 b. N) w8 ~3 E# F2 BSet fso=Server.CreateObject("Scripting.FileSystemObject") 6 o8 N- y7 u0 C! A! f/ S
'// 打开网页模板文件,读取模板内容
9 f. }. n) P$ l( }! {) u% `, vSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 7 J/ J- Q/ ~: q0 U$ W! n
strOut=f.ReadAll 3 h8 T0 c# X0 G4 {
htmlwrite.close ) e2 A( E! ^% _8 C: k) J+ C4 [
( P# ~: s1 `. `4 U+ D3 o5 [
strTitle="生成的网页标题" ; u% o6 q# z; V+ i# C r; k
strC ( K: s$ a+ _4 v+ E: M
$ M) Y& r- f& l; c- t( q'// 用真实内容替换模板中的标记
* B" Q2 {2 {) Y* p/ C" t0 \strOut=Replace(strOut,"$title$",strTitle)
8 D! U9 ^! m2 N1 z3 \strOut=Replace(strOut,"$body$",strContent) " g& R* W) S+ h/ _' Y- W8 P
6 H- T6 @2 o+ g- F9 U( l
'// 创建要生成的静态页
$ J' Q6 L- J1 A% ?% [Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
@# {4 d- _9 X1 }( k5 }. H! N/ z% Z8 _% b5 W" \2 X
'// 写入网页内容 - h, u8 `9 \( c& ?5 c
htmlwrite.WriteLine strOut
h$ I6 R' o9 e( G/ `htmlwrite.close
3 E- l4 C0 s6 K
F+ D: K$ e9 K: DResponse.Write "生成静态页成功!"
! [) E: g" f9 x& v5 X+ i L; n$ ~
4 x5 k' ~9 a- @7 S2 h7 {+ s'// 释放文件系统对象 5 X3 ~$ r4 @$ @: f3 o) s) |
set htmlwrite=Nothing
9 v: O6 B) I8 i o) ^! t/ W& yset fso=Nothing
0 c" f# `+ R; M( z; z%> M8 q3 V4 n4 V6 L3 B6 O
1 l5 c2 p* p% A; I
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. " b, z" w- U$ W* ~6 g" p$ }
<%
5 H8 }# w( p c- B
4 m' ?1 L& q* J'常用函数
8 e: {( k. @; p+ Z'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 9 d* I% p; D8 G: u5 W
function getHTTPPage(url)
% m- t5 d! I0 hdim Http + {5 _7 Y: ~" m* _# H. M8 C
set Http=server.createobject("MSXML2.XMLHTTP")
/ t$ d4 Y' c% `! ~* U" iHttp.open "GET",url,false , Q: t2 J0 I/ x; e' J
Http.send()
^, o3 |) _7 q( s. q2 u5 m# a+ xif Http.readystate<>4 then
+ ]' V7 N" O( Z7 |/ |4 y# I, O& { nexit function ' d, V5 ]9 M7 r
end if . Y# w( o: F2 x& y2 @# C* H
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") ( G- p$ e: l$ u* R8 \! [% R
set http=nothing $ }6 t2 @ A2 A7 f/ H
if err.number<>0 then err.Clear 8 W* J( z$ \* a$ h: s% A
end function 5 N9 }# K* D& I" Z5 _) F
9 M: m7 b/ Y L# ]
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 2 z' ^3 Z z+ }: Z) I5 [
Function BytesToBstr(body,Cset) . L" ~6 M# l+ L2 W; ]
dim objstream
" g* y4 A' X0 ~- rset objstream = Server.CreateObject("adodb.stream") ( A! F' F H8 U4 u8 m: R; [- R
objstream.Type = 1
6 W( e" c. s6 B5 Hobjstream.Mode =3 * l& a9 w$ ]" j+ V, K: k- z, ^7 F
objstream.Open . O: z* J' Q/ i1 o4 j, F
objstream.Write body ( d! W* w) E e. m, m. J6 g9 ~" y
objstream.Position = 0
\$ s, d @& Y. J* p1 `: Eobjstream.Type = 2 ( q. R- t* J% m
objstream.Charset = Cset
6 z J3 i0 D* X) J- h ?% c/ LBytesToBstr = objstream.ReadText
0 k4 k2 c* f9 ?8 V# nobjstream.Close - S, i; i) [- u+ u8 A, s' m
set objstream = nothing 6 S7 V# t0 m: z, X8 r
End Function
5 R4 n& V6 {+ A5 X. ? G) k
3 c4 _8 ]# |& ?0 v8 ^* ?% u; k) K( T4 R: t# a
txtURL=server.MapPath("../index.asp")
: j* d$ h; I: K. u; u3 T$ P$ T, y+ L. i. t0 ^" u1 Q W
sText = getHTTPPage(txtURL)
* \6 g9 D! v# q3 J5 Y
9 s \/ S$ F9 _Set FileObject=Server.CreateObject("Scripting.FileSystemObject") & B5 w, ?+ Y- m% [+ ^! v) @
filename="../index.htm" & t( `1 i$ e3 ?1 N
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
- W) n9 p0 }8 K% l+ |/ @openFile.writeline(sText) ( ~ L3 R7 _- \: g5 @+ X4 Q1 G( [
Set OpenFile=nothing $ E2 ?3 b3 V! s' H9 W$ ]1 u9 a
' W% k7 ^( x; H% ~' }: h%> ; k& Q8 t6 e4 s, K: h* C& G
<script>
: d6 N, e0 m# c/ v7 g0 W$ D& Ealert("静态网页生成完毕"); - y1 x3 ^ w' s" V* E5 e
history.back(); 4 C9 ^1 U# s9 I6 j/ j( q
</script> |
|