|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14443
- 金币
- 2507
- 威望
- 1647
- 贡献
- 1455
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ' c6 H3 |8 m4 u5 c* x, a
像www.aspid.cn的主站就采用了TSYS生成html文件! 9 N% C0 A# l4 _! e% j
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 0 p3 q- a: J3 w* ~4 _' P
: H% T- t6 o! N2 \+ V. Y+ i
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
+ O# H) j% }! f+ i O2 c N3 y. |. qfilename="test.htm"
- c8 r3 C, u$ g1 I1 x1 Zif request("body")<>"" then
2 q+ e* O. i- |- P {0 tset fso = Server.CreateObject("Scripting.FileSystemObject") 1 _5 I `/ u7 g+ N v
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
/ A* ]! M, i3 x: O4 U% z0 h4 [htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ) l; [, h; F% u* X; i {
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" $ T1 u( D$ y/ }" ~! e& G
htmlwrite.close
% x" |5 g, Q: Y: O) _* z: Gset fout=nothing
% e8 ^& W0 E0 F; W; t4 v6 ~! V4 @set fso=nothing
/ w; x5 a( C5 D: ]7 R ]end if 3 U" d& t, f6 U7 O* ^# N
%>
* j% C' i% y! i8 i) C+ }: Z/ P& Q3 y<form name="form" method="post" action="">
0 R% J4 J( C+ V<input name="title" value="Title" size=26> * \! v2 [& p1 D X: Z" n
<br> ! u- z- f% Z. [0 E. C4 `9 B4 |
<textarea name="body">Body</textarea>
( K6 K! V9 i; X: H& j; y<br>
, l" e+ m( D! F% G' V<br> * M% \ [, e8 [0 x, I" ~' R e
<input type="submit" name="Submit" value="生成html"> 1 W2 n, z* N* v) Y6 P
</form>
[, h5 C; e1 k4 a9 X" S! S2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
" H, P" K, S" v, Ytemplate.htm ' //模板文件 <html> - k& s+ u9 D2 r8 m9 x
<head> 7 V; h3 j! e9 _5 t
<title>$title$ by aspid.cn</title>
. s. J! n* \1 ?+ o w: H</head>
5 [; D& c4 h. L<body> , M( P, W% U! ~7 D: Y9 h1 }
$body$
. Y5 [5 n' s$ R9 b4 z% H</body>
6 d( L7 N0 g. R$ i! e</html> ?
?4 _9 J* o/ O1 t, [1 o6 I: r5 \: d" c4 z# M8 \! l0 U' r+ B
TestTemplate.asp '// 生成Html <% 6 q# ?6 g* G/ t8 Z
Dim fso,htmlwrite
- u. ^$ E3 m2 }! l" z! y: dDim strTitle,strContent,strOut ; N2 G6 q' p8 B3 E; @1 `7 ?) z, E( y
'// 创建文件系统对象
4 r0 A6 ^( K A6 A# M- p' gSet fso=Server.CreateObject("Scripting.FileSystemObject") ' O+ T) X0 W( m1 v/ t
'// 打开网页模板文件,读取模板内容
. Z4 Y+ d w. OSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 8 O* ]! p% ^ ?/ d4 l. X: L2 v
strOut=f.ReadAll
# ~, _$ R( ^+ uhtmlwrite.close & I# L. o% E. V( k/ T- Q' U% Z
+ E. Q8 n, K4 g
strTitle="生成的网页标题" 2 H' v* G5 n8 }+ ]
strC
, o* i5 Z7 f) V5 G) q; F4 u
8 z6 Y1 K; R: i' g6 X d( J'// 用真实内容替换模板中的标记
7 ^- M& l! y7 l, \0 XstrOut=Replace(strOut,"$title$",strTitle)
" k+ ~' V0 q9 J5 {) I( tstrOut=Replace(strOut,"$body$",strContent) : m- K; U9 r/ u( ]/ ^; c& f
4 b4 Y2 w& r6 n! S. H9 V7 i+ J
'// 创建要生成的静态页 9 s& P; }; P! _" j8 j8 T& R
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
" R# C0 B, E% C4 }) u5 I; l, l5 I: y" l8 |: L. d- P
'// 写入网页内容 6 q3 w. f0 N) s. \
htmlwrite.WriteLine strOut
. D& T+ |3 L/ Thtmlwrite.close
) v t& F: N) | R5 V: [7 t, Y% @5 ~) h0 }9 U
Response.Write "生成静态页成功!" ! Y& t- l" ?& L7 g; T% _) M( p
: K; N) Y) _3 z, O1 @'// 释放文件系统对象 6 ^' g0 k+ [% s g, T
set htmlwrite=Nothing
# q' q+ r# G+ Fset fso=Nothing
8 g0 j0 |: g3 n( ~%>
3 T8 k: h* W% i
# v+ R2 C; O* _2 D9 x3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ' L3 v, C( I4 C1 I, W- d
<%
5 H# r, I" r3 `- {* P$ X) `( P) Y. C
' L% ? [% B+ W9 s9 m. @% A% X'常用函数
, o! |' e- i" u4 j0 @9 G! D, K'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 & x* h2 _: A- L5 [* ~1 }# V
function getHTTPPage(url)
L! Y3 M$ U' W9 S) n, }dim Http ( Z& b& |0 D3 Z' H7 Q
set Http=server.createobject("MSXML2.XMLHTTP") 4 T4 `; g. W) g, Q
Http.open "GET",url,false
7 f6 c5 ^ M2 ]; cHttp.send() " i& q( \# U/ G7 h$ C6 E% q; d
if Http.readystate<>4 then
% D, h: _3 r" D" _0 ?exit function
1 a9 H* `! _' t& d( @: t. k8 uend if ( I" o z4 |" Y8 ]- U7 T
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") + e7 Q$ i& ~0 g+ z0 C* _# p
set http=nothing . e, N- n4 s' |7 A
if err.number<>0 then err.Clear
; ^) X& W6 D3 A, uend function
6 s+ Z* v/ I, X* f ]) Z) b7 m0 u. a: F
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
3 K' S; H1 _' x' bFunction BytesToBstr(body,Cset) 6 v0 G. U2 u( @, l- s* z& L! c
dim objstream
A0 f: T" Q# r: p5 n' jset objstream = Server.CreateObject("adodb.stream")
7 V4 B( L# x8 c% A! yobjstream.Type = 1
7 m" L$ q# T8 m. c# b6 q' Kobjstream.Mode =3
* Z- q& p( {4 q8 \- eobjstream.Open
5 {( s$ w N3 V3 ]objstream.Write body + J/ v) [! m3 D1 [! D
objstream.Position = 0 : x) Y8 `# z7 H3 z
objstream.Type = 2
! R! k5 ~: I5 u9 L0 Xobjstream.Charset = Cset
0 v: T& T: ~- w2 j: {) S6 jBytesToBstr = objstream.ReadText f1 Y) T% W; _
objstream.Close % f# o! f* {" B
set objstream = nothing 4 S. }* p( _" L0 [% B% l
End Function 2 v5 _9 m2 a7 B7 E! o
: U l, f7 v# T4 N
' |/ i8 H, v+ J5 p6 T" ^0 [
txtURL=server.MapPath("../index.asp") 6 f) b) \: o. W ~5 @5 ]
; @% S) i% g$ r& P! DsText = getHTTPPage(txtURL)
* w+ J/ ?: L; K% _! M% H7 J- Z: J- M: Z
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
# }! n; U0 [' e- m4 R5 nfilename="../index.htm"
6 f: _2 P0 `; h1 bSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
& @9 E" T& J% P2 s3 L* S0 zopenFile.writeline(sText)
, e# M" R& ?$ X4 w9 `Set OpenFile=nothing
& ^& K" Y5 _* H i- x8 D" K* O/ Y* ~# ^' h9 J' K
%> ) c S* W$ V, H% Y4 W$ Z& C6 y( Y( e
<script>
. n: K& q. \3 c7 O; Ralert("静态网页生成完毕"); ; Y4 l6 N8 Z& K, ?1 R# R
history.back(); 3 t: z. B5 ?9 |1 Q, x1 v. ^- y3 P
</script> |
|