|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14391
- 金币
- 2481
- 威望
- 1647
- 贡献
- 1429
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. / x: g9 N' G g" r: X! c" X( P
像www.aspid.cn的主站就采用了TSYS生成html文件! ) J Z b/ W$ Z& N* H
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 4 W! s) ?. I" |. m
. s6 c1 `) t0 C/ X1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
( `+ v7 y% O5 S3 ffilename="test.htm"
1 K/ [0 j$ q; w& }& m. A2 z& Pif request("body")<>"" then
7 @9 Z: B, k! Q5 P# m nset fso = Server.CreateObject("Scripting.FileSystemObject") " T% ]$ p( `) |0 @4 f
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) % [+ b( |/ s# I( U2 y4 T3 @( A
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 0 K$ D4 w7 z& \* j
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 2 i. b3 V' K& O; n+ O: Q3 l. R
htmlwrite.close 7 R6 Z; e9 N' @. Y
set fout=nothing 3 { s0 s8 G- P( o( k c
set fso=nothing
" C* e" v: i$ A" ?$ [3 g: S8 Cend if $ }2 K1 a4 g4 x
%>
' u, q5 w J8 R0 F( [0 `5 i0 U( [<form name="form" method="post" action="">
- J/ a8 z( c" Z& S<input name="title" value="Title" size=26> / G1 s& p$ ?( s" I3 [
<br>
b; [: r6 L' h/ H: B4 z# b<textarea name="body">Body</textarea>
5 m. B% U9 M- ~& ^<br>
7 q6 u+ X" u1 y* |' I$ s<br>
0 s' J6 G# x5 v<input type="submit" name="Submit" value="生成html">
( X. w8 W Z) Q+ v. H7 b</form>
- j: D9 r' I# K4 s2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
3 ?: s* ~7 X, k7 c8 A& L) m8 X- X* Ltemplate.htm ' //模板文件 <html> $ C" Z# H2 [* L% H( g0 F4 r
<head> 9 `1 o( f' g- k" S9 q
<title>$title$ by aspid.cn</title> & J+ n: [- U# ]/ t
</head>
$ d2 E( g* r! H$ j. I<body> 9 ]# n y7 y0 z
$body$ , h4 Q- W; Y6 X# e+ {5 D
</body>
( n* p6 n" Q$ c9 F: j1 x1 ?</html> ?
9 J' X$ S' a; w8 k: ]% n# W2 m7 x# m* ^) V
% F$ ^. {6 w2 q) bTestTemplate.asp '// 生成Html <%
; @/ r1 Y ?! g) u+ tDim fso,htmlwrite
4 X. v3 T1 p. `- q- d0 _Dim strTitle,strContent,strOut / I% n& I7 P. v$ }! z& M9 d+ ~
'// 创建文件系统对象
; C2 Q7 J& D/ i2 w1 Z% l/ w3 I, kSet fso=Server.CreateObject("Scripting.FileSystemObject") 2 s& F) b' B. c
'// 打开网页模板文件,读取模板内容
2 k, W7 v$ ?* F K# [( oSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) / M; ]1 C# k# `
strOut=f.ReadAll
9 `2 N5 h- f+ y) w' ]htmlwrite.close
6 \& ?3 A3 | J9 A2 w' L) E& B1 I( g% K: G0 Y; |# p, x
strTitle="生成的网页标题"
; m, N5 l) {# Z' x/ istrC . h8 N7 ~2 a7 ?5 Y4 {- X/ p$ J
% ?# a6 B! ?. [* G( ]'// 用真实内容替换模板中的标记 1 F8 d( Q6 v& b1 ]8 _. o9 P
strOut=Replace(strOut,"$title$",strTitle)
) j* e2 f# m: n3 B1 g) D" FstrOut=Replace(strOut,"$body$",strContent)
$ F2 n8 Z0 n) }& b& \3 Z8 X7 N8 \% r. ]
'// 创建要生成的静态页 8 p- Y7 a3 o" p* ^8 W; D
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) $ X! Z) y3 {6 ^" v& Z# c$ _
0 K5 d9 T! q% V4 |' Z3 G( _
'// 写入网页内容
/ R! p2 A8 b; d! g _) f, j- ehtmlwrite.WriteLine strOut
/ N1 O4 f p" _- ~: V+ x, o, H) Whtmlwrite.close
+ e6 w G- D# D% T) R) P* r) O
. c7 }2 }. t) D1 mResponse.Write "生成静态页成功!" 6 b& n1 W1 W9 W
) i3 Z( s1 K: Y'// 释放文件系统对象
. S0 L" n0 Z3 D4 k; I4 m1 xset htmlwrite=Nothing , x3 I- w+ H5 ^( M0 e- L% T
set fso=Nothing
+ C. ~+ g! ^! F# i" @: a/ d%> 5 P! M9 l+ A: ~. E0 A& W, `
& e- e8 `% g1 F- I7 p4 t3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
" l0 p2 G/ ]6 o* ]: H( b; q<%
; }+ [# \8 s: @# K! d) }+ W' I% |) U* B& \0 \3 ]/ m# r
'常用函数
' c9 c0 }, \& ]/ s" M% T; N x! a'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
, j s+ \9 ]' |8 ~7 a5 Xfunction getHTTPPage(url)
- A; x, Q. j2 m: [4 rdim Http
4 g- U! V& R, g8 {set Http=server.createobject("MSXML2.XMLHTTP")
" x |4 L2 T1 O5 H8 P9 fHttp.open "GET",url,false 7 ?; R! b o8 [% b% N! G
Http.send()
/ ^: c) |5 k% r6 V% r9 jif Http.readystate<>4 then
) @/ @/ c2 Y# @% D4 ^" w; c1 P Xexit function 3 w: ~! W; Z4 v; L# Z% l) `
end if
! f4 s& D: {- s8 ?& X$ ZgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") ; S5 y8 W I' z9 b/ i) n0 I
set http=nothing $ o# {8 F7 g& H/ ?/ A; f
if err.number<>0 then err.Clear
! E0 M# `/ j5 `0 q! G: I+ Y+ \. W3 \end function ) } D. ^7 a* d. C0 Q2 m2 W
+ i# T- W- M- Q# F
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 3 ~& J. Z2 U9 m4 J% ^0 O
Function BytesToBstr(body,Cset)
7 @! I4 D3 u0 W6 y( xdim objstream
1 n7 b: A6 t0 T. d8 {4 x0 vset objstream = Server.CreateObject("adodb.stream") 4 N9 L% D0 R9 W. {+ }; S: X, I* ]
objstream.Type = 1
8 g# P% T7 n8 [% W: \: }objstream.Mode =3
1 A: N1 B+ Y/ _. e7 K: Zobjstream.Open 4 k% s1 \1 x% f: r5 w# Y" B
objstream.Write body ) z( v/ m9 |+ H; V7 k1 w2 s
objstream.Position = 0 & j6 s6 l& C1 \
objstream.Type = 2
' z! Z4 {1 t* u& K# _* W! a B4 Robjstream.Charset = Cset $ r3 j, V/ _' K0 m2 h2 P5 f
BytesToBstr = objstream.ReadText
; |: _ d# \, U1 S# cobjstream.Close
! Y6 U0 D: z( O: w0 Cset objstream = nothing % p1 e/ b: m) g$ B% v7 Q# Q7 y
End Function
- M* v( R8 Y; e5 c* D6 e2 R0 r/ }6 Y
/ s+ l! f) a" m4 N% F8 g* D
txtURL=server.MapPath("../index.asp") - r M( i; K9 o- ?# _8 E
1 P- B* ?5 Y( S+ @7 E
sText = getHTTPPage(txtURL) ( E- o$ u& N! ]' x% Y
( d4 q; l. i" h: ~3 x. lSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
7 a( _# U! a' H9 u9 G6 _filename="../index.htm" , B4 [7 B5 B/ G$ x y- S
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
3 L$ J2 Z+ t/ s" ~* L$ @( I* kopenFile.writeline(sText)
6 I/ q$ ]* o0 J% f2 w2 S& USet OpenFile=nothing
. z. g! [* k5 A6 P: |7 v9 W. I0 b/ @
%>
5 |" o8 F4 q' H6 A; T! k<script> , z. X2 `( t/ D5 Q" T$ P
alert("静态网页生成完毕");
2 v- i5 N5 L; b, ]2 lhistory.back();
6 y( M) X2 x5 h/ N</script> |
|