|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14435
- 金币
- 2503
- 威望
- 1647
- 贡献
- 1451
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. $ `; s5 Q0 U% ]# q* h" r m
像www.aspid.cn的主站就采用了TSYS生成html文件! # z: b" Q7 h* a& h! C6 l% [: l
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
9 m3 b/ I, B3 _! @0 C+ h& o* B5 B: }% b6 Q5 a, B0 |9 C `
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
5 P2 _& x8 D$ m* k7 Pfilename="test.htm"
9 F0 N2 k% ?% d' V+ |/ G1 Jif request("body")<>"" then ; V3 C' i3 d$ ]
set fso = Server.CreateObject("Scripting.FileSystemObject")
9 {- z p# e& L: e$ R! bset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
2 E, M! R0 R, ]htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" $ F1 y# G M& |7 A
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
0 W" v4 Q; X3 |" M" i/ O6 Qhtmlwrite.close
2 q. e$ U3 q" S# v6 aset fout=nothing 4 D5 N( g; x T; e4 q9 B
set fso=nothing
/ L( w& X' N0 g# v1 vend if # A9 Q1 _3 P3 n
%> 1 Z( Y0 R$ _+ i% _) b) M
<form name="form" method="post" action="">
/ Z/ H+ F m5 T z<input name="title" value="Title" size=26>
7 Q+ h: g7 a" _+ U1 g3 _/ N( v<br>
, h) _) `: f7 Y6 ?+ ]# h. F$ E<textarea name="body">Body</textarea> ' W* b/ M) u6 Y0 N$ q5 {5 ^3 A8 X
<br> ' t/ n9 p" j& J& `
<br> ) m: H: d' z% G6 ~: W# X7 ?2 f
<input type="submit" name="Submit" value="生成html">
+ O, l: b2 b% ?7 Z! N2 v</form>
/ c0 W! u* e. H. M3 R2 v2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. + D R/ @$ z2 z+ H/ S- A
template.htm ' //模板文件 <html> ' Q! @1 _2 c. t) p. Y
<head> # ~* A* x% B# W1 y4 s5 _ W3 S
<title>$title$ by aspid.cn</title>
% t K. l4 d# H+ M. p5 ?* d! W</head>
+ p( X: A/ J% i; v9 v<body> 7 E/ `. Y% R; k1 ^4 x& f& Q
$body$ ' A1 _5 C6 @/ ?' J
</body>
6 v. w K. E% [- r+ s$ `* P</html> ?
8 _* r- m2 f- x$ |% u% c
5 g1 y! R$ ~8 j5 x1 t& @, x" `, UTestTemplate.asp '// 生成Html <%
0 Q0 f5 j5 S3 W& Q X! ]: E+ MDim fso,htmlwrite
2 X/ x2 u6 n) ?/ J2 j& y3 G/ LDim strTitle,strContent,strOut
, c; R0 O; g/ ]6 L'// 创建文件系统对象 ' i# |- |7 K* ]2 g6 l
Set fso=Server.CreateObject("Scripting.FileSystemObject")
, x; y' {$ | h/ ?3 ['// 打开网页模板文件,读取模板内容
1 j5 I' c. n2 a2 P7 OSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) $ k' Y7 n+ P! c2 {$ B: g
strOut=f.ReadAll 6 r# |+ Y/ B7 i9 ? i
htmlwrite.close $ ~$ @# F# p4 L, L4 i$ G0 m' }
8 _( A7 c. @2 W! g
strTitle="生成的网页标题" 9 Y z0 C/ R: F
strC : v* W: X. `2 o u
; ~/ `4 [, d+ k# \; @
'// 用真实内容替换模板中的标记 - k, Z. o4 q M9 S
strOut=Replace(strOut,"$title$",strTitle) ' F6 z" a7 R/ x4 t
strOut=Replace(strOut,"$body$",strContent)
1 V0 e1 ?, q, W) q. {9 {" t
0 t0 g* Z( P6 V9 w'// 创建要生成的静态页
7 s/ g' F* v* O& \! SSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
: L* ?( ~% _1 Q
% ~+ E6 U2 X/ _, s4 x y `# G1 d'// 写入网页内容 7 W$ v1 t$ b8 c4 M8 F: e
htmlwrite.WriteLine strOut , D" i% \3 F, s( g9 I
htmlwrite.close
2 {: V- d4 y) Q7 R0 G. E$ ?( j! _( m: D" L; T5 s& o+ A0 ~
Response.Write "生成静态页成功!"
" H$ v) o* F8 \, B
- [% g/ ?& i2 j* J& I'// 释放文件系统对象
6 j' j. q" f" `7 w. |set htmlwrite=Nothing
7 \' u% K" y; k/ Q' z( Oset fso=Nothing " S" d' p4 r9 Z" U
%>
# q; V! o" Z, I4 l4 f
* v* F9 t) d$ E) f; E6 O V3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
7 M8 R9 d9 c! \<% 1 I/ w! [& b+ l7 J! @
# |& t/ F% ~5 S& u( y
'常用函数 # r+ W4 a3 @& q% t. `" f1 G J) l
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
- y3 ]1 T7 `5 i1 K" [6 [( Afunction getHTTPPage(url) 7 J+ t) z7 m3 u( g Z- d( y/ W
dim Http ' R; ?/ H+ m/ ]1 j% F& K
set Http=server.createobject("MSXML2.XMLHTTP")
) b5 n- |& Y! G. ^2 U" ]Http.open "GET",url,false
2 l! ~3 |* r5 R' BHttp.send()
3 f$ L0 [. B2 u T9 Eif Http.readystate<>4 then
: g" R0 d8 X* T5 l* \$ j$ Lexit function c; K' @1 _8 `, {2 }% d( t. i
end if
$ D& E8 D4 ~* t, H7 r* W K; f: SgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
8 l" y1 [; S/ g- d5 y c& Nset http=nothing
0 K& [0 S) R5 c9 Vif err.number<>0 then err.Clear - U/ p$ V: t5 }3 }! ]" i; l% J
end function : [2 `0 j! S2 g8 O. ~/ d( [' ^# e' x
7 [2 Y' B8 D% J" J' D2 T: h'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 ! c% Y- i" [* N
Function BytesToBstr(body,Cset)
- y; }. M5 n4 P: S$ K4 x) idim objstream
9 i$ T) N! S: g( S! \set objstream = Server.CreateObject("adodb.stream")
* ^" d7 l% \2 ]objstream.Type = 1
/ C) `, S- q4 x2 Gobjstream.Mode =3
. k2 a8 \5 q ?8 D4 \* _objstream.Open
0 E. }. x. s# Bobjstream.Write body 6 l, b4 g0 C7 l' ~4 J* f
objstream.Position = 0 1 P! U8 R; k! c7 G2 {$ V2 h
objstream.Type = 2 4 ?" h3 U* @& R- F# W
objstream.Charset = Cset 7 ~$ [, e9 e5 N4 [& }
BytesToBstr = objstream.ReadText
7 m) i T; `% Z2 I+ K3 t8 `5 Iobjstream.Close ; s+ t R3 p+ K' v* _
set objstream = nothing
) e+ @6 d6 c# [End Function
! r; d3 G7 h) B% D$ ^; f: p2 ~% w
( V j) J: | y& }0 O) q, \/ l8 }
txtURL=server.MapPath("../index.asp") $ P2 c5 a) N8 \4 K/ B
) M# C% P7 C# g+ {. AsText = getHTTPPage(txtURL) 2 v; @! T4 t$ O& C0 p' a; y
" {. q# a# g$ _& {$ P0 P
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
( k$ y( B2 W0 gfilename="../index.htm"
& P+ i P+ ^& W, bSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 " O7 k& f; @2 ]5 q# Q
openFile.writeline(sText)
& U9 {* @/ Z. I( N/ a5 `- e5 \Set OpenFile=nothing 1 a9 U3 ?/ o4 X
! c9 ?* U9 C9 I; m- d" Z
%> # l4 Q7 N, e" ]" ^' o2 Y
<script>
: p+ _2 s- e' \0 \+ ialert("静态网页生成完毕");
3 ]: P; L4 s8 D2 dhistory.back();
% h1 H7 p6 A$ u</script> |
|