  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14247
- 金币
- 2414
- 威望
- 1647
- 贡献
- 1362
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 3 Q" Y3 \* z5 I1 h5 K0 O$ z
像www.aspid.cn的主站就采用了TSYS生成html文件!
6 i' d1 \+ ]& L5 P! e所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
* [7 w. `2 D: `) I8 A7 R; Z/ s! \% }9 k- L# B" v3 O
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
$ ?' w4 r0 B5 e6 U( c: n8 Ifilename="test.htm"
; }3 u) h# Q5 }if request("body")<>"" then $ e: K$ }" {4 |
set fso = Server.CreateObject("Scripting.FileSystemObject") : C/ }, `& U+ @1 j3 ]
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) / q# z: j2 e% y$ g- l
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
1 b7 y% X# K3 j4 l+ Dhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
- I6 { y* x9 F' e* s" yhtmlwrite.close & O9 X# D) }) |/ _5 t
set fout=nothing ! s5 y% x0 ], \# h6 P5 D
set fso=nothing
8 B% h2 T3 E/ B- Y _* N, uend if
' k2 c7 M; U' B* d5 g6 h2 d%> 4 j( T2 \# a1 \8 F/ h
<form name="form" method="post" action=""> & d" C9 G% z }) D
<input name="title" value="Title" size=26> - p: b0 }2 S7 I, f$ f8 l
<br>
& @+ @) P+ U3 Y+ q<textarea name="body">Body</textarea> / h9 s8 w9 v& X, y
<br>
) C1 ?# v/ b- W* W9 B/ J! K# `. W<br>
2 }3 n4 b% G* ^& I6 n1 u$ Z+ w<input type="submit" name="Submit" value="生成html">
& d- ?! O m; c9 T$ o2 N</form> ; F0 o. x w0 g2 `7 X
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ; n! A7 k: e8 A) q
template.htm ' //模板文件 <html> 8 ]( \ g- }6 w
<head> 9 e4 ^ B8 t$ o' f3 I+ U
<title>$title$ by aspid.cn</title> ! w% u! f9 P# o7 Z' F; S
</head> " Z; Z0 K1 _3 ^
<body> & f: F: }* m/ X2 o9 ~% N
$body$ 5 i( E ?2 c: U5 m. B
</body>
, X B( w8 V/ Z& C2 ? w</html> ?
5 v) ]9 c D0 Y0 i/ X4 A0 r
: D: j/ Q2 \& z' K. P. zTestTemplate.asp '// 生成Html <% f5 ?' K/ g+ v0 [0 S$ G
Dim fso,htmlwrite
. E) C( D) b! \; H1 J! p' XDim strTitle,strContent,strOut " H/ z( @; k% h) i* V0 D A4 N, ^+ R
'// 创建文件系统对象 . [& ]4 q" f# ^) @' ~, {$ }
Set fso=Server.CreateObject("Scripting.FileSystemObject")
1 i8 c& \! }* ?, p! p" x3 R'// 打开网页模板文件,读取模板内容 7 d* \3 H: o. X: c- e' i
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
& q1 @& w0 Z# `" p0 V, zstrOut=f.ReadAll 3 W1 g$ m q h. a1 Y- }, i' p4 k
htmlwrite.close ) f( A& g O3 q8 n/ g2 P
, ]$ x% @9 E2 {# n, g7 k, o: y$ C
strTitle="生成的网页标题" . }) ]- V5 V6 b
strC
1 k+ P4 X0 C+ u' ^/ B4 M4 S' u( N6 s& Q
'// 用真实内容替换模板中的标记 0 A/ }. t3 m; a1 [4 |3 I0 u* C- r0 G
strOut=Replace(strOut,"$title$",strTitle)
+ S% i5 c" _! zstrOut=Replace(strOut,"$body$",strContent) & Z' T$ Y: v: j
9 j+ N4 G6 W* m2 Y n3 s
'// 创建要生成的静态页
' B5 D% \! W% c- OSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
9 c: [+ A/ R+ d) P
n; E: S8 G. a4 u+ t'// 写入网页内容
- ]$ T- g& f6 M: bhtmlwrite.WriteLine strOut
6 `* e5 _+ O9 r2 h: hhtmlwrite.close
3 [/ s' k: L$ e: H( y7 {" x7 }% G" Y) C( h5 t% g) _
Response.Write "生成静态页成功!"
8 u+ p: g$ `# H7 F# o. Z+ P7 D3 [. w5 x9 N( m) T* k
'// 释放文件系统对象 6 }# P! O6 j0 E5 g, t. e' Y7 m/ }8 @
set htmlwrite=Nothing . i/ |8 P6 d4 }; g3 u( d
set fso=Nothing
/ a' {. r1 S1 F7 e1 f# u ^%> ) m- S! l% W! x& f+ P4 E
, T4 \% H/ @4 J* ^ V& v3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
" W7 J6 g: C3 a' P( d2 g5 j( i4 l<%
* r, e( h+ U. |# T
7 Y! o" I1 \ L" [" M- y$ E, G2 I'常用函数
1 t- v( `( z4 F8 m* Q |% Z0 a'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 7 R5 ~% T( ~% g$ S5 n# Y
function getHTTPPage(url) 9 k" A3 E/ j3 s: N& ?) Z
dim Http
% l# |% L6 p) C; O( wset Http=server.createobject("MSXML2.XMLHTTP")
4 K" R1 i% a. s! `9 v/ t S' \, }Http.open "GET",url,false
6 q" T7 v; @5 X2 T7 r1 J9 yHttp.send() ) c! A- } k. N2 p9 m8 }
if Http.readystate<>4 then : e( p4 A( E! w; {) u3 J
exit function * V/ P. W& y, K/ K% c5 Y4 E, Z
end if 5 O& ?* t: [( _/ V* Y9 H9 D
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") ! p. i9 X6 {% O, C3 u A
set http=nothing
v ?& Q7 `: K& Cif err.number<>0 then err.Clear - {* u' ?( f- L1 d
end function & @$ Q3 D4 U1 m( L
7 a8 h$ ~* R! V# z'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
$ U1 _' }. k. ~Function BytesToBstr(body,Cset)
$ G: B$ L- V3 ~* P1 L2 Hdim objstream 0 _( P- N: Y" M$ o3 i( l
set objstream = Server.CreateObject("adodb.stream") ! u" j: \8 x% Z6 `5 u' \ q
objstream.Type = 1 / r& n$ X: B- F( E9 M0 [+ n
objstream.Mode =3 9 J" N& s) V/ n$ m7 x
objstream.Open
% W7 l( C/ ]3 D! aobjstream.Write body 6 e* |: S* [1 e/ F8 g; t g' a+ P
objstream.Position = 0 6 ~6 g9 E3 Y+ t2 e' `- O* {, |
objstream.Type = 2 $ K, E% R; d& H6 t$ p" r. }5 i# I
objstream.Charset = Cset
! V. Z* W7 v/ f: N7 nBytesToBstr = objstream.ReadText
/ q; l' h! z# U2 lobjstream.Close ! c3 k. ~6 F+ v. Q/ ]4 k- o& v
set objstream = nothing
9 G/ v* r- E2 E" B/ e- _0 WEnd Function
# ?% L$ o; Z9 r7 N: C- ~
$ x- T1 v6 V: b" e$ P, I" ]' E5 _( S$ S
txtURL=server.MapPath("../index.asp")
+ M7 Y( N0 T- |8 ^3 B+ V: w) X! |
& K7 t$ U `4 t* P) c/ Q8 rsText = getHTTPPage(txtURL) 6 w7 x; I5 G. ?& W+ y
, E( q! ~1 o7 T I" z
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
6 I" c: }# A2 C2 a+ ~6 m8 O: vfilename="../index.htm"
" x) G; b! |/ v& ^7 J( J/ Q; RSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 " {& O6 y$ {2 f4 I
openFile.writeline(sText) 6 e# w) o- M5 g3 S2 n$ r
Set OpenFile=nothing % U: z Y8 G* b4 a; v* K# U# g
8 Q# h2 t) i* Z%>
) c p! M2 b. Z7 V- h4 M<script>
7 o- e& L- B2 R5 w0 [) c9 ralert("静态网页生成完毕"); ; f: q/ f! c/ L2 I* R e
history.back(); + F- J6 A; S; r; I
</script> |
|