|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14359
- 金币
- 2465
- 威望
- 1647
- 贡献
- 1413
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ; ?( f: y" @& h* ?0 }* I; H
像www.aspid.cn的主站就采用了TSYS生成html文件! # d( ^& c& u+ W3 u. x0 r
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. ' h5 ~6 J' C) S; `
4 j# n8 k" ^+ j2 A2 h1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% / }. ~ N3 w; d1 }
filename="test.htm"
) s! }# p& T+ rif request("body")<>"" then
3 T0 ]( w) X7 A+ w. F( Y9 Iset fso = Server.CreateObject("Scripting.FileSystemObject")
- A7 g1 x9 o2 r2 W. g* j |* A+ Nset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
5 p) S" Y( W5 z& l* u, `8 phtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
: J1 I3 m \8 I, R4 Dhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
9 B! C! f$ F4 q( b* P4 ` c# khtmlwrite.close ( D8 N6 M Z$ |; h
set fout=nothing
1 t( |& |, y+ X1 u5 rset fso=nothing
* H6 j4 [5 |) n+ Q; _2 p" B9 ?1 @end if
2 {8 i! l8 |. d) {; Y%>
! x! }# m* d$ Q! M3 N<form name="form" method="post" action=""> 8 u$ {8 j, W" P9 `# ^' r* u; t
<input name="title" value="Title" size=26>
3 U( t+ m T _! G& T c<br>
7 a, ~- X0 R4 v9 _8 [, q<textarea name="body">Body</textarea> 9 n# s. Z3 b/ _( x0 S
<br> 5 ?+ B; ^2 F# D, R& Y4 p2 c, f# B U
<br> 9 U, N! Q% [2 a5 F% n4 V
<input type="submit" name="Submit" value="生成html"> : _" ~# [# Z; a
</form>
8 e: c8 P" i6 b' v$ X! f; H2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
! b4 h; v; S6 r5 j Jtemplate.htm ' //模板文件 <html>
7 {- I% C2 U9 [8 t1 {: N' p1 K<head> ; ]/ }8 C& Y% d5 ?1 H
<title>$title$ by aspid.cn</title>
. u6 Q- V2 U' x5 U</head>
4 j2 x: o# Z5 p- {7 ]<body>
/ I4 C; Y# Z% m# L$body$ * l- }$ h- K3 o! g7 x4 t# a/ t
</body> 6 ?. w( I% S- _- {
</html> ?
; l, O; @# p0 }. ]7 M4 z5 K7 h
% T6 M$ J$ i9 h( ]( FTestTemplate.asp '// 生成Html <% 0 p! ?" I; }( `! d0 K
Dim fso,htmlwrite
; l1 N9 j8 C4 R7 Q8 ^" _5 e9 UDim strTitle,strContent,strOut
5 |2 o# y8 G6 a# T' \( J1 t'// 创建文件系统对象
" j1 }+ h! D3 Q. J# gSet fso=Server.CreateObject("Scripting.FileSystemObject")
1 l; }+ Z- c& D! L9 S; b'// 打开网页模板文件,读取模板内容
2 | c* i, N! _Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) + W4 |' D; A$ r
strOut=f.ReadAll 9 [- r" z n, K
htmlwrite.close
' B8 a4 X! ^6 T0 ]* ~3 \
6 |; `4 m# o2 @- ?strTitle="生成的网页标题"
# Y5 ~) {% ~/ v$ W, gstrC ; [2 H, V( U/ u1 p8 x9 ?# Y
$ r. N; B6 d$ S$ ~; h1 q'// 用真实内容替换模板中的标记 ! ]) A1 y$ H& l, N' v& X4 L' x9 X3 n4 t
strOut=Replace(strOut,"$title$",strTitle) 3 V1 S: i3 z( G, H3 J8 r
strOut=Replace(strOut,"$body$",strContent)
+ c$ `+ w) e# }( i4 g3 D' B
( c- @, E, ^7 N/ U. q'// 创建要生成的静态页 : v) L, k& t% ]' j5 c+ A
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
! }4 R7 Y! T( p& G6 R5 q+ Z3 b
6 c/ R1 I7 X" ^" `- L'// 写入网页内容 ' _- S# \) F/ z% w" H; p
htmlwrite.WriteLine strOut # h' J+ [+ q$ d; P' y; [
htmlwrite.close
8 L5 C" F6 }8 n! E& g9 w) }! c( ~0 o0 v; h0 v
Response.Write "生成静态页成功!" 4 `" A7 X- z. Y8 } T, x$ i
8 R# u: r i/ l0 N'// 释放文件系统对象 : f' ^, s$ U. z3 n3 a+ @+ A
set htmlwrite=Nothing ; D. u* ~+ j4 B" i$ }8 @
set fso=Nothing ) F, _$ J B7 C6 P! u* [) a V' u p
%> # F B% {: v, l! N0 r4 z
9 g, E$ w- a! h; y8 }1 l/ @# `
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
) h5 ]6 P! P/ ~<% 7 F' o# l3 O! ?! d. ^- x
) Q+ d% ?: Z$ h( f
'常用函数
/ [7 K, b! A# h4 |# F+ l) R1 V'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 2 g! w- g. ^7 ^( w* B: E' {' P
function getHTTPPage(url) $ o/ C$ C( ?* S. _
dim Http
8 Y# }* E5 l. ?7 Y1 Jset Http=server.createobject("MSXML2.XMLHTTP") 2 \& p0 V% r6 K0 f) m
Http.open "GET",url,false 6 j( h2 N& F6 p1 o
Http.send() % q2 f O4 s2 U1 b6 V
if Http.readystate<>4 then $ a x/ V- e4 C/ g, ~9 o2 s( |9 I+ I
exit function
8 y4 J e! l' M, q, S- eend if . Q c9 m6 W/ Y A% q
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 1 Y6 l0 f6 T" s
set http=nothing
! ^: y! V/ o3 d6 R" F( Z0 Wif err.number<>0 then err.Clear ; @. K$ y$ u1 s5 t8 E! s' r
end function
7 z- \8 i+ q; g J5 r+ V, m' M0 m0 A" b' Q
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
- ]2 e" w5 ?, l- F. q$ pFunction BytesToBstr(body,Cset) ! F( k, [/ C1 m- n' [
dim objstream
( p0 l B* F6 X$ w. j) X3 O0 i5 _; [2 rset objstream = Server.CreateObject("adodb.stream") 8 w; h/ O7 h7 I
objstream.Type = 1 7 E0 G* y3 N! N' f0 k8 K& L
objstream.Mode =3 , ~" o8 i" O/ P v) G) N% o
objstream.Open ! T' r/ X1 E- z8 D3 V/ Q
objstream.Write body ' z3 \; q! u7 b% t# s
objstream.Position = 0
2 s: ]2 Z, I' p& ]4 Eobjstream.Type = 2 0 \( l5 B+ y% H" m3 C8 w5 o
objstream.Charset = Cset
5 S% R7 }$ V0 I; u+ U. ~/ |BytesToBstr = objstream.ReadText
9 H5 ?$ l, u) ^objstream.Close
7 L* {9 J& G, j% C% }( Mset objstream = nothing
5 X+ o4 t* x9 t9 v, j+ [) [0 eEnd Function
' L* `& g5 `2 E# A) ^/ }+ H) } h! X! H8 [' t, }2 w
( n! X' L- A% F3 U
txtURL=server.MapPath("../index.asp")
& V! F. ]1 e& y8 @' W
7 p' K- M; Q% R' |sText = getHTTPPage(txtURL) ; |$ N% A0 I9 I" Z$ l
1 R* i+ h2 l8 lSet FileObject=Server.CreateObject("Scripting.FileSystemObject") 0 m: I+ D {: ^% C
filename="../index.htm"
, M1 I: R& e4 T# N1 s" k' xSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 % n$ t$ t% c( U" x' c( j
openFile.writeline(sText) 5 j% ]! P7 q; H5 s9 L! ?+ L
Set OpenFile=nothing $ d3 S: u9 b# g. R+ _5 B: n
0 A) T5 O+ [( w%>
# X& t. z3 Y: O: Z i& |<script> q$ \" l& @3 f, @0 O
alert("静态网页生成完毕"); ( a! _ m H7 w: {" P( }
history.back();
0 | h+ |3 t/ H" O! Z& C</script> |
|