|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14331
- 金币
- 2451
- 威望
- 1647
- 贡献
- 1399
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 3 \ ~ D* O0 t" W9 l c# D
像www.aspid.cn的主站就采用了TSYS生成html文件! j$ g$ D) X2 N+ W! H( ]- x2 P# K
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
0 q9 y+ b, G, x( u# Q$ K2 z6 k: j9 r8 L$ n( H
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 7 q( h! p. i1 z- h
filename="test.htm"
9 e |: p5 j) B \1 Iif request("body")<>"" then . g5 h$ w" i! v }
set fso = Server.CreateObject("Scripting.FileSystemObject") % k' i# R3 ? j, q8 n Z: l
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 2 K/ Q( S1 ?4 V# w7 \
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" " Z' v4 s6 C0 k4 j) v, b: Z5 a
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 3 p2 V( s; i+ E& G }& c
htmlwrite.close
& M' c3 d! [+ C% h4 }( C' C) aset fout=nothing 4 ^5 u# d6 w, i% G o1 }
set fso=nothing % X. A- r) A9 n7 _; g) U
end if & ~- z- a3 e/ b
%>
# g( [0 E: C& X! q8 y& _' E<form name="form" method="post" action=""> 3 Q8 r+ x1 i; c$ T
<input name="title" value="Title" size=26> 5 r6 X' V5 `4 t' G# j. C& z- u
<br> . c7 b, Z% R$ [$ e+ _9 y# s
<textarea name="body">Body</textarea> 1 {2 i, j8 @5 T) G
<br> 8 f: l! A6 W8 s( V5 k/ t
<br>
; G( A2 L9 G9 `5 z; a<input type="submit" name="Submit" value="生成html">
$ Z7 D: ~+ g' l9 s) m3 B6 V5 o! u</form>
! A" y, r: M8 S2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. : A6 e! q: ^- r) o* A: A) r* n
template.htm ' //模板文件 <html>
" ?5 ~; C9 S* `4 @5 e, w8 o<head>
! C, b4 q7 f$ s! _<title>$title$ by aspid.cn</title>
- v d" ~: q3 d& I0 G; T</head>
b4 G! V$ f5 @1 i3 N<body>
+ o: B: s/ T1 U+ ^4 z4 {$body$ ; T- p" }' o0 d) ~' k
</body>
; J O( u7 P' V8 B0 Q</html> ?
0 T. I& M. _0 q. y* p) C0 f0 k: h* |* A' ?# M$ M
TestTemplate.asp '// 生成Html <% : ?2 E0 h3 L' O9 x; x. h6 C
Dim fso,htmlwrite
+ D: U1 a0 Q) V% V4 J7 ODim strTitle,strContent,strOut : P0 L: o- W; e/ T
'// 创建文件系统对象
5 h. [' Z8 u2 ~+ X* q1 O1 FSet fso=Server.CreateObject("Scripting.FileSystemObject")
$ C2 o3 G" p2 a$ Q5 \4 E: F% M'// 打开网页模板文件,读取模板内容
j" Q2 e" \# H+ h* GSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) A( I9 S" w3 }" s( I
strOut=f.ReadAll
/ Q; m7 B$ L. E& T$ xhtmlwrite.close
|! u0 L1 ]4 k) V6 O( }9 Z' [9 u
strTitle="生成的网页标题" # p$ z$ [6 l0 U: E
strC ; N, A/ V5 `6 M/ ~) c8 d
+ S# L9 ]; i z; y/ Z3 C
'// 用真实内容替换模板中的标记
; z( x1 ~+ d+ h! F2 M+ sstrOut=Replace(strOut,"$title$",strTitle) + P: z2 {1 l7 b
strOut=Replace(strOut,"$body$",strContent)
6 b9 E! i5 \2 b) H& v2 g+ {! O4 g( W
" Q, N! U) k. q( q8 ]; U'// 创建要生成的静态页 $ m$ l) B p7 ^9 i& q( A" n
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) / ]+ a% g! p' r- i& e9 X0 e8 r
0 z: g* M# y- ^; \5 @$ n
'// 写入网页内容 + g3 C% G3 E# V* A+ b/ g
htmlwrite.WriteLine strOut + T% ^$ q0 Y- g
htmlwrite.close 1 y9 W* n4 u4 L: \/ E
. V8 g; `. N& i' n& M* G- I/ l6 mResponse.Write "生成静态页成功!" 9 U# c5 }, m2 |+ w9 }: |
& u0 i: \" Y+ l+ I0 M/ q! i
'// 释放文件系统对象 8 O- d4 ^- y1 |9 ]" x! e# _ F
set htmlwrite=Nothing
$ r' e' U4 k Q+ Fset fso=Nothing
' C3 P% R# [8 v, H0 Z+ \% ?%> ! ~3 ?4 N. Q1 k) |! S# ]0 R
' X0 L f! B3 c \
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. * a5 n2 T4 S8 N5 I" V
<%
8 X1 C( O0 X+ S f O; ^. t \4 b& E8 I! W7 ~9 [/ n2 t2 `
'常用函数
1 T9 r8 Z+ Q( c3 c" c$ a'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 " t% {$ M( @7 `8 I
function getHTTPPage(url) : F4 r& y6 i" x: Y8 _+ C$ C
dim Http
5 y0 j) X) w N" E7 d" r! \$ {set Http=server.createobject("MSXML2.XMLHTTP") 4 t7 b: t5 m+ y6 t5 {4 O+ c
Http.open "GET",url,false 1 j$ Q, Q2 a, N7 k# {% ]
Http.send() ! ?% {0 }2 D! F. a+ H) N
if Http.readystate<>4 then / n: A9 P# c. N1 b) R. \; ~8 `
exit function c) R- `6 s4 ` f S
end if S' m) o* R2 Z& t5 I, L
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 1 Y+ x/ c& H5 Q7 e. W
set http=nothing 6 Q2 M( h, \! g, M; V
if err.number<>0 then err.Clear 9 y! `# @1 H, `! I8 i- f5 _
end function 5 J" G# ^+ i4 @1 s$ A
+ d, C1 s: j! {: D
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
0 K4 b: f) y* b: R0 `4 J c' HFunction BytesToBstr(body,Cset) + z9 E+ J' h. g* L9 Q* Z! w
dim objstream
. F3 e( E5 r& J3 z% Z' Wset objstream = Server.CreateObject("adodb.stream") 7 W5 h( ?* m$ L3 K2 h9 j
objstream.Type = 1 . c! A5 U9 t& Y/ r: g ]- }
objstream.Mode =3
& w+ _0 {0 f5 C* t- ~8 ]$ P/ _objstream.Open ! z U$ V y5 \. R9 `
objstream.Write body
2 J& a' J! h, K$ Iobjstream.Position = 0
5 {, W9 ^0 X% uobjstream.Type = 2 1 f* F$ p( w! S+ k- P# Y
objstream.Charset = Cset
$ {9 O6 ^# f# q) Y! l( B7 D- v+ _BytesToBstr = objstream.ReadText : c9 N9 G8 t6 k# Y* D5 N$ f* K
objstream.Close
! q: \$ r/ F, n, K) L7 k! Wset objstream = nothing ! k9 m4 U$ }! c
End Function 1 [2 g/ r' t% r
2 A. W" d- e( s$ ]' B: S% U9 Q7 @
: ^" ^$ u0 T2 [txtURL=server.MapPath("../index.asp") - Z5 Y- U7 l, k
( L% _! v h9 r, F
sText = getHTTPPage(txtURL) ! \' u9 w; P& O) q: h) i
& ~9 z$ K& ^. W. ~/ h
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") $ ]0 g1 A8 a9 D1 p+ T1 d
filename="../index.htm"
* n4 m% J3 K/ fSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
! ^# |+ L8 _ Y6 kopenFile.writeline(sText)
2 t2 U5 M' G" p; @4 O7 _* ~! xSet OpenFile=nothing ) D! [, G1 {& P+ N1 d' X
. J/ m% R0 j, J6 M6 @* F% [8 Y%> - z9 B( M' ]4 U0 `3 f0 g
<script> 7 O- o: H+ n/ K
alert("静态网页生成完毕");
5 H: I( |0 f2 p9 b7 u0 rhistory.back(); # x @$ L' Y0 a1 f+ |5 T% F
</script> |
|