|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14411
- 金币
- 2491
- 威望
- 1647
- 贡献
- 1439
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
. o" m5 ^6 r" v6 P0 `% ` q像www.aspid.cn的主站就采用了TSYS生成html文件!
$ K: |+ w0 a3 _! b所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 1 `& s1 ^& S0 r9 T* f1 r) Q
! z$ G* c4 S7 j, j
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
& R8 n: n* M% _5 V3 Jfilename="test.htm" 8 p2 ?3 |# Z A$ U
if request("body")<>"" then 3 p6 h- ?. E7 A* l( ?$ c& s
set fso = Server.CreateObject("Scripting.FileSystemObject")
; Z' q7 N' ?! E; B6 ~ d. }set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
" z. ~8 s7 V# P' Mhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" - n( G& z; H: A3 E
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 0 m/ C4 T$ m! a* G
htmlwrite.close
0 s% U0 H, X- U: ?* Q" ]3 Vset fout=nothing 1 ~3 Z5 T: c7 T) s) {
set fso=nothing 6 ~+ f/ N7 w2 e
end if
4 u' }8 W$ Y& R* @5 L$ P* o%>
' A; {/ _; }1 f2 N<form name="form" method="post" action=""> ( t0 g& C: f/ O p& i6 d' ?: R- B
<input name="title" value="Title" size=26> T4 s# v* ]0 G/ ?! ^; ^1 e8 E* v' ~/ x
<br> $ {* A1 ]. V+ Y$ H3 x
<textarea name="body">Body</textarea>
0 v- z X q2 g& k) Y Y/ p<br> & \2 B6 n. t4 z! S
<br>
$ C# X' R U+ a<input type="submit" name="Submit" value="生成html">
: N2 ?" M8 w2 s' k</form>
- R. P+ x, F$ v2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 1 g0 {$ S( m- t1 O1 b1 ?6 a% }
template.htm ' //模板文件 <html>
% q8 R# v: C7 Y* g<head> 9 S. j- D5 W; |; Y* m* [, F
<title>$title$ by aspid.cn</title> 9 F# W& t0 ?) T
</head> % ~ S8 F6 G( Y
<body> 5 V6 C4 d9 v" k& ]% {
$body$ $ h4 v- Y: N8 g5 v) D; \$ H$ |- p
</body>
! h8 n6 D8 s0 s, e V4 Y' |4 j& v3 Z</html> ?
4 S2 z' h) |* u J4 F8 g4 S8 l! [2 d, F' [
TestTemplate.asp '// 生成Html <% / s6 M5 ]& ?! \8 u* ]
Dim fso,htmlwrite l1 \0 H' Y2 |# X4 g: W* j
Dim strTitle,strContent,strOut v( R$ k& i# ], ^2 s
'// 创建文件系统对象
6 T' Z7 Y* L- HSet fso=Server.CreateObject("Scripting.FileSystemObject") , [* _$ X# C. r3 }
'// 打开网页模板文件,读取模板内容
. l3 k- ^# G" ~# N9 y8 nSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) M3 W& J. U* Y
strOut=f.ReadAll 8 R# Y, C A% A4 h' v
htmlwrite.close
$ ?5 p" m; P, X8 Y- t
4 `- h2 [: \# P+ y2 n1 o# V0 `strTitle="生成的网页标题"
1 ~- h' ~1 q% BstrC / a$ I6 {; }! J/ F a/ N. i
) B |0 r: V) ?'// 用真实内容替换模板中的标记 . A: D: k' k$ [0 D6 j
strOut=Replace(strOut,"$title$",strTitle) , D9 k6 k! b3 i
strOut=Replace(strOut,"$body$",strContent) ( b2 I3 u1 P8 q
& @8 H+ i- s/ j. x( r'// 创建要生成的静态页 1 C% s0 \0 C, Y9 W6 |; h
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 0 c& N# k# @, z2 B- W% r
$ x) z& a* e1 ], n A$ v( m! x'// 写入网页内容 ; R1 z& V! }3 M1 H7 H! ]& C
htmlwrite.WriteLine strOut , I; J7 ^4 u# h5 w: D& V3 }
htmlwrite.close
0 d* y, u( Z4 O" c2 B3 h$ c
2 N& p5 e& Z! CResponse.Write "生成静态页成功!"
* j7 Q3 G X# i0 ? y1 h0 V0 b) G: i; o: z. u3 x8 R
'// 释放文件系统对象 2 A$ @! }" _! `( ], V
set htmlwrite=Nothing + `# D c( h) p. h
set fso=Nothing
3 ^9 b0 c6 t/ t5 R2 Y%>
2 S* k! C- [3 R& m1 V8 w- J
! q1 Z* N# B; U3 U/ V$ T5 K. ^; g( u3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
! B! j9 ]: j4 k1 E; }<% , l6 g, s- L8 k1 L+ K6 s8 w @
7 O: G% T! ~4 n' b'常用函数
1 W& _' O& A6 p* M- d'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
H8 g/ @7 K4 @8 l. k. ]2 X7 Gfunction getHTTPPage(url)
1 m1 ^4 K9 x) j/ D) hdim Http 0 |- y( w5 K$ `8 c$ \9 m
set Http=server.createobject("MSXML2.XMLHTTP")
% @9 A' I7 q$ gHttp.open "GET",url,false * G, Q5 K6 Q4 M- @7 c' T. P% J* F
Http.send() : q ?0 k, e+ I. v
if Http.readystate<>4 then 1 i6 |* V! i. ~; z I2 O) ]
exit function 6 k5 |/ | i0 M& |; a! V2 Q: |
end if 2 p6 l% m7 a" h1 a2 v( g
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 4 n' m+ I; v1 e: b6 d$ W: i' B
set http=nothing 3 \2 l: E- \: \- h4 a9 W
if err.number<>0 then err.Clear 5 [, r4 M# `7 X: }
end function & h c2 Y1 j) p9 ^
5 G3 M. z$ s$ i& i/ Z+ \1 P
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 . X* @ \7 L$ ^8 G' o1 j# d2 g9 X! }* U5 V: M
Function BytesToBstr(body,Cset)
" U. b: x {4 s! a2 Ydim objstream
7 j) G K9 O3 C2 q8 }8 b5 xset objstream = Server.CreateObject("adodb.stream")
, p/ T) o5 k1 Z( e n3 I: nobjstream.Type = 1 1 N, P9 l3 `( @1 R. F
objstream.Mode =3
; I5 y+ M! N3 T3 p1 Hobjstream.Open ) H- O) S; F3 d, v" k2 W6 t; Z
objstream.Write body
. t4 m0 o2 n8 B9 o, k- aobjstream.Position = 0 + ^8 y" w* |" E4 Z* h
objstream.Type = 2 1 v2 z, Q2 z z+ t
objstream.Charset = Cset
; o5 m2 w' `% M1 ]: |9 m! |BytesToBstr = objstream.ReadText A9 r8 E0 [# K% e
objstream.Close
! X, ^- W: U4 eset objstream = nothing
1 Q1 a; Y0 B# B4 E. eEnd Function 2 R( f1 d5 x4 x7 _ ^4 F
+ H" }4 Y8 z2 t$ f
# H( k, E" @3 D$ D7 X, V+ _& vtxtURL=server.MapPath("../index.asp") 0 y9 d. p7 U, {& x# \5 `; U2 r
6 n' J$ L7 J) c4 F. v" \3 |
sText = getHTTPPage(txtURL)
1 k1 Y: j$ x, h; C2 R0 k, w0 f9 F) P* I1 r, T0 ^2 x1 i3 B7 y
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 0 S8 H3 P! H1 {1 m5 \
filename="../index.htm" 6 A& `, m; L% B3 [7 g% `1 ]
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ! q8 }) _- N/ @. {" r9 T
openFile.writeline(sText)
L1 G2 J5 }4 ~* f$ iSet OpenFile=nothing 0 S3 {0 H W2 }8 y- `9 d+ I
: E' z( r1 j3 n# D! }' @
%>
1 v3 I8 c% a+ E% H: A: [3 f0 h1 t<script> 3 h$ A5 \5 V4 k) T
alert("静态网页生成完毕"); ; }1 Q6 V1 q1 @( o' h! u' g' w( d
history.back(); ! b( }& j8 `2 H* y
</script> |
|