|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14341
- 金币
- 2456
- 威望
- 1647
- 贡献
- 1404
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
3 f6 T1 L. p* F* K: _像www.aspid.cn的主站就采用了TSYS生成html文件! * {1 P! e, u2 n* i' P- V
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 8 R) M7 c9 k! i4 v7 M
# Q# A, V) S* }% P! r
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% ; n7 j3 @ M2 k H
filename="test.htm" 7 j+ y$ R- T! g9 V1 r( W
if request("body")<>"" then
9 e* u, z+ Z1 Q' W/ u. t: f- `set fso = Server.CreateObject("Scripting.FileSystemObject")
: d# I. z% z* V& ]6 h& m% Wset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
$ h7 H, @7 ^8 @6 U+ [htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
3 L3 A) P. Y+ @9 g$ mhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 6 g/ W! t g5 w. x7 b& n0 W
htmlwrite.close
. u t0 p- H0 F3 Q6 Sset fout=nothing
+ j8 B; ]0 {2 r- ?set fso=nothing 8 s* E- e' t# T) ]" V, k
end if
( ^: M+ K- p9 l' m* H%>
! n+ y2 T* `' o# v<form name="form" method="post" action="">
% J) ~0 b" p/ l6 J0 E/ I<input name="title" value="Title" size=26>
0 o5 M4 j0 R' d7 O<br> # F2 f2 B" [- l" L* {
<textarea name="body">Body</textarea> + A8 K" c8 I" I+ w6 o8 j- y2 ^
<br> : I3 Q; P. x8 `# v
<br> 6 G; [5 G3 T" o4 I
<input type="submit" name="Submit" value="生成html"> , W' B- d. c) D5 L" A7 {2 i
</form>
( M/ M) T9 d$ ]% S2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
( }7 x/ D$ P* Z6 E4 Q6 x I0 u: ytemplate.htm ' //模板文件 <html>
8 j0 U+ O- h- Y; g2 h. K- V" M<head> ! O! i2 b( f& _* u/ \ b& q
<title>$title$ by aspid.cn</title> 3 W7 P7 r' \ F
</head> , J5 L$ {; d. T l# c( M: r
<body>
% A7 c7 f% R. L9 s- w. r8 b$body$
b/ c9 \! D+ K2 c0 W</body> / v, j+ C. @1 t. O) s2 o
</html> ? / s+ D/ F* n& W0 `# a2 Q
7 }7 R" n. R3 C, FTestTemplate.asp '// 生成Html <% ; T* i0 r0 }9 K4 w0 I8 [
Dim fso,htmlwrite
4 \6 g1 M6 d0 Z/ D" @; ^+ xDim strTitle,strContent,strOut
- U. e' b/ i& h'// 创建文件系统对象
% H/ f" I6 i$ USet fso=Server.CreateObject("Scripting.FileSystemObject") ) e7 M. m |# c7 Z3 W o* F
'// 打开网页模板文件,读取模板内容 X' n T4 m9 B
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
. w r$ j4 ]3 ~! T; KstrOut=f.ReadAll K6 e- ~1 J/ \: G2 d
htmlwrite.close 0 y) I: W0 x' |: u% _
- w" v7 u/ q2 W o2 D' tstrTitle="生成的网页标题"
, O2 Z$ ]8 R$ G" k( ?! a: z8 J2 cstrC 1 C2 [( ^! k a4 C9 w
1 H3 Y3 i" v$ }7 p8 R0 m2 E4 G9 @0 t'// 用真实内容替换模板中的标记
2 k2 X- Q5 [0 P0 fstrOut=Replace(strOut,"$title$",strTitle) G4 a5 I" x _% \0 d; g
strOut=Replace(strOut,"$body$",strContent)
1 T! I# a" m" P) k! @* S
, r1 P- s1 n6 K' s! P. D0 a2 M2 e'// 创建要生成的静态页 - x2 ]6 V0 ~) j; ^% I C) a
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
% q+ u& m8 y4 \% E. H# Z3 H+ r
" X' @ |' }8 K' R) x# V: x'// 写入网页内容 6 W5 \8 E4 s! k- W E! J7 e9 @
htmlwrite.WriteLine strOut
; M9 I' A& }- \htmlwrite.close
$ b+ A. B3 R" l( L" t8 H; y F% {$ c# W; H; G1 q* w' ?3 A
Response.Write "生成静态页成功!"
8 o) g! l. x# p8 i/ f: v+ M. Y, y8 I
3 e: g( n: h, A, K4 ^+ w: r! q'// 释放文件系统对象 * P6 L, |0 \& \. \5 ~* ^
set htmlwrite=Nothing
+ q: y6 Z+ s, F/ y' v# ~/ |6 q5 Vset fso=Nothing
* w+ p+ k0 A$ I0 ?$ q4 C9 k. J/ c%>
/ I# |$ [, a% j' w) }. ^- A8 h, ]5 o' e x; a
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
, Z& D+ u* w1 L2 `0 @6 Q<%
' M' u) O6 p& M7 b1 F
/ a! P4 K+ ?% A- Y. c'常用函数
) V* \- S8 E* X9 e'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 8 a1 D, Q& A) I8 ?
function getHTTPPage(url) " ]! }! b k8 L; l' D
dim Http ! r! W0 E1 Z. R
set Http=server.createobject("MSXML2.XMLHTTP") ! l T4 V' s- t7 I9 L6 Q
Http.open "GET",url,false
5 a' @9 m0 }+ Z/ O7 e2 WHttp.send()
% @8 }9 l0 s$ U% K6 zif Http.readystate<>4 then
! e4 s: L$ Y: s- F8 q* Mexit function
; w7 L1 Q- t$ E1 X9 q- Send if
4 J% S$ j+ k; ~9 G+ v$ tgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
U9 h* u# Q6 w$ Uset http=nothing " Z/ p3 P+ z: p! f6 Q
if err.number<>0 then err.Clear 5 Y6 Q& i- U. e. w
end function 2 }$ I& o3 p" \
$ n; ]2 y" W# `) ~'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
3 k9 a9 Z( @% b. K% j' m& {5 yFunction BytesToBstr(body,Cset)
+ Y9 C7 P. W# s/ _dim objstream
3 L7 z8 c/ f( T5 D2 M# nset objstream = Server.CreateObject("adodb.stream")
, E2 }1 O: G, q: t. h( Dobjstream.Type = 1
6 R, Z8 i$ |! h8 d/ X7 F fobjstream.Mode =3
, ?" M n% e/ }; x, @, {" Z( _objstream.Open
4 `" l; _! X/ J9 p5 oobjstream.Write body . Y9 q: i3 m0 J( } B, d3 Q
objstream.Position = 0 : X1 W( k: N$ [! v, I; n( k- |
objstream.Type = 2 + P& R) X, S# W6 W3 p& X' W
objstream.Charset = Cset
' q* F* g: L* S9 U9 v `BytesToBstr = objstream.ReadText
7 r; a F/ j, m( S0 h0 w1 Yobjstream.Close
( W& ^6 P8 U( x( t6 uset objstream = nothing
" K# l* ?, N0 m) S3 l4 REnd Function 2 L# y6 K# O# s
5 }% ?$ ? X) t% t9 Z5 p3 k) q1 |' j
k' c% Z2 F( _7 DtxtURL=server.MapPath("../index.asp")
O3 B4 F+ x5 D0 i- z. Y# H! U4 ?
8 t/ s0 F ]3 f3 o8 X- D" ~) @sText = getHTTPPage(txtURL) 3 C: g3 l' L4 }8 D9 R* C
; S; ^: m6 o" O4 L7 x9 R* ^2 _Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
2 ?* U) W! Z* R! x3 w: _filename="../index.htm"
5 i+ y7 E7 n0 a- ], W' rSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
- C1 X( @7 j3 F5 v4 n# YopenFile.writeline(sText)
9 S% Y. k( \5 I d* OSet OpenFile=nothing
" t2 `# X4 h' }0 Z/ u5 M' _" C$ s% j) Y, Y
%> ' V3 z" m; w4 Z
<script> - d# L( H7 m0 A4 n
alert("静态网页生成完毕"); - O# h# L, o$ h" n) d) s: K6 Y4 P
history.back();
y# Z, ^ U3 M; [- Y. ?* ^# Y) Q</script> |
|