|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14341
- 金币
- 2456
- 威望
- 1647
- 贡献
- 1404
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
8 t' L3 g8 L7 N" I像www.aspid.cn的主站就采用了TSYS生成html文件! / w) p- m5 W( G0 `% ?5 ~$ {. Y8 o u5 t
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. ! T% W* F5 T" D% n% F9 y7 Q
8 _9 G* ?' a2 t% Z* V1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 0 V7 q" T2 u1 Y6 v% i
filename="test.htm"
+ n4 B/ l5 ]2 i+ V/ rif request("body")<>"" then 6 ~3 g: x3 S9 J* E$ e# X6 t
set fso = Server.CreateObject("Scripting.FileSystemObject") 4 K' l* s- z" d( q% R$ ?& a
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) K, r1 F/ e* H" v0 C/ f2 U% v
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ( X& w. @; k) a4 S9 C8 R6 l
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ) x# j8 N; k. }
htmlwrite.close
1 d. r. Y* [" s8 C* E$ a4 k4 Dset fout=nothing
: y( @5 I' c9 v5 f# Gset fso=nothing 7 F+ L# }$ G- y% q9 C# w% j; D6 }$ q
end if : B* X+ V5 d# D. n+ `& c
%>
' R, W z; P* {/ t4 c<form name="form" method="post" action="">
% C* e; u3 z* Q" @0 t( x4 u<input name="title" value="Title" size=26>
* b* L/ p7 e! H _0 O3 t% x<br> : P$ r3 p4 I3 c" ^) D" G* j
<textarea name="body">Body</textarea> ' U+ H& G; z5 A0 b
<br> 9 ~! e" z# [' a+ q* v) W
<br>
2 G8 d! T6 U8 Y8 ~+ N- C5 T Y* h<input type="submit" name="Submit" value="生成html"> _# R, n! l7 A% m8 z; U* S
</form> * M( _/ N' o1 @+ X0 s* g4 @$ b
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 6 ^; I4 Y" u- `9 C3 }( Q4 X6 c
template.htm ' //模板文件 <html>
[3 x2 c5 c( x7 P* Q<head> ; j j u' e e' p. Y% ~2 @1 A
<title>$title$ by aspid.cn</title> $ U" A" a5 H* K
</head> & ^5 v |5 ]1 p2 c- z# @/ u
<body>
# @& A8 ^6 R1 c) Q$body$
3 N( J* j( _% G$ l6 }/ {</body>
* A% Z' I6 \6 `; ]7 @# H; h</html> ? $ Z6 y' }/ F8 ]
' q! w% O% ?$ r; ?
TestTemplate.asp '// 生成Html <% # O! I; e4 V D6 u, Y+ H8 Y
Dim fso,htmlwrite $ y3 E* q& Z% j9 x
Dim strTitle,strContent,strOut 1 }/ ~( i6 r3 e5 n7 u
'// 创建文件系统对象
* j+ l- h7 l- u" j& r9 ESet fso=Server.CreateObject("Scripting.FileSystemObject") : H, O I" \. K# z/ d/ ], {5 n) y$ l5 E) D
'// 打开网页模板文件,读取模板内容 , Z5 F7 G3 [+ d' B \
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) ) ~' b ]6 ~2 I- X" b( r
strOut=f.ReadAll & S! Z' R' U# y0 r+ q0 C
htmlwrite.close
1 M0 J! o0 m7 C2 n! f, }1 R1 t& N& }8 D
strTitle="生成的网页标题"
3 B$ f8 Q; H& q- s9 n$ IstrC
. w& b9 P3 w" `0 a( ]9 K
* K0 M) U' e! K( @! s( r, y) L'// 用真实内容替换模板中的标记
~; e- m4 }: @ K9 y( G Y; ~strOut=Replace(strOut,"$title$",strTitle) h+ O9 { r& u5 ?; J7 }1 I& ?' f
strOut=Replace(strOut,"$body$",strContent) 4 p" T& [! c5 C
) U8 p& p7 M3 |* Y' Y
'// 创建要生成的静态页
* z$ J. s! B4 W* B5 l% gSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
* h1 J; g- z- O1 ` |8 N' v) ]* i: A. x$ f( w
'// 写入网页内容
; T" m$ i3 w0 {5 Dhtmlwrite.WriteLine strOut
5 o8 [7 N. e& q' G1 ~htmlwrite.close
; K# \" b% s' w. Z
4 `& s& |3 ^: p& a; Q: L- W8 hResponse.Write "生成静态页成功!"
: G- u8 L# c+ s% H4 p. U
/ ? t& L9 M7 \'// 释放文件系统对象 2 M) F4 X0 i2 Q6 L; S, h& x& L
set htmlwrite=Nothing * k+ g! r7 V7 E6 m# t p
set fso=Nothing 3 L: }! T6 J$ k5 J1 f
%> : E& k- U: V) r6 ^( a
* X# D0 a$ ^( `$ A! o4 L: a( k
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. & |% i$ [5 b' I+ K! c5 l' ]
<% ' T5 [' I4 p1 d( r9 U
- g: c" ^, r9 g% i'常用函数 $ r0 M7 y9 z" B1 F, a* R
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ! Y$ k6 j- L B, T
function getHTTPPage(url)
; x' }/ k4 b6 U4 ^. rdim Http 2 h) L; P: |: y# w5 K
set Http=server.createobject("MSXML2.XMLHTTP")
: f6 M5 [3 M( O9 fHttp.open "GET",url,false
" `2 U; Z8 d) N( X. cHttp.send()
/ ~7 Z& h* C4 B& D4 ]' _7 Yif Http.readystate<>4 then . T. m% Q x" Q2 t" J. T
exit function P. t3 E6 u- Q: N
end if , l5 W2 E' ]8 Q$ g- O
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
! q7 f5 W. E" \$ h! j' Sset http=nothing
8 M$ L) a* h. K$ C# c1 |& hif err.number<>0 then err.Clear 9 V3 k. V/ I1 k9 Z, G: o
end function
' p* I( S) p: A+ M) e) C7 \# O' ^ p' Z2 K5 a3 M
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
2 p# Y! O1 |( K5 q( b1 aFunction BytesToBstr(body,Cset) 2 v% ^& D4 S/ u* a# e7 {0 P
dim objstream ) E) g0 C) M7 ?" s; d; @
set objstream = Server.CreateObject("adodb.stream") 5 Y4 }# d3 P) h- z
objstream.Type = 1
! i! _! ?8 l* p2 z! \8 @objstream.Mode =3 " L$ i( `4 A& a {8 x7 w: t
objstream.Open " ?0 H u) }6 u( `
objstream.Write body : C/ W& P5 N% c% I, w& W
objstream.Position = 0 3 F. g# Y/ t" z+ A2 z
objstream.Type = 2
2 t* L8 i s' _/ I6 n robjstream.Charset = Cset
5 o* \ p: {/ YBytesToBstr = objstream.ReadText
5 C# V. n# X4 r/ ~3 E- Aobjstream.Close ; n, \* d" r$ r! `$ ^2 w
set objstream = nothing : ?2 M5 k. c. M' r+ ]8 Z
End Function " O' Y0 w9 E: x2 z* ~
; }# Y2 B$ F' j7 W% w& u$ z4 w7 }, G
txtURL=server.MapPath("../index.asp") : ^; R; i$ ^4 q8 P7 L- K, p
& H o# L0 r3 T; x3 K# F$ S4 ^sText = getHTTPPage(txtURL)
# H3 m, K1 y/ U, |* w
) Y+ W7 _' [- Y) ^- r# e7 T) R4 ASet FileObject=Server.CreateObject("Scripting.FileSystemObject")
+ `4 f. O) {7 p# u( @filename="../index.htm"
$ l$ A% Y0 o8 s" A6 R) R" WSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 , v9 M: r5 A0 f3 }
openFile.writeline(sText) & P- N+ D& h/ t, y0 s
Set OpenFile=nothing & ~0 w9 P2 b8 }) m- e7 M6 I$ {
, E8 w Z" M7 F%> # Q/ j4 D/ M, J1 |, n0 A1 k0 f
<script> 0 J: T' r8 z* G, }; W! i
alert("静态网页生成完毕");
# g6 T! f; [; ]history.back();
* Z9 s2 C- p# V- i" y' J3 q</script> |
|