  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14173
- 金币
- 2377
- 威望
- 1647
- 贡献
- 1325
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
! V3 c2 L1 s* W" z' `像www.aspid.cn的主站就采用了TSYS生成html文件!
7 _6 e2 P. d. u& t! v# |0 T: R v+ f% S所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
7 B3 E% Q7 x+ a8 |9 W: `4 P* y$ n- b4 c5 p6 T% m
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% + b$ P8 v) K; n( ^! S8 k
filename="test.htm"
% g5 [8 `, G! N( q0 [' t, r8 Fif request("body")<>"" then
$ J6 B# Y4 X4 U" M; P2 wset fso = Server.CreateObject("Scripting.FileSystemObject") ! n$ T% c. [4 O F" q$ c0 ]9 m
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 9 U: z' l m9 T, v
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" " T U6 C' b: M
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
0 p- U3 A8 n- `$ y$ k7 Ahtmlwrite.close - e* s' Q4 y4 A2 y9 e" m
set fout=nothing 6 D/ t) m- Z( r; k4 G
set fso=nothing
7 Y5 V. ]9 ]! e, D4 ?end if
+ @$ b" W( ]1 R%> : E: e+ A I; P' B! O4 L
<form name="form" method="post" action=""> ' Q* o( R+ k7 w
<input name="title" value="Title" size=26>
1 W: J- d6 Q9 D: O<br> 3 S0 ~% `( }, `; h! w& F
<textarea name="body">Body</textarea>
0 r5 F$ y6 B+ S; v: j9 L<br>
% V3 B9 Z, ^$ M<br> & _/ N/ L" [- Y5 n5 ^
<input type="submit" name="Submit" value="生成html">
y- ?: Q9 z1 s- L</form>
' r, g3 d3 t- _. p2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. : Z% I( Z7 S4 J# F
template.htm ' //模板文件 <html> % n h7 ] h( u @
<head> , K u7 S3 l' j6 j6 `, m
<title>$title$ by aspid.cn</title>
3 I5 E0 \( q: h J# t" k; t; N* J</head>
+ U" A% Y+ ]+ N1 r2 h<body> ' Q; r3 A: h; @) [, l' O% ]: ? j B
$body$ ( ]- u9 h) I9 e* ?1 U
</body> ' r2 l+ c& |1 I& j" \8 J# [
</html> ? . h: q4 B) ^7 ?9 @" t
! ]/ n9 d) L i9 |& WTestTemplate.asp '// 生成Html <% * r) y1 |/ Z, M
Dim fso,htmlwrite 1 h, s! i$ w& H
Dim strTitle,strContent,strOut
! V1 z- ~; t$ d'// 创建文件系统对象 # Z5 a+ S/ V q/ Q9 l; k
Set fso=Server.CreateObject("Scripting.FileSystemObject") - {0 _1 h6 k; F" S6 s
'// 打开网页模板文件,读取模板内容
2 ?3 E3 \, r1 j- i. S9 ?. l7 cSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) " I. d4 m/ J3 X: Y2 t5 }
strOut=f.ReadAll 0 U/ w/ {0 Y# q6 [% u4 p
htmlwrite.close $ K( _& S9 W7 u
" K+ c8 B9 d: a6 e. l
strTitle="生成的网页标题" $ Y I; X+ I G A& ~6 L
strC
) [ ]; Z; |1 n$ [# n* S9 }. Y, r% b3 _$ ~9 O+ R2 u1 [" o5 n; t
'// 用真实内容替换模板中的标记 . o0 G7 f R. _/ j5 E1 m% Z
strOut=Replace(strOut,"$title$",strTitle)
6 i7 l4 Q4 ]; h7 ]$ ]1 cstrOut=Replace(strOut,"$body$",strContent)
1 b6 A# E& _! d8 Q# |/ X# o4 d/ P1 z* d( q- J" [5 T/ J. @
'// 创建要生成的静态页 & Y/ f4 Z2 w9 R% q" V' p
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
% V! P: B [7 X2 Y7 o- ~
/ N$ f0 L: x0 W* R'// 写入网页内容
; [9 _! U4 V8 A% U0 h. ohtmlwrite.WriteLine strOut ' Z# x1 S# f$ |6 G: c, R2 r' L
htmlwrite.close
7 U3 G+ m: J7 Q6 t2 f* C$ D$ A7 q! ^8 @9 i9 h+ b
Response.Write "生成静态页成功!"
, e; z. L5 F) Q: g! O! V/ F3 f- H8 `* m9 N: T3 t1 Q1 i u: _
'// 释放文件系统对象
9 H! ^ X3 |% Xset htmlwrite=Nothing ! z# @7 f2 {9 \7 h4 x8 F
set fso=Nothing
8 ^$ k1 ^! T5 Z6 p5 r%>
1 b9 Y) `- x- g1 P3 _( V3 O& Y$ \+ i) r) k$ r7 E# E; u; ]1 k
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. " u& t8 ~4 j' I5 }/ ?
<% + k+ A! U! W9 m @4 J3 R' N
) G9 Z% I6 d3 H6 q'常用函数 1 y! R3 ?' N2 S: D. ?% d( e9 X0 T
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
m8 s5 _+ a5 e! rfunction getHTTPPage(url) - G3 e$ A2 Z: Q4 t7 g: W
dim Http 8 Q$ J' v/ A2 E; U {4 r
set Http=server.createobject("MSXML2.XMLHTTP") 3 \2 a+ j7 R& M- ]
Http.open "GET",url,false
; `! |% ?0 [9 HHttp.send() 8 r9 {" y8 L2 A6 t' ^- L
if Http.readystate<>4 then
! I' v% k1 T9 i, Q5 Aexit function / L$ \9 B- Y l% ~2 A1 u! @1 d- |
end if 6 R l5 T- H* g I4 I
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") / m; }) k* t% J z( Z6 V+ I
set http=nothing + C1 k6 s6 }: _+ u4 D
if err.number<>0 then err.Clear
: t2 b( N1 N0 W& ?1 z" Hend function
) W/ m5 V6 c/ K( d7 s. |5 T( r* ~8 G3 j
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
3 i. @4 Y% W2 s/ m! M9 b2 HFunction BytesToBstr(body,Cset)
- D4 q [$ h3 t/ S+ s, r$ n* d9 J6 Hdim objstream
. Y+ G7 k Q" rset objstream = Server.CreateObject("adodb.stream")
- D( x' u$ g/ b; O9 p% ?' g; D: iobjstream.Type = 1
/ g& t! |" ?5 l5 Lobjstream.Mode =3
$ e# C' f7 Q" j* mobjstream.Open
: V- i) v; n* o7 {0 b$ tobjstream.Write body
8 k0 y0 K9 \% O6 E% G/ kobjstream.Position = 0
6 I) u( x( j4 Q1 @4 W" }objstream.Type = 2
9 O& H# [) O' P% P0 @2 W! r# @; yobjstream.Charset = Cset ! [& C2 X) R* r5 Y. H5 _! p. _ @
BytesToBstr = objstream.ReadText
2 J; j0 f3 M$ `) U' O6 M. `* ~4 hobjstream.Close
- q R; s G- y4 ?" S* Zset objstream = nothing
! Q, @$ h# {* Q& a& L9 f8 x0 C, b' R1 UEnd Function 4 C6 g0 C5 r: h. Z) l0 o2 b
& W5 n# l$ J* N5 j8 C* M5 d( f/ }' I; ]
txtURL=server.MapPath("../index.asp") ; C5 H X. w( L: v( e9 _
8 w! w* B% m+ _
sText = getHTTPPage(txtURL)
4 W( [1 b* D( V! T- y, O) i6 `# V9 c
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
/ Q7 N' W" b* K+ q. a5 cfilename="../index.htm"
& J5 R7 N, p" xSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
% J1 u+ @# d4 H$ Y# GopenFile.writeline(sText)
- F" E/ ~3 ?$ l$ q, I" j3 A6 a$ qSet OpenFile=nothing - d2 L- c' \- u0 ]0 f
$ i- r# V* U5 o
%> 0 y) `7 p& }' D9 B3 K; g; {
<script> 6 O/ E, N @/ {% e3 r
alert("静态网页生成完毕");
( Q' E: [( t: H0 }history.back();
( C# @, u9 V3 Q( l6 e</script> |
|