  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14221
- 金币
- 2401
- 威望
- 1647
- 贡献
- 1349
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
8 Y* d! `* L( T ?2 P, h像www.aspid.cn的主站就采用了TSYS生成html文件!
. P8 v4 J# e/ J& {所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
) {9 C6 o( x) i) {; T! u
" X3 ?. p( W% `' v1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
# @ N. K! f* N8 \4 q+ Rfilename="test.htm"
8 ~2 t$ I. j$ g) ]. ?8 Hif request("body")<>"" then , l9 {: P' B+ T5 ]- B
set fso = Server.CreateObject("Scripting.FileSystemObject")
) M8 t* @1 B9 J5 d1 s% o X* vset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
' t! _; y2 k+ Q8 ^& M$ K& vhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ' Q6 K- U9 V( C- T3 o
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
7 w$ T: t1 @. i$ Whtmlwrite.close * G" U* x3 h6 W
set fout=nothing 7 L: j8 S8 ^. A2 s. s# a0 ~( c2 Y
set fso=nothing ; G- m( h: M* E0 _# N
end if * |# m& L, G5 r/ x* s S2 M
%>
, ~6 t2 _, A) i% `- f$ v<form name="form" method="post" action=""> o) Y! n& \- h' `" c# q
<input name="title" value="Title" size=26>
: x' ^& R% ^0 n0 t! ^<br>
& J! T6 }) i* z/ D; g7 g<textarea name="body">Body</textarea>
9 `& g$ e& H1 b' `9 c<br>
2 F, I& R! ^# q2 x8 f+ S<br> - }5 e; ]' |2 i! M- I
<input type="submit" name="Submit" value="生成html">
7 r: |8 N. L1 m a</form> : `9 N+ j8 u& N$ j B3 {: g9 m& `
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ! C; t/ c9 w8 P& ]$ `( s% q8 L* d
template.htm ' //模板文件 <html>
8 V7 W9 [$ p x/ q; J( l<head> ; @: o5 O: v% E! m5 ?
<title>$title$ by aspid.cn</title>
( A" c/ C% h* y( m/ B</head>
- G& `. D* \2 @& K& x7 V; ^+ y<body>
& F4 B( i7 W* p: q. f# `$body$ 3 {' i: ~3 ~5 n5 a+ B9 Q: I
</body>
z5 k. X; ?, a& n" F1 V</html> ?
! s- e# V T( K5 X; X; R8 Q, b2 P8 E5 A, k3 @, Y
TestTemplate.asp '// 生成Html <%
! _. Y; O* ~' N. ` I5 RDim fso,htmlwrite ) A3 f c2 a$ k6 B
Dim strTitle,strContent,strOut
; C+ [1 H6 h/ \% o/ J& \. B'// 创建文件系统对象
" E$ ^$ @' R! v0 v' j/ F1 S9 WSet fso=Server.CreateObject("Scripting.FileSystemObject") 7 @2 k4 V. U0 b3 ^+ d/ S$ B
'// 打开网页模板文件,读取模板内容
' _9 u& ]; @% tSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
& V& I( s% D+ g6 zstrOut=f.ReadAll
! U$ M3 J9 ]3 uhtmlwrite.close 2 b2 t( F9 h# S" h' F
, F P5 J9 [/ r: V, O. j+ w: n8 f* D. jstrTitle="生成的网页标题" 1 F9 Q3 `. A m6 m) J
strC
8 U( ]1 }2 ~3 P8 g+ [
! m- Q+ s# ~* z5 F3 ^ }'// 用真实内容替换模板中的标记
& K9 L, r9 v! H' B; N* Y7 g: nstrOut=Replace(strOut,"$title$",strTitle)
' r* o, b; k. w. lstrOut=Replace(strOut,"$body$",strContent)
7 C( c/ @) c8 @" R. @
7 }1 @ P8 W0 l8 x1 O'// 创建要生成的静态页
1 M; B9 y8 N tSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
9 n" V$ }! f& p1 p* E" ]7 y- x6 Z9 L' f0 B6 \- m
'// 写入网页内容
0 ]. E' [ D( h) s( L' ^htmlwrite.WriteLine strOut
2 d* A; }6 R: Y- Q E7 p5 A/ Bhtmlwrite.close
* h0 T4 j2 P& B2 J* V4 \- } \* Z5 F3 s/ V
Response.Write "生成静态页成功!"
]& c8 R1 p1 ]: W1 U" |) O% U
+ S" Y: M! q$ ^# Q2 V$ M- C'// 释放文件系统对象
/ u+ S E( L7 u( wset htmlwrite=Nothing
7 |- _: c$ P; ]" _( @' O% e: nset fso=Nothing
7 q6 T; H( ~1 p. r" R%> 2 I" G; r5 p6 h- @; s
; l4 H7 s- Q# x3 F
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 0 \ |5 ~8 b$ u2 l# E
<% ' V7 c1 }) |2 A
: R9 P9 o4 w+ ]* `+ A
'常用函数
# E2 }; e8 X) _'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ; a% B6 J0 a: b- }
function getHTTPPage(url) - W. T4 x! J8 H1 @. i% B
dim Http
: m3 G' y, O$ _. l1 Uset Http=server.createobject("MSXML2.XMLHTTP") ' G6 L; z( p" G$ `( {9 P: r# d
Http.open "GET",url,false 2 C6 d2 N5 X* Q( X
Http.send()
t( |) R9 `* C1 M# B/ T# h- Kif Http.readystate<>4 then
6 d, ^* P# q1 Y8 I/ xexit function / H# N0 \- K" d: f: g8 E
end if * J: u, m A) E" R" V
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 8 o$ {6 B* j- c
set http=nothing # ]' F" B7 G4 t; W+ u8 w4 P
if err.number<>0 then err.Clear
# N3 m- ]9 f- y8 N& Y1 zend function & `% ]9 w8 F& n/ E5 E
7 F6 T7 C9 l- C' a+ C'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 ; W. r1 H9 h: E. E! i' ?
Function BytesToBstr(body,Cset)
~0 }/ j7 O- |1 \dim objstream
5 _/ L4 O: T' q8 g7 ]0 dset objstream = Server.CreateObject("adodb.stream") : e* [, W- T! J0 ^! x7 g
objstream.Type = 1 * f) g" ]" x5 T, D" Y
objstream.Mode =3
% h$ `2 d$ \/ f0 Y! w9 yobjstream.Open , U$ a# A' H' [
objstream.Write body
/ @8 P Y3 F* \8 Q+ C% M4 xobjstream.Position = 0
" ]- ^# V O: L8 mobjstream.Type = 2
; J( ~. m; o4 L/ M. @/ Lobjstream.Charset = Cset - h8 Y5 ?1 l- x' @, [
BytesToBstr = objstream.ReadText ; i! M$ a# G' A3 V/ K# ?/ Q
objstream.Close ; b) y% I M- Y. k
set objstream = nothing ! p, t4 J9 B& Q9 V) S4 ~
End Function
% }4 s$ [" ]/ r1 N
# l# @4 q6 e+ F6 K. l u, b
' [3 V% X/ e; vtxtURL=server.MapPath("../index.asp") ( q# k2 K; M7 Q, {7 h" r3 `0 H
- e8 `1 ^5 Y* |: FsText = getHTTPPage(txtURL)
5 o7 r! i% y# Z4 c/ V2 @0 T$ `/ O# E/ v. c: ?0 c
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
& k) q$ d! E7 h# C3 x2 d4 Vfilename="../index.htm" . M9 m7 C9 X; S! y/ R: f- j
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
1 Z i! d5 w1 wopenFile.writeline(sText)
9 L1 B1 W, G; J4 `) ^) @2 k7 u' WSet OpenFile=nothing
6 C% b0 m$ S0 K9 H! ?7 s9 z1 q( Q$ Z& R6 a+ I& u1 Q- r
%> 2 V: t& d$ |6 s k3 K. h3 |9 I
<script>
% U M3 h! E$ `# p4 `- kalert("静态网页生成完毕");
& J, i+ m& l6 O) \history.back(); ' {+ [2 h# S4 S
</script> |
|