  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14221
- 金币
- 2401
- 威望
- 1647
- 贡献
- 1349
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. $ |& V4 g& M4 W& W; x: O0 _
像www.aspid.cn的主站就采用了TSYS生成html文件! j- k0 U* `8 z0 j# G: M- p
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 5 i- \# a, [) J4 ]
: U8 b5 G" B" w. K9 ^! v
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
, R; y# C7 [; n3 vfilename="test.htm" + p3 E# v+ Y9 w; q
if request("body")<>"" then
8 @$ t* Y& l" Z5 |% r9 Xset fso = Server.CreateObject("Scripting.FileSystemObject")
9 v! }5 x7 w* I, T3 q+ nset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
2 {* o. X H. n/ J; yhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
) X: `/ M1 L e. [: v m; B$ Jhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ( |6 j, b; `& R
htmlwrite.close # b& C4 W4 P( y" ?1 q2 q! Y& r
set fout=nothing * b* b5 f) T/ u4 q% V6 \3 q) Y
set fso=nothing R- k3 O# M5 j- ]
end if + l6 [1 t' s3 v$ b0 n9 W0 r
%>
2 j4 V) Y+ H# \3 l$ \* e<form name="form" method="post" action="">
& }; c9 F, M: \2 ?- Y1 U3 ~<input name="title" value="Title" size=26> 3 p& t6 }9 d, ?
<br> ' e3 k ~6 b6 z( k1 ~% o8 x
<textarea name="body">Body</textarea>
" }7 o, V9 I: \( H+ o<br>
+ T, [5 n9 V$ y3 k<br>
, t3 ?) } Q3 ^4 Y3 q' ^<input type="submit" name="Submit" value="生成html">
$ @9 S; M+ f6 U. {. Z5 R</form>
& h8 ?! f4 m/ D+ j9 b K- b2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ! T. x5 D3 }, e) P4 c
template.htm ' //模板文件 <html>
" N, A7 f* r" ?; c3 n! r<head>
* ]$ [% `5 V' z- V<title>$title$ by aspid.cn</title>
$ A' j3 c6 ?7 P& P</head>
! y5 @3 Z+ ~+ b1 J<body>
4 e! e J) x+ A* j1 n$body$
+ y' l$ d; c; }+ _1 a ~</body> 5 B2 U/ }+ a) x3 k5 q
</html> ?
& `6 Q) l; n/ T7 ~* P; g( R. z! D1 T5 j
TestTemplate.asp '// 生成Html <% 7 `9 @7 ^; E) B; m; k
Dim fso,htmlwrite - w3 w) _9 J6 e a) o1 a
Dim strTitle,strContent,strOut " K/ W$ `( T! W5 ]6 a
'// 创建文件系统对象
/ C- |4 o6 O0 j" v. X# K' }Set fso=Server.CreateObject("Scripting.FileSystemObject") % ~) J( Y8 `, ~. J
'// 打开网页模板文件,读取模板内容 6 _# B- p7 N# [2 c- L: y. F
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
6 \& ?0 I7 G& p! L: a: UstrOut=f.ReadAll " }, a% Y! J2 q6 Z1 o+ x
htmlwrite.close
/ C! X! {" ~& Q5 o+ f2 z
6 z& P/ i( l2 l# u! A# w+ f0 sstrTitle="生成的网页标题"
$ x( d* z- U7 o6 P- e$ V# jstrC
# o% s( M$ p- b/ K6 C" |
$ a/ \) {- T. Q5 A'// 用真实内容替换模板中的标记
. x/ f0 C- t+ ~+ `0 D+ ZstrOut=Replace(strOut,"$title$",strTitle)
! k8 ^0 i2 R+ @* Y3 GstrOut=Replace(strOut,"$body$",strContent) ( d9 ? Z. L2 B/ r" O, P
4 M0 b. q5 Y- ~'// 创建要生成的静态页 $ r0 M8 D: o% f8 p
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) " l+ R* Q: K, J$ f6 Q# ?, R2 b
8 n# M; Y( m4 o'// 写入网页内容 ( M5 W; E7 m# B8 l, e
htmlwrite.WriteLine strOut
8 O- ^. t- T6 E9 b Z( [% y# ?3 q% Ahtmlwrite.close
/ u) v- t' s. }. T/ I$ }! ~* A( E. R& g/ k# Z
Response.Write "生成静态页成功!" * M4 M6 I9 H8 m: @
' w$ N( U$ u" }& j7 }: t'// 释放文件系统对象 ( O& k$ g) t- \" A& j4 ?
set htmlwrite=Nothing
; l0 ?$ u: m7 p6 n4 |' W! X7 m- Wset fso=Nothing
+ i% e" h8 I1 `# G' b3 c1 _%> $ q7 v2 F! v1 r4 ?! Q
v8 w# @' w4 _; ]6 }
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
, m4 G/ A7 H! U: K1 O2 e<%
+ X5 G+ L+ d2 q' I0 `1 d4 x l- [ F- b) a" W0 Q( i* p; E
'常用函数 9 E, s7 h: |2 P' z+ v, G
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
, {6 f6 K/ e4 T- q; C* O2 sfunction getHTTPPage(url) & D, [. z, K+ [7 M4 s* g3 E9 p- l1 H
dim Http & Y m8 d3 c. J! N
set Http=server.createobject("MSXML2.XMLHTTP") 9 f0 ?- N$ {) a( [6 ~
Http.open "GET",url,false . @5 B& F% L0 k* f) m
Http.send()
9 V8 _7 |9 X4 K* D" n) n6 |if Http.readystate<>4 then
! E% Y+ C: ], }3 v/ w9 Pexit function + K' k6 Q/ @/ L0 U
end if
/ @# G% m8 \2 w0 rgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 5 W" J; s: F$ q! g6 X1 ]2 i
set http=nothing ! a1 c7 u% e# e! h
if err.number<>0 then err.Clear
3 A, d6 h! E& B9 c& Mend function
# K$ |( `- G c3 Y8 b+ Q' q7 h5 Q
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 % h3 L+ [/ L% G0 r' i: @
Function BytesToBstr(body,Cset)
6 s1 }. f5 f7 K! Rdim objstream
4 o4 q- f6 L+ `4 F" nset objstream = Server.CreateObject("adodb.stream") 6 w" ~$ M5 ^2 L# [+ n! ?, P4 L
objstream.Type = 1 / a j. C6 ^9 q
objstream.Mode =3
* E. B3 N- @- O* ]. ~( `0 V3 x2 eobjstream.Open
# u% U9 h g1 b. }objstream.Write body
; I M/ G9 U; O* `objstream.Position = 0
9 J: k0 Y8 f; y) R: uobjstream.Type = 2
% x4 ^5 a) k6 V; v; r& J9 T' Xobjstream.Charset = Cset
' C. Q7 I& x2 X1 [" s- yBytesToBstr = objstream.ReadText 0 |0 n! @9 `7 J! N1 ?2 Y7 N# P) r
objstream.Close ' l4 j" ?: w& F" `. K/ Y0 T
set objstream = nothing
5 |) n* Y+ w( [2 K7 s6 G: X/ F1 {( e. vEnd Function
8 E6 S9 k; s. F# F& M
( K8 d- Q! C+ l% Q: w, ?4 c
% [9 [5 B0 R6 s$ W2 |' g2 ItxtURL=server.MapPath("../index.asp")
/ h0 ]+ K( i9 J! P9 I' F" a# ^$ ~3 Q. Z" B/ o# D" F9 _+ I
sText = getHTTPPage(txtURL)
% @% Y( `/ g, g2 O% F9 D" j
! `7 K0 ~- |) B% b- JSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
5 e; W, v& O% B$ ^( ~! i/ t; q* U1 m0 hfilename="../index.htm"
" v1 J* i; l' t6 R2 m. q- d+ eSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
( F9 V* ]8 j2 s0 J6 n' A$ DopenFile.writeline(sText) ( `: }7 Z9 z# j& s7 Q/ q3 @$ `$ \4 j
Set OpenFile=nothing 6 w2 W9 m) K" g% C+ _
. }# `- ^+ I' P- u0 v* F1 y, ?
%> 0 i) m% v% x2 p3 H8 p1 F7 ?5 A
<script>
2 Q/ W1 c0 c! v9 B N) [* u0 Halert("静态网页生成完毕");
9 U5 Z& ~$ `7 I9 Thistory.back();
1 R8 }, s3 x, L* T3 h( B</script> |
|