|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14331
- 金币
- 2451
- 威望
- 1647
- 贡献
- 1399
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ! Y' {0 g, w! q% I
像www.aspid.cn的主站就采用了TSYS生成html文件!
; l# Y( B+ f; z+ Z( s4 Z0 u( O所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
) v- b* ~, [0 L. X" w g ^3 ~* F T: ` v
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
8 F5 I) `% D" ~- r7 u" ]4 Xfilename="test.htm"
% @0 `, [8 E. k$ }# V! f" Eif request("body")<>"" then
L! I& @9 j/ P5 Xset fso = Server.CreateObject("Scripting.FileSystemObject")
; n/ {$ l& C, s0 iset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
# X' [' c( X1 P5 O7 x. L: J$ }1 I# O0 [+ ghtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 5 P X. T9 `8 G0 q
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ! c: J: m# `9 X# R& I( q& x% a
htmlwrite.close
# _7 t, ?% t/ E/ Mset fout=nothing ) O1 H! h- e8 a$ `$ ~
set fso=nothing
1 ], B! z6 x. x. Q) @2 L" _" T+ H$ Fend if
' O. |7 @! V# d( a" o%>
# j: @0 P3 e5 ?<form name="form" method="post" action="">
6 L7 n8 K0 K, @! m<input name="title" value="Title" size=26>
, v( L& K. a2 F4 H! Z<br>
% U) [0 t$ X+ c3 w/ b<textarea name="body">Body</textarea>
# u* K0 C# L3 h+ [; X& ?<br> + N7 U' E6 E% E1 c
<br> 0 d, z* G! M P
<input type="submit" name="Submit" value="生成html">
+ x3 G& V/ m8 {$ R: d# d8 r</form>
7 M- o! S3 R% [7 E" P/ Z+ H2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. : _3 ~: C0 |5 J1 K) u
template.htm ' //模板文件 <html> ; H7 a: C7 D5 L+ I. b
<head>
) g9 m& e( K( n* I<title>$title$ by aspid.cn</title> 2 S5 h7 Z' F" M
</head> 5 _0 ^" R9 \$ t/ \1 d0 X0 J5 I8 e
<body>
3 u4 |& v" x* ^$body$ 4 \- Z. F+ S6 u! G1 i# P
</body> 7 C) `1 @& Z6 @4 s5 L
</html> ? $ s+ ?5 R# l$ r" [! l" t
+ ~0 d( w' R/ T% O) w! }; `TestTemplate.asp '// 生成Html <% . t8 C$ F7 y0 W3 I0 Q. s8 Q$ I
Dim fso,htmlwrite
2 }" Z( O; M: w" c7 i5 FDim strTitle,strContent,strOut 7 f7 W2 W) [; r' L: F* L
'// 创建文件系统对象 ) F. d7 U+ Y# S) a+ v
Set fso=Server.CreateObject("Scripting.FileSystemObject")
6 k7 b4 ~1 j. L$ Z'// 打开网页模板文件,读取模板内容 ( u" A2 W3 @* F3 ^9 Q( Y+ `
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) / l+ G! I" e; y, s( J
strOut=f.ReadAll
0 o" e$ X& ~% hhtmlwrite.close
, ~: L' r9 R6 D% q5 `( L2 W1 F! b4 `3 v8 g& j' F
strTitle="生成的网页标题"
( r7 R6 H4 l" M5 M4 DstrC
+ m! l" q% p& T. {/ t4 ~; ^/ G0 [- i
'// 用真实内容替换模板中的标记
; [; ^% {! v* S3 F- p( UstrOut=Replace(strOut,"$title$",strTitle)
8 ]) U4 j, [% z1 H U4 XstrOut=Replace(strOut,"$body$",strContent)
7 g6 e9 p: {3 h, D( f# _$ |( m j, a) U6 t2 ` T
'// 创建要生成的静态页
$ D' g, V7 U' [9 ^2 `: bSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) * i9 p G9 j2 H6 @6 r, ?
% T7 c6 X+ ?( `$ G M5 }' G8 _
'// 写入网页内容 3 W& o. F& z7 I) k, M* d+ W
htmlwrite.WriteLine strOut * H0 L4 Q3 |2 T/ [* j; O( r
htmlwrite.close
( s' Q4 H) c4 Z
2 a1 K9 y" t* h4 {, M0 bResponse.Write "生成静态页成功!"
2 L. V" Z" r; c; f) k& Q! p
) v4 S3 ~7 H# @7 t: `) `! ?8 f'// 释放文件系统对象 1 K( f5 g1 i' V, ]
set htmlwrite=Nothing
% Q6 P# c- I' n- Pset fso=Nothing [; s* k' W/ g
%> h$ o! q$ ^2 R3 b: E2 U& S% e
3 T* l0 n$ }5 B" Z( |/ B3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
: L. E- ^* l2 i" h+ t5 o<%
' U0 Q) ] y A+ x3 Q I. ^2 J& P/ A _5 e2 U+ i% ?
'常用函数
4 z5 A) {( _5 y; z5 o# L'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
; p0 T0 \1 d6 F, C% `' ^function getHTTPPage(url)
, t7 S. r# |! E2 p& d4 wdim Http # \, S% p9 i( ^# X8 t2 d
set Http=server.createobject("MSXML2.XMLHTTP")
. n0 L4 c# X! V( XHttp.open "GET",url,false ! j% D- ~. }. p; `! K1 s7 l
Http.send() / P0 N. H1 W% s
if Http.readystate<>4 then
0 e8 h+ d, a2 [6 t! H T, T) sexit function
0 ]- C* f5 C9 A6 |end if - V- A+ c9 {) K
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
0 p! J. Y- V0 o- X, ~0 `! \set http=nothing
' @0 m/ v1 u& h B3 Gif err.number<>0 then err.Clear 6 X5 X* Y: r7 Q& ?
end function 9 X' Q! n/ r2 W1 v# u* n* [' E
5 [8 w) s' f5 W'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
; D' \% g" d& h& E- BFunction BytesToBstr(body,Cset)
/ s9 I) z; G& n+ E" M7 xdim objstream
g2 h T7 X4 T4 V* y$ hset objstream = Server.CreateObject("adodb.stream")
; Z6 P" R U! a: N3 D. `objstream.Type = 1 7 g" b; R/ n P& v
objstream.Mode =3
7 W4 |- N! V1 O4 y# d L" [4 \objstream.Open
% s2 l0 I8 C9 @9 W3 bobjstream.Write body
- S$ ?6 [* O$ Mobjstream.Position = 0
0 S) S, B6 T0 c* W; Nobjstream.Type = 2 " G3 p2 @4 c& f- l: s3 Y
objstream.Charset = Cset 5 C6 n5 f- v j1 g9 p0 t
BytesToBstr = objstream.ReadText
# _, A. M0 H( j5 tobjstream.Close
3 C) E# y: e6 A! O' F. B. Kset objstream = nothing
5 j# ^- z% a1 CEnd Function
, R" T5 ~4 m' i1 l& \/ h D: x" L, @1 L; j. z, b5 u `
! `9 g, D+ J" N$ K8 U& `txtURL=server.MapPath("../index.asp")
4 y9 q# ?$ E, L1 E. E( { B
' N; p( }7 B: z7 ^1 GsText = getHTTPPage(txtURL)
. a) U J( X* C: ~* S. I( e% M( n+ X3 w& I2 R4 X6 {+ U' O
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 5 M* [& D. A2 r% n$ Z0 q6 H9 X1 i
filename="../index.htm" - a0 K1 t) y) k% h+ w* I" F2 ^8 z
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ! N6 V% u7 `( R' T
openFile.writeline(sText)
! ^7 ~4 X5 T' Z. LSet OpenFile=nothing
* f3 A Y+ Z' l, }- v" j" p* h5 `. C. X, k2 O9 B+ b
%> : P. L# L4 [6 V6 ^3 ^2 `
<script> ' V; }3 e& U& q. ~# \1 A
alert("静态网页生成完毕");
* u' S0 \. [+ k! C# Fhistory.back(); # P! V7 \/ t" |# K. j$ ]) A0 D
</script> |
|