|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14443
- 金币
- 2507
- 威望
- 1647
- 贡献
- 1455
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 3 j5 U* u# b6 G7 ^) D$ x* d) N+ R: ]
像www.aspid.cn的主站就采用了TSYS生成html文件! 1 ?$ N9 E& Q( r( E8 A" `* T
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. . }$ D; U2 m1 v' J" J5 v4 d2 h+ R
) p/ t% C; {/ E+ m2 |1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% , ^ w# ^' L7 c1 j. \, H
filename="test.htm" 9 k- X0 \0 O9 y' A/ G; p
if request("body")<>"" then 7 ]' B+ V+ W4 d
set fso = Server.CreateObject("Scripting.FileSystemObject") 2 G8 ?/ ~0 T' ? S+ W3 T
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ; `7 @" |* |; b2 x* z
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
( H# e6 y3 A. S3 uhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ' P( y$ p, ?/ c7 V
htmlwrite.close : y% T1 l; a/ z8 U
set fout=nothing
* d7 d$ U& E# } k' K+ [, Mset fso=nothing
& Q; ]2 e$ X# r0 `7 r- _end if
8 }2 ~- E1 h7 M! V%>
* E. P! n2 U; d<form name="form" method="post" action="">
' g( e+ W( h# d' ?2 l<input name="title" value="Title" size=26> * @: u# K8 r( G6 i$ L4 n
<br>
6 ~0 Q1 \2 _; t" W, \* U1 M<textarea name="body">Body</textarea> - G: a* p8 [/ u: y( A
<br>
9 a8 k* C3 s `9 b$ {1 B2 K/ X$ v<br>
: S+ T4 ]' g. ^( j<input type="submit" name="Submit" value="生成html"> / d- n- `% K2 u0 K4 P+ d5 f/ N/ b; t
</form> + Q( o. c7 ]+ ] F/ K* x6 a( E
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
2 k6 |7 r4 P* H. ?, T3 N2 H J+ Stemplate.htm ' //模板文件 <html> ! }8 m0 X! r1 C# v$ L7 W
<head> 9 A+ D4 \- B6 j2 Y
<title>$title$ by aspid.cn</title> 9 C# w9 n5 w2 Y& |3 B- h
</head>
3 Q8 p& g! c- i8 e' J! }4 \# R<body>
* J+ f+ [6 j) e0 I$body$ ( k$ z1 ?' ?9 m; a: i
</body>
/ Z; v/ A/ [! {# g6 K& X' V</html> ? # c4 X8 p0 ~/ v; y2 \
. G7 |; G8 |) } m' n0 Q
TestTemplate.asp '// 生成Html <%
/ i* I0 V! v6 C9 z1 Y( |' SDim fso,htmlwrite
% c. U. h; c- z0 J8 E- Q: D! HDim strTitle,strContent,strOut 2 T9 _; [/ Q( h# B; j, e3 u
'// 创建文件系统对象
1 R& O. j6 |$ @& i, j& ^Set fso=Server.CreateObject("Scripting.FileSystemObject")
6 l3 O3 L+ A9 o! F O6 I'// 打开网页模板文件,读取模板内容
% z4 i9 t5 d! m lSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
6 I# h- m$ {" U, l1 S( FstrOut=f.ReadAll ' P2 G* X1 Y; y% _7 \1 F j8 Y$ d
htmlwrite.close
+ ^. N ^2 K$ w. g+ u1 v7 A0 b: c W! c( M5 h
strTitle="生成的网页标题"
: s! G: n/ W8 H7 A* A" X+ `9 XstrC 7 V! s+ ?; n0 O5 {( ^& X2 Z
/ ~8 L* |" z3 L) n" d0 v/ _'// 用真实内容替换模板中的标记
/ _% O7 Y7 t; {6 x& S7 dstrOut=Replace(strOut,"$title$",strTitle)
6 [. X) V( W$ M; V" y& QstrOut=Replace(strOut,"$body$",strContent)
@& o S8 ^9 C8 L5 a; A5 c# }: b& J, G9 c# R) P
'// 创建要生成的静态页 8 o# m2 M% P$ ]( r
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
; F' z; @2 R6 ^: F; P& S* K% O5 X) L6 [) D- x) k
'// 写入网页内容 8 @% g) ~0 ?6 W
htmlwrite.WriteLine strOut , D( w) e" M) X* C: [5 K3 L
htmlwrite.close
/ g. B" H2 w% B- ?1 w' G9 f: b/ R. G* g4 w
Response.Write "生成静态页成功!" 4 ?# s# ~% v8 a- o' B/ v
0 e7 E9 D% d$ C( A3 e: f, Q
'// 释放文件系统对象 8 }, a8 G: U" h( r
set htmlwrite=Nothing
; [' l/ {: L' _, v# T, N' k* Gset fso=Nothing
1 J* q! k# o; w& T; T" T+ ~; V/ @%> , P6 I D* s+ W0 P. a
2 d; X# a+ n2 i' {0 s8 }3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
( {3 d$ |8 |/ n! u% a B6 l) D<% 8 \3 X% F- g7 a
4 {; O6 {. N9 d4 f: M3 h4 h% L1 F, k'常用函数 $ S1 m* R" I# {# g2 ]
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
! F1 j+ I% e \- d+ G |function getHTTPPage(url)
" P2 ?) e8 l. Y; bdim Http
1 _7 t" b; Z9 ?1 k( \/ jset Http=server.createobject("MSXML2.XMLHTTP")
; G6 I: h5 a. d( P0 H2 [. a; `Http.open "GET",url,false
% W7 ]2 e# S' Y$ W2 ]# ^6 a ]! dHttp.send()
% S! B7 |- E0 Q0 _- p/ }7 Vif Http.readystate<>4 then
) J/ w% ^$ o. }% @8 `$ c- Mexit function
+ n* S1 f* W5 y1 f7 y) wend if
4 A6 Z5 W, \9 W6 S0 F) C+ jgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
( [% c" b" i# ]+ sset http=nothing ; ]! `+ v) r1 H& O3 A
if err.number<>0 then err.Clear
- w0 N4 d; J3 z+ gend function
% k& z# b( G7 e7 o1 }& C. P9 k& V( T* _( q; C7 d
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 ( Q5 g. i5 Q7 d$ z1 t
Function BytesToBstr(body,Cset) 4 x- R1 a/ C$ w: p9 A9 x' o7 M
dim objstream 2 ?9 W/ I7 G) w
set objstream = Server.CreateObject("adodb.stream")
! p# A5 ~6 c$ p, T$ n& N& M# Hobjstream.Type = 1 7 t) Z( X! o( K o6 O5 O
objstream.Mode =3 * s* p/ V* J$ r' o1 [/ {1 L$ X
objstream.Open
! L7 a% Z: w( x) V, r* tobjstream.Write body
4 d% ]' ~3 T) P" n" U% u# {4 D/ aobjstream.Position = 0 7 N4 Y5 f, }6 j* A, S! U/ \) f
objstream.Type = 2
: q3 X8 F- P7 c, |" R gobjstream.Charset = Cset
. X. o: q, W# tBytesToBstr = objstream.ReadText
/ Z* |; [4 S2 C" _objstream.Close ( `+ U0 B* Z9 `% A( _
set objstream = nothing 3 I$ W' ^( L# P# Z7 M1 C
End Function
' s+ Y( r- w% s
; ^2 ]: s8 D* d- A
% [) |, q, m5 O9 X* T* VtxtURL=server.MapPath("../index.asp") : g% U% E$ l; g: U; ]' W7 W/ E
. S2 n! Z) X/ L! r# P) qsText = getHTTPPage(txtURL)
. t R& n! }! l ? y+ @7 q7 I
4 W; K+ x3 w, ~$ c- D1 d/ pSet FileObject=Server.CreateObject("Scripting.FileSystemObject") 9 c$ }, g# |- {- m' k, {, i; m
filename="../index.htm"
( j$ p b! d# O* f+ l, ?Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
3 Q1 O+ i! @" @, ~openFile.writeline(sText) ! R9 P. D0 Y$ {: M/ _# v& M, t
Set OpenFile=nothing
- E9 J7 Q. N+ X, e% G2 D! v/ _" T7 K$ b( N7 Y
%>
& W8 f( g, U/ |. K# _$ b<script>
* g2 I4 [* c2 U. H, Y' Q1 I& Ialert("静态网页生成完毕"); ; l8 K9 C8 x1 w" K4 C
history.back();
4 s- L4 p0 ?, i# v% F</script> |
|