  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14197
- 金币
- 2389
- 威望
- 1647
- 贡献
- 1337
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ) t% v+ ]: b+ p- T, l) C
像www.aspid.cn的主站就采用了TSYS生成html文件! $ i( m, M( J) U* @ t
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. " w- g& _ `, S0 W
( {9 H8 t0 A3 v7 l6 \' u4 C; C1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% % e- \( P; N9 M4 c9 R n. J
filename="test.htm"
1 N( N' I( a( M' q7 |if request("body")<>"" then , M \9 T& I( b& L5 ~6 \9 |9 ^! j
set fso = Server.CreateObject("Scripting.FileSystemObject")
+ P# E* _ w5 y8 F8 h2 Lset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ) B! M1 i- x2 l; s; Y
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
/ W3 a' p7 V* Q& fhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ! A7 e& P6 y; I
htmlwrite.close
" f; N* Q- P4 q- \: ^; Dset fout=nothing
7 |1 _0 j# N* e Mset fso=nothing
; n, }* v' M4 I2 E9 Wend if & x T9 o7 u/ s' x5 z
%>
" E; u2 Y- O1 h5 I" h<form name="form" method="post" action="">
! I$ ~' ~3 N* n3 L7 r<input name="title" value="Title" size=26> 6 Z/ n6 Z% I" d" ]" U
<br>
* ?: s y2 _8 z<textarea name="body">Body</textarea>
# o8 R6 \* T0 t9 m* l' p* B# j. R<br> 2 U) G+ ? h0 |2 r( Z; r1 k
<br> ' g: t. f5 g) L( [4 X
<input type="submit" name="Submit" value="生成html">
2 d+ C. N! a( w# f% F</form>
$ e( q" ?/ A6 ]0 \( R2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ) ]- A5 u4 T+ W& d& h' z+ s
template.htm ' //模板文件 <html> 9 j/ f/ u) z1 `# |% W2 n! ?
<head> C. X6 m/ z8 r* W1 I
<title>$title$ by aspid.cn</title> , w5 A( ^5 z% T" P7 I5 z% K+ W o
</head> / Y4 N' n$ f Z$ j, q/ b
<body>
% B& U9 I0 W8 W X: @5 ]8 _$body$
* S! Z- _2 ]" m$ x) J! G) ~</body> 7 O6 o# A/ l% F( R& @; H9 O' o$ F
</html> ?
# J% y1 C; e+ C, u& s
S' X6 I6 b. M# g8 ^& `TestTemplate.asp '// 生成Html <% 4 d9 z0 l. f& L y- E/ c. U
Dim fso,htmlwrite M* m0 G+ j. R- Z; P. z* V
Dim strTitle,strContent,strOut 0 p, Z1 r' O& Y5 ?6 S
'// 创建文件系统对象 % [* U9 |2 T+ K6 r. U
Set fso=Server.CreateObject("Scripting.FileSystemObject")
. E% t b( R+ l+ f# H/ h'// 打开网页模板文件,读取模板内容
. X8 E: a) G, U$ DSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
, o) b5 e, i7 x( e2 c% ?5 ~# zstrOut=f.ReadAll
2 E" k5 K) B; Zhtmlwrite.close
+ @1 i& \2 p2 M" R2 c; ~2 f
3 B/ l* A/ B6 d& y P7 [* FstrTitle="生成的网页标题"
6 ?/ d, D4 s: c7 BstrC
N8 h* x/ g" E4 v) j1 z7 u8 b; G1 Y$ @9 `5 I- Z
'// 用真实内容替换模板中的标记
7 g4 c% s7 D. J* f- n5 V% astrOut=Replace(strOut,"$title$",strTitle) ; a2 F9 [4 y( L
strOut=Replace(strOut,"$body$",strContent) 5 J0 [) e* z0 } ^9 R0 q9 u
; |, c" B; K f v n
'// 创建要生成的静态页 " z3 F3 p: F/ K
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
: K5 Q" K+ G& p+ ?* d, Q6 s
' T! q6 k$ B& i6 V% L1 t'// 写入网页内容
; K* w: B+ L" |" G9 ~) d* dhtmlwrite.WriteLine strOut ; |, E- B: ~% G, d
htmlwrite.close
/ `$ t4 n% u9 A8 A% r/ N' D; m- i' K4 E- V( I
Response.Write "生成静态页成功!" ; f) h1 ?% S# S3 v6 Y7 D; g: ~
! |. N, A1 s! o4 n. \'// 释放文件系统对象 7 V. [9 V3 P1 e. ^9 A l' r" d
set htmlwrite=Nothing
' d1 p1 ]4 `# F$ r8 e" U; [set fso=Nothing $ K; j) x3 A/ g" u
%>
& O/ T3 q \- e/ N: n4 z* O7 V2 q' b8 \: { V& Q( e7 @3 c2 ?
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
% \- o) i! w8 P- | `" @- V# y# }<%
6 [1 b* n3 a$ s. a) ^
5 V( M4 x8 ~, ^$ m4 w, ?'常用函数
" i9 u: _1 r! g# y* W( ~'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
# [, B; s. i: F# S& U, r9 ?! hfunction getHTTPPage(url) ! r1 ^. q: h1 }& Q
dim Http & O/ z6 m8 F, Z* y- x! D4 r
set Http=server.createobject("MSXML2.XMLHTTP") * E- `5 v2 B8 h
Http.open "GET",url,false " |% j3 C i( E' V h
Http.send()
. _ T! j) o7 b* B" Aif Http.readystate<>4 then
1 r; e5 a# o0 u9 v1 Y2 U& Kexit function 9 W5 I% Z+ ]; n% n
end if ) |9 ~8 K7 J) Q( |$ i- ]: I
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 9 z: C" P. J( B
set http=nothing 5 }- s- C1 S/ y2 E: H
if err.number<>0 then err.Clear : q! J4 t4 s$ K, z. U" Q
end function * I/ P) d7 M- P. P
8 J5 D! S, f3 L'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 ! [$ T* c6 n: l0 u+ O! v
Function BytesToBstr(body,Cset) 4 ] N/ Q P. U1 g
dim objstream * E) U, n% v0 i% S/ n, r
set objstream = Server.CreateObject("adodb.stream")
: {1 d% L- U6 G7 t0 _" `objstream.Type = 1
" O4 u- _2 x1 S- Fobjstream.Mode =3
+ _7 W8 J; f" p& ~9 ?$ ?# {8 j3 ]objstream.Open
6 c# I D! o j& j$ Yobjstream.Write body 6 v+ k ^3 M: W" M
objstream.Position = 0 7 u" h8 q0 A. w% Y$ J+ v
objstream.Type = 2 : u/ n7 M/ ]& s
objstream.Charset = Cset 0 S8 L+ r$ ~/ ]- t1 O5 }% x
BytesToBstr = objstream.ReadText
% t- f3 M$ x: e. O6 tobjstream.Close , U$ W, k/ H( \4 D, s8 c1 h
set objstream = nothing
P2 p9 x8 `6 j3 nEnd Function ) b9 T0 z0 G1 V: H
3 }) b8 e: k w. H2 Z, e, u
6 T) [5 M' F! N8 u4 m& b
txtURL=server.MapPath("../index.asp") @( {; L3 ^4 H& K6 W* t1 N
) K. {3 h7 n9 w2 B/ H1 d0 @ L
sText = getHTTPPage(txtURL)
1 H4 A" m4 Z4 H' p9 @) q/ e: k# K* D8 ]. B
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
1 M9 }: x+ a2 l; R, K0 M8 J0 Jfilename="../index.htm" " @3 M% h8 \& l2 Q! j, a
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
+ J/ s5 I+ F; L, M$ \, DopenFile.writeline(sText)
E8 y: b4 f5 HSet OpenFile=nothing
6 C$ I& K9 U# P8 x" c# ?
# b6 W' |4 X& Z/ B1 l5 f%>
' X, ^% h7 ^% N, x% r5 f$ l1 l<script> , z2 G: ?. I9 G' y8 C4 v
alert("静态网页生成完毕"); 8 @2 M. F2 Y3 }, b1 U2 [5 ?
history.back();
$ l$ ~4 I' X$ }+ U</script> |
|