|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14381
- 金币
- 2476
- 威望
- 1647
- 贡献
- 1424
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ! J' s$ J; W- z3 c+ o
像www.aspid.cn的主站就采用了TSYS生成html文件! 4 c* @6 G( |6 ~8 _' N$ N) _9 z6 i
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. - x4 l) U8 h) l% C
% S& t+ `0 X2 t- A C% ?: r5 }
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 6 [: `3 U: M& w
filename="test.htm"
; f/ ]) Z2 M+ W' J, ]( `if request("body")<>"" then
6 a" Z! s: v& w1 Wset fso = Server.CreateObject("Scripting.FileSystemObject") 4 p0 c. ?# z3 d6 K7 A
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 6 ^+ t+ W2 v& K4 U' R
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
( E0 D+ i8 z, W* R- I; i& Z# a( p% chtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
6 T: y! Q' H% z1 ~$ B8 X, Qhtmlwrite.close $ ?: C F- Y6 X# q& b0 H
set fout=nothing
7 \& a8 A @; V: \set fso=nothing 2 Y3 O4 n8 s1 @' K
end if : W) d2 x; E% x# R0 u$ F$ ^, m
%> & h. h& K H9 U; [
<form name="form" method="post" action=""> ) h( z4 T: Z5 E' x- [3 z+ v
<input name="title" value="Title" size=26> & N$ m1 n' O8 ?1 q$ x
<br>
1 x5 s1 k- P7 }+ A* m" q. \<textarea name="body">Body</textarea>
! h, M# K3 {/ o1 G: J6 g) l<br>
' H1 i2 Y( o7 h* t" L0 F2 `<br>
5 V+ i2 f" H" G<input type="submit" name="Submit" value="生成html"> 3 R4 L1 W7 R% b& _' {1 x1 M/ x+ {9 E
</form>
0 _- q* `2 H# E: x S2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
* p- E6 m6 V7 @1 V9 v! u" k D) |/ htemplate.htm ' //模板文件 <html>
* R: `! m; v' m, i<head> 5 s9 _" E; o; D& ?
<title>$title$ by aspid.cn</title>
; ]5 |: `$ S* U6 c4 j</head> ) f! b ] ^# z/ B" Q7 ?
<body>
! F# j$ m+ G2 x* |$ U4 b7 i" M$body$ 3 p2 c+ x# l1 }& U
</body>
5 o T5 Z6 P& h& B. Z</html> ?
0 }+ }' ]5 L8 V8 F
9 \, ]9 o0 E }& V6 N4 n* x5 s- NTestTemplate.asp '// 生成Html <%
# V% X7 n8 U( _& _) ^. O; b RDim fso,htmlwrite ( u5 P7 l" l. ?* G
Dim strTitle,strContent,strOut 6 a. C+ D; ^; o& u% X7 z9 \: m
'// 创建文件系统对象
) x9 x `- K( u* {" N' fSet fso=Server.CreateObject("Scripting.FileSystemObject") % d5 Y% P- R' _2 n
'// 打开网页模板文件,读取模板内容 9 F/ |* Z+ z( v& c+ H
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
1 Q& h p: y8 hstrOut=f.ReadAll
1 f& v2 @5 ~4 q4 X0 u' ~ P8 Shtmlwrite.close
* ?* W( H, t( z! I9 I% V
# O `) T& k+ {1 P% \ C4 U, ustrTitle="生成的网页标题"
: I! C( J( `3 a& O4 sstrC
/ ~3 O1 [$ o9 c3 i1 B2 k
4 E- A+ c, V( |' G8 l* ?* d'// 用真实内容替换模板中的标记 $ a# g6 z4 ]% V9 _$ K
strOut=Replace(strOut,"$title$",strTitle)
" C' o" n6 V5 V ]strOut=Replace(strOut,"$body$",strContent) - U: [% \& p/ N6 x! D4 l
5 l! y2 b; u# |'// 创建要生成的静态页 1 L7 G3 ?6 B! H- x# w- M
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
2 c- X' r, g! x$ G9 J$ ~) c- q h/ _# P$ V6 Y" M [5 i; s0 \
'// 写入网页内容 ; E e: {% r7 G& y! e- c
htmlwrite.WriteLine strOut
. [8 E9 S- C. N2 F7 h3 c7 bhtmlwrite.close , z, t8 g1 n" z' |' K2 Q* k
& b* U, S; A: s. Y- o0 rResponse.Write "生成静态页成功!"
7 |1 G* J1 k8 e5 Q+ ]: p1 q8 H" d' Y
'// 释放文件系统对象
o, C l/ t2 X: Wset htmlwrite=Nothing ' N& z" C0 L" V
set fso=Nothing
, i, H9 b5 x! K* b" x9 z%>
5 c9 m- v" @! e6 b
! A( n4 {) B, N+ ]! l( d* c3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
- f1 p# v# ], E% n6 C, J<%
# {/ C% A4 `* o- A, c. ~) V* H6 S- Z+ p- Z% _
'常用函数
* K) H! a0 B" V6 Z6 t' d/ Q( E2 V7 n; J# U'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
7 k& F$ s0 H0 ]& S* C& M! Jfunction getHTTPPage(url)
( _1 n8 Q- ]( [dim Http * K8 X. m3 U" o4 z" P
set Http=server.createobject("MSXML2.XMLHTTP") & ~# o) } O" `, c/ x/ |
Http.open "GET",url,false ) R: A- o- p4 M) ~# d. K
Http.send() 9 f* l/ v$ A; s% m& v+ R8 v
if Http.readystate<>4 then
; ?6 S+ e) a/ k$ b7 y7 D6 t- Q( mexit function 3 E- x8 T2 n( }. N
end if $ o9 N( Q* ^0 P& W
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
* t ~7 k8 a4 c1 [ Z" l0 T. Kset http=nothing : K6 s/ T' i0 l; X* o
if err.number<>0 then err.Clear
9 m$ x' R" ?' `2 \3 V$ _9 n, Kend function c( v, Z( j) s# C( N6 r
1 o8 p, x0 [" J2 V
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
* S0 r t1 u# ^Function BytesToBstr(body,Cset) - u4 j3 ~9 }! z4 C6 v! t" E
dim objstream
5 j- c: L& l" x( R# b& o5 K6 {set objstream = Server.CreateObject("adodb.stream") % C ]; \6 K+ R; f% o. |
objstream.Type = 1 7 @ G( z* q% I* `% x# }9 f
objstream.Mode =3 - H1 K! P& R1 k/ G7 g+ F
objstream.Open
1 ?. c9 Z( f! P9 [' Jobjstream.Write body
, t5 [4 T+ d" [# U% Nobjstream.Position = 0
' }# Y+ B; s' ^: n# [objstream.Type = 2 $ D" b3 ]$ T/ W- L$ \4 i
objstream.Charset = Cset
E* d# }9 m; p+ a( V: SBytesToBstr = objstream.ReadText
, ~) d4 K5 y/ C0 q% nobjstream.Close # s; w- U J6 ?0 C; n2 M
set objstream = nothing
9 A3 `6 ]& L/ e; @# ~2 K$ H8 U$ XEnd Function
9 ]4 d3 l2 E) u# M* c1 v% b- ^/ ~" b) ~
1 N8 [0 s9 G; J4 a- i; atxtURL=server.MapPath("../index.asp")
. c- q2 I. c8 u# a5 ]2 ]! x
8 {: ?# b7 b) f2 S1 `4 r8 KsText = getHTTPPage(txtURL)
* T8 D; c; k4 i7 W2 J P* i4 J9 w
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 1 }. {6 ]3 ]7 ^: k3 @
filename="../index.htm"
) U7 I o5 w: H4 V. r: C7 v1 CSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
* V; l2 m+ L, dopenFile.writeline(sText) : l& g4 \, Z2 d7 w; s3 N
Set OpenFile=nothing # W# g0 M- l, ~# m
) Q1 r+ e8 g" z4 D/ h* y; ~4 a%> ) G& _- z: q) f m6 h
<script>
. z: w: k$ B1 i/ c; ]alert("静态网页生成完毕");
. D" Q' B( P: \! \3 lhistory.back(); : ]) `- Y, e7 B0 j4 p2 B0 @, c
</script> |
|