  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14249
- 金币
- 2415
- 威望
- 1647
- 贡献
- 1363
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
- _" v7 \" B$ u3 L" f2 s像www.aspid.cn的主站就采用了TSYS生成html文件!
! u$ S. j4 w. K/ B" }所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. # X+ @# E3 W: \1 h+ E* R9 j+ I2 H- Q
: c) k4 g, t5 o3 w7 l1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
2 f% R/ B1 W6 f4 _filename="test.htm"
" @( F0 Q! v: l7 Eif request("body")<>"" then 7 H" \3 {' z B3 S% `
set fso = Server.CreateObject("Scripting.FileSystemObject")
$ K2 v! ?# |" S6 F3 v2 lset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 3 Y* [; |+ p2 f* h
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
) x# J2 L9 K5 }0 ]! |2 m" ^6 M9 whtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
3 A9 e( F: i* N. d6 P' b, a6 ahtmlwrite.close * \$ [2 ^/ v6 ~ T
set fout=nothing
+ F3 W( P1 U& g2 q, C& E- o) iset fso=nothing
% V- E) a' Q# b. Q1 }8 l+ Iend if 8 \$ _7 p' [- B: z# Q
%>
1 b f7 K/ l' y8 Q# ?3 I; _<form name="form" method="post" action="">
3 J# q& h4 a& w: k+ P0 n<input name="title" value="Title" size=26> + ?; L& u0 M: T
<br>
2 W/ I8 G% w" Y: T" a0 G4 b<textarea name="body">Body</textarea>
* B9 V& _+ g+ ]# j+ G+ E<br> ; v+ o6 f/ w) h/ f% H* p. V
<br> . e$ ?3 U, @ B$ r0 h3 X
<input type="submit" name="Submit" value="生成html"> ' D% h# Z' ~* r8 m+ \# H
</form> ; i0 M- V' {) A6 s
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. + |4 s6 w0 F( a; |8 f6 ~
template.htm ' //模板文件 <html>
* r; a. V, N5 i& ^6 {<head>
. t! a; w' [' Y- t$ Z) R<title>$title$ by aspid.cn</title> . k1 n$ u& @$ s# T# r
</head> ; j& Z% E9 _/ _6 P* J# ^
<body>
9 M6 {2 `+ Q3 t& s$body$
) U. z; U" E; e4 j) A) y- k</body>
3 u& b" @; w- v</html> ?
- a' J% \0 H- B* V0 N
: F% d# }1 q) j( mTestTemplate.asp '// 生成Html <%
, Y& l% ^: R( t5 r3 yDim fso,htmlwrite
/ W- M5 B$ I- b! R$ l. t' b3 IDim strTitle,strContent,strOut , l) }6 g( d6 m# D4 ?: S$ \& J) @
'// 创建文件系统对象
0 u( M% I" [( d: fSet fso=Server.CreateObject("Scripting.FileSystemObject") # ]# G- D6 q C! c# |; m
'// 打开网页模板文件,读取模板内容 u% R2 o- ]; v3 c. q+ @: c
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 2 T5 l9 l: H' U
strOut=f.ReadAll , R% X" n6 ?/ {7 ?! r- i/ _
htmlwrite.close
/ @( ~! t- u. w+ Z( y+ x0 h5 h( a3 s. R) ]; r5 B
strTitle="生成的网页标题"
" t& Q5 X5 {# |7 t( |* xstrC ' C7 b% z% @* i8 \4 {5 _8 V6 @
3 h5 n q4 g H( R# C8 C( r% L* y* Q
'// 用真实内容替换模板中的标记 N* n1 B" w2 `% c7 h" e7 w
strOut=Replace(strOut,"$title$",strTitle)
9 d0 q0 e( W, H2 J( Z7 kstrOut=Replace(strOut,"$body$",strContent)
% }1 m8 R! d: S$ J( A
/ a# g0 u7 B9 e* F2 @! j, h; Q'// 创建要生成的静态页 ) F. \( r- Q% Y/ ?/ m& |$ g8 |7 H1 N( U/ V
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) : A4 ^" _$ O: e$ P, F, \1 {
- z( G$ E9 U, X' B4 S5 }4 \'// 写入网页内容 ( A: T* W. j( O ?4 b/ a
htmlwrite.WriteLine strOut - Z- P( U8 X |. ]* d; ?" f
htmlwrite.close P4 @$ m+ ], L, b- F' [( [+ G
- w+ v Z* ~. `3 OResponse.Write "生成静态页成功!"
7 G+ d6 i& C/ q! G
. \5 x2 D/ i, T2 ]'// 释放文件系统对象
$ l+ y) o$ p8 c! fset htmlwrite=Nothing 4 x) D1 N& R6 J5 v, h5 ?* N2 D/ m
set fso=Nothing w1 a. q2 J7 _3 h( K
%> * I7 {4 U2 v: o# Y2 D
9 ^; C' `/ n0 G: A2 ~& i0 U
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
0 S6 [8 o! q" g<%
, s& _* k, B( U+ j1 L6 w" `! P2 q4 ^
'常用函数
* `2 g; [1 O8 V$ J W'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
, F) O$ Y% o" A) c' g, U5 `& [function getHTTPPage(url)
# X( K4 I' f( k0 d" Qdim Http
3 [4 f* z: q( V% y" lset Http=server.createobject("MSXML2.XMLHTTP") 2 u, I- R3 m4 p/ N' ~1 K2 S
Http.open "GET",url,false
5 s# S& ]8 ~' w, pHttp.send()
8 l# h/ Y6 K4 _3 b! F% P- rif Http.readystate<>4 then
) T' T- X$ [; c0 W/ `8 Xexit function
( U( @' g! h+ g- f; @9 I' ?( Kend if ' f* t v. x# B0 V8 m$ j& P/ `
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
/ Q, K8 H# h5 d# y( b: ^set http=nothing
" `( o, [4 e& ~ B6 nif err.number<>0 then err.Clear
+ n/ \5 O0 ^2 c" y; E) h) pend function ' b9 S8 s/ p( F4 B) x# y- B+ A
, T9 x6 ^' Y ]: U8 E* j0 l'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
) K K" Q, M/ }7 Z8 qFunction BytesToBstr(body,Cset) ; n3 }0 n' E6 d# o) T% e
dim objstream % c( @) h/ O* O1 x
set objstream = Server.CreateObject("adodb.stream")
5 E5 v I% g3 M" [9 |% G9 d& zobjstream.Type = 1 3 P, H; Q* T8 u- a" b4 z
objstream.Mode =3 , V- r/ t; @6 m' S" \
objstream.Open
) e, w$ e( Y' x% Nobjstream.Write body
: I" B- `3 L& s, |1 cobjstream.Position = 0 6 M h$ ~' q. C3 a
objstream.Type = 2
. o0 V/ u: K* p# q h' a) \objstream.Charset = Cset
' c6 v2 Z. |, _" HBytesToBstr = objstream.ReadText
: V4 Z2 x( z& u' X' W/ c Eobjstream.Close
/ C4 T* |7 C: b' o' x2 wset objstream = nothing
* g: R& ?1 c. f7 P6 V8 [3 bEnd Function
5 D4 ]- h! g+ w" R1 `. m
) l1 }% r# p. b: V( W+ y; [; J' p, ~8 @- f6 \/ i8 ^- X
txtURL=server.MapPath("../index.asp")
3 o G' w& ^: i# ?% C+ l" g
* F3 ~ V3 @2 J% z# ~sText = getHTTPPage(txtURL) 1 o; d8 o* Q2 g2 N9 x0 T
& d/ S4 s2 e3 n7 T# D6 tSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
& r9 e$ S9 n$ c/ Q& Sfilename="../index.htm"
" Y7 }6 N% y1 C v; g9 p$ bSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 2 d( ^& v( r# c* L! D( T
openFile.writeline(sText) / _9 Q F1 z' @' j; Q) |) F6 B
Set OpenFile=nothing
4 I' V* S/ z3 Y* A7 k0 m: X# J* S3 M# g. J4 _6 h
%>
" G* R* v; w7 i! ?<script> " o# |0 ?( v% o+ j" B$ Z) F: l
alert("静态网页生成完毕"); ! A7 o3 U% O) m2 y9 M) u2 d
history.back();
- ^$ E' g6 ^+ i- p. D, z</script> |
|