|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14367
- 金币
- 2469
- 威望
- 1647
- 贡献
- 1417
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
' X$ O" \. c: O7 E像www.aspid.cn的主站就采用了TSYS生成html文件! ( M; v4 i$ s; v2 H, A2 ~) k# c2 d
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
7 R* L& b. o1 A1 ]9 M2 b6 n$ |! s% a3 l! a* J0 I6 p* e
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% $ d3 M# o2 o4 h
filename="test.htm" $ b1 W6 A# H! Q% d# v
if request("body")<>"" then
. e) S2 V2 K, H! v6 [- E! H$ Vset fso = Server.CreateObject("Scripting.FileSystemObject")
$ u' Q' w' d9 Aset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ( p, @ y6 M5 n
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" q' F( Z3 E, M
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
4 m0 G( C$ q I* B# A7 Ohtmlwrite.close ( T3 ^, H" f# O) w5 A
set fout=nothing
$ [8 G4 ], e! B: H# n7 O0 S7 f0 |set fso=nothing : X1 l& {; S) n! e% ?
end if 5 ^" B4 x* o3 ^- {0 X% L# m0 K
%>
. ^: ]6 P& a( v6 ?! S1 m* ^1 b<form name="form" method="post" action=""> 5 s* G" M% \& D m# U y( ~. s
<input name="title" value="Title" size=26> / @8 k$ X+ t; J2 N$ T& u3 h5 W# p$ ?
<br>
2 d7 _7 r) R# E7 u9 x- f<textarea name="body">Body</textarea> ! P; r; ?$ z- ^+ ^6 f) G
<br>
. O; i2 c: p. L; D. J( I2 E<br> + \9 B3 o+ c4 b
<input type="submit" name="Submit" value="生成html">
8 m0 U3 o a; q7 I1 M7 p</form>
/ }( \3 V3 z3 G2 k% {2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
& @. F& R0 G1 |' wtemplate.htm ' //模板文件 <html>
! T2 u( E- J, }/ `2 l<head> & R% p1 H Q5 ^. T" n
<title>$title$ by aspid.cn</title>
$ y, G9 R8 U8 i</head>
# c) e0 s' @0 `7 m' N" i! A<body>
4 A) J9 k# e& P9 s! l8 f; l$body$
2 m+ I, |2 ?! G9 Q; @</body>
) d$ e' N( {6 Q+ t# y</html> ?
% O/ W# {& j$ h( W
3 ~$ n4 t `4 r# ]TestTemplate.asp '// 生成Html <%
G. w/ o, M' QDim fso,htmlwrite
/ T% \; s4 W$ [2 A$ HDim strTitle,strContent,strOut
2 @6 j P& ]! r: x4 _+ r'// 创建文件系统对象 0 W( O/ B7 b- u$ {! r, ^( ^
Set fso=Server.CreateObject("Scripting.FileSystemObject") 6 z8 F/ B/ W3 w$ C' K
'// 打开网页模板文件,读取模板内容 & m: N5 A$ p; l; q) Y* J, n
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) % L8 p) E1 |1 }
strOut=f.ReadAll # N7 J* \5 p: G: n, L9 u
htmlwrite.close
! i$ ?3 \) Z L
7 Z% u0 ^3 s# B% z( k3 W. ?6 _3 ystrTitle="生成的网页标题"
) ~) U" k! h8 W! _- d, Q8 \strC + N5 O- `) T5 y A# s3 z3 y3 n
: y! J& m7 K. x- w; ~'// 用真实内容替换模板中的标记
( e) v2 I8 t1 { E, h( ~strOut=Replace(strOut,"$title$",strTitle) 2 b$ e+ F4 ]' r2 m9 }4 ^
strOut=Replace(strOut,"$body$",strContent) + K' O; Z \- T0 E3 I
; q: f/ A. [3 N& _7 p' W( C'// 创建要生成的静态页
: Y& {. Q# E3 ], b$ E* SSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 8 T5 P$ a0 v$ p$ k, E& n/ d t! N
% u( \$ b, B J8 _4 {! V
'// 写入网页内容 # [" X& u5 D3 G. D( x8 x. r8 l! }
htmlwrite.WriteLine strOut 1 [) w# A/ A% V) _7 Z1 M
htmlwrite.close
: y' A+ f8 B( q, M& v9 A* ^( g4 @
Response.Write "生成静态页成功!"
" \( {+ M, i+ X: p
( |0 f9 I8 o; o2 M0 \'// 释放文件系统对象 8 D8 f: V4 h/ `6 @1 t* F
set htmlwrite=Nothing
+ S7 U/ X8 S. I1 e) Y! T" S- M7 Y6 k! Fset fso=Nothing 0 H- C, H7 P* F' D1 c" D
%>
+ t' v& Y4 h5 {* Z( [; |
% K* E" ?) C0 M; K3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 1 O/ [( d9 U/ S& D$ u
<% 9 E# g1 ^* ?" ]* \3 t7 O5 P, I5 D
8 j. X2 |) u* o
'常用函数
; q" p, \2 f7 c( D" f, h'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ( D: n2 y7 [9 {
function getHTTPPage(url) : Z1 ~* [; ^* B0 L1 b8 M
dim Http
( |+ T! L. |2 sset Http=server.createobject("MSXML2.XMLHTTP") 1 h% o4 ~9 q( h$ z0 m
Http.open "GET",url,false " \1 P _, R i2 x
Http.send()
7 u' d4 b1 L3 t$ M6 e t3 Uif Http.readystate<>4 then
9 b7 x& J* |+ w' ~- t3 Bexit function # H% t' ?" c) Q+ F* A
end if
, P R' X% V% I/ a- m5 E0 K' CgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
3 P6 x1 C/ p1 q0 c7 K" Q2 W( c8 i5 Rset http=nothing
/ E5 n6 ]$ J6 r: \8 }% e7 eif err.number<>0 then err.Clear ; `( G) ?6 `7 h3 D c
end function
0 y7 D: o0 N1 _. M1 a" j
! J7 _# R: V' R3 Q J( f'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 + G% F9 {# t4 a2 o2 k: x6 K2 p+ p. ^
Function BytesToBstr(body,Cset) , D# Y4 y5 k) j0 y9 E0 b# ?/ I
dim objstream 4 x6 Z0 Z6 O$ a& z1 X# j2 Q
set objstream = Server.CreateObject("adodb.stream")
# X, s5 o8 c B0 Y0 ~* V+ Aobjstream.Type = 1 ) R9 `$ F/ s; Q9 ~# d# v! K
objstream.Mode =3 & U5 q# Z8 a1 \; p d: J. Z8 B
objstream.Open $ K) d) ^* ]5 z' {# v' h
objstream.Write body 7 a" g3 n T! l( E7 c. I5 h# y
objstream.Position = 0 ; k) T% D' ], Z8 x; i+ N
objstream.Type = 2 1 b9 C5 U) p- l0 s: R7 L0 o
objstream.Charset = Cset
* _7 w8 g0 {$ X4 K" |/ t/ l+ lBytesToBstr = objstream.ReadText
9 b' P, ?$ Z% u% p) Aobjstream.Close 6 K* k$ F' r0 F( e) `" ?( b7 G. Q
set objstream = nothing ) O; f7 K8 n8 d% W3 q& \- ]( a
End Function
1 i/ W+ f$ C1 {$ h2 n$ S
% R, P9 k7 D8 h1 Y; O! ~
/ l; ^. \- a7 J% n* r2 j1 f6 }txtURL=server.MapPath("../index.asp") % @# m# w$ u5 t2 a6 a- \% [4 x
" ~6 q, l) ^) u/ G; Y7 J
sText = getHTTPPage(txtURL) 2 I1 f3 D5 }) t: h9 d
0 B0 A0 s: ^2 n- s* _9 F
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
2 {( W6 Q ^' C% C4 j4 u$ h8 mfilename="../index.htm" # k% x& b/ t5 h8 U: R, P
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
" O4 r0 V! e4 u* _% z, f) iopenFile.writeline(sText) 9 O: }; o4 M7 @4 w4 ^/ k
Set OpenFile=nothing 5 C5 Z9 i! @& v! }$ z: y
- V6 n# f( b% y9 J' a9 W
%> : ] d, J1 K# L6 R u# f
<script>
5 _2 m3 X+ ` ralert("静态网页生成完毕"); + ^/ b9 n# t- f. }
history.back();
/ r' _- a" l. H4 c0 M4 d- @3 u( ~</script> |
|