|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14369
- 金币
- 2470
- 威望
- 1647
- 贡献
- 1418
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ; k# S6 [% ]& ]% H( D' H
像www.aspid.cn的主站就采用了TSYS生成html文件!
: j W4 Q& j5 x8 J/ \9 Q所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 0 I) I2 I- t' W- z t; z6 r7 E
7 e }! A: q4 ^. c/ |% J$ G$ @
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
0 d! y$ b) J# m0 g! Y6 pfilename="test.htm"
% |, d* k( o9 cif request("body")<>"" then 3 O$ Z: m) \7 L; S2 U& b, M' a
set fso = Server.CreateObject("Scripting.FileSystemObject")
+ l$ x. d7 T1 Iset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 2 e3 T' L" `( H- p4 ~) u
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 8 R: I4 j7 ^) |* ^0 w: F9 v
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
) L+ X1 e1 ?1 A! ghtmlwrite.close . R z& P; R% j2 R; C1 D$ N$ n4 t
set fout=nothing % K; g& V: d, l0 w H8 b' W
set fso=nothing " u6 e$ Q& A7 l; V$ c1 f
end if
; J" @7 g4 D9 y1 Z$ k%> % U1 Q. V) r# ]
<form name="form" method="post" action=""> * f8 T* O1 X- v7 g. E
<input name="title" value="Title" size=26> , e; } _& o* M+ i* Z: {7 ]
<br> ) _5 l7 g0 v/ C# U+ w
<textarea name="body">Body</textarea> 7 a1 }( v2 w8 B
<br>
! X( z) j: s) C) i5 O4 b& V<br>
# l6 Q: T1 c% i<input type="submit" name="Submit" value="生成html">
1 r+ C$ Q d9 k8 Z5 }4 D; {2 y</form>
5 C4 w4 h5 @- o# \: m# y2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ) O$ q& h7 A6 l$ B& I8 g
template.htm ' //模板文件 <html> 5 l+ G( B& e4 Q. x$ o- h
<head> ; {8 W( F- b1 [; Z. {% _$ ?/ g% ]
<title>$title$ by aspid.cn</title>
' C: ?5 u& I, R2 F0 s2 N</head> + L$ g M/ t8 L7 N$ c/ `0 B$ c. ^
<body>
1 e" b6 O9 f! l: B, Z, _$body$
4 S+ `' W2 {. n0 P: A</body>
4 c4 `8 O1 {$ `' l+ |7 {</html> ? T1 u4 c& f% t" e/ F9 I% x
- E7 D. r+ x1 [& T8 \TestTemplate.asp '// 生成Html <%
6 N N4 H) r: ?2 KDim fso,htmlwrite
( _: N5 T2 h/ y) d& ZDim strTitle,strContent,strOut
* D; \0 V1 p2 c& [. t0 f'// 创建文件系统对象 ' Q% W' U" f7 M# s' G- b
Set fso=Server.CreateObject("Scripting.FileSystemObject") * u8 K& U+ B% g8 q a( E
'// 打开网页模板文件,读取模板内容 9 b* d1 ]- f/ C( j; c2 k" p* s% U
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) % C- }# K6 k1 T' H H
strOut=f.ReadAll
: }9 o! |: g+ t5 r8 ihtmlwrite.close
: C* C$ h3 I( [$ S1 ^: S5 s* E8 P+ ?! o8 h, M: Q
strTitle="生成的网页标题" w ~7 [- z1 Q0 Y8 @
strC , O! K" Z# V+ H& M/ k
+ P" W" {3 _7 ?/ l0 N7 I, _
'// 用真实内容替换模板中的标记 1 W* I* a8 b: s$ W4 w
strOut=Replace(strOut,"$title$",strTitle)
. J0 ?- ]9 z6 }; |2 IstrOut=Replace(strOut,"$body$",strContent) . I8 X: \0 }3 i+ i6 C4 q- T
1 e5 a& o4 T: T2 E& f
'// 创建要生成的静态页 : R4 U' w$ t8 M; g
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) / J* u o8 h* c
5 I( Z( P, T; n0 O6 c
'// 写入网页内容 z" j" G. K V# F
htmlwrite.WriteLine strOut
* {5 f5 t) h$ c! B) B0 ]2 ]; R) n+ ghtmlwrite.close
: X+ ?6 K$ h2 r4 t5 [$ o. u8 q6 g( h' Q9 C, _- [* |/ N1 }
Response.Write "生成静态页成功!"
9 Z# m) x- Z! Y3 {1 g0 U n6 k$ K6 w9 ]2 w
'// 释放文件系统对象 1 y; q8 g+ o( P7 R% r8 p* |
set htmlwrite=Nothing 0 m3 {' K; ?7 v+ l7 v
set fso=Nothing 7 f) Y7 k- I5 `$ q6 \
%>
2 V/ @' Q/ l, {: D1 z3 `" w4 Q: M2 H' O6 a& C. z8 x! a
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. - Y$ }, t7 n0 n# M5 ~
<% ' R* ~: G6 y2 t% I9 O: t
8 x% |/ B3 T* K; c
'常用函数 5 L8 J: f4 E% r" J7 H
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
' h( D0 h9 Q' v( H: f9 Dfunction getHTTPPage(url)
1 n& r! C3 E" [2 S# } H7 D; _: p$ P* Bdim Http 9 R" [. ~# t) t
set Http=server.createobject("MSXML2.XMLHTTP")
+ A" N. H [7 Q! P: ?; OHttp.open "GET",url,false % o: q3 b1 p' L) a- P, f& z
Http.send()
1 y+ r' ^. r& ~& kif Http.readystate<>4 then
+ A, g/ Q* E I1 Texit function ; J: W# \) j5 k
end if
# ^) Z* f5 |0 B3 p# J4 YgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
" L& O( L, I+ Q" j* [set http=nothing 0 z7 {5 _: q5 F' f) E; v
if err.number<>0 then err.Clear
4 u: z' m9 a% _8 r" w+ _end function 5 {. C1 U# W6 V6 R3 m9 \6 _, e
7 ?8 A: F0 v! x. p- O* k'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 0 d( Y! U) o9 }. r
Function BytesToBstr(body,Cset) ( ^7 R, y1 |) v
dim objstream
( L6 ?7 P v$ ?, f! |2 `3 z2 [" ?set objstream = Server.CreateObject("adodb.stream") & e+ }; O8 x* f3 D+ E l
objstream.Type = 1
0 t. [% h R% w: N6 ~2 v7 Q) I( Pobjstream.Mode =3 0 M. l* [" w: n" \
objstream.Open
8 C: P) Q# D, [; F/ Dobjstream.Write body
1 q# j, a1 ^: S4 s4 c3 y0 f0 l& iobjstream.Position = 0
: O! r/ D& v+ L7 }objstream.Type = 2
5 q: ~& G, l& I6 xobjstream.Charset = Cset % r& |% N! g' _* r4 A8 _0 ?9 O" {
BytesToBstr = objstream.ReadText q; Q4 ?; F) [ Y3 c% @
objstream.Close & |7 h8 Z( _- ?
set objstream = nothing
6 P0 a& _5 ~8 }! U4 uEnd Function }$ m, N( u3 N- D9 D' @
7 b2 o6 @* y3 O2 b K/ R8 b y% a, m7 Z7 k. Z- a# c
txtURL=server.MapPath("../index.asp")
& U: O: I9 u0 L9 l5 i
9 R; o; v8 |4 e+ r6 _sText = getHTTPPage(txtURL)
+ W/ W. e1 a* q8 b2 R5 \
5 k, E" E0 o4 X P9 [* G0 n, w( ISet FileObject=Server.CreateObject("Scripting.FileSystemObject") 7 k; E4 V5 y% N v0 t- u
filename="../index.htm" ' Z/ A6 I/ j# _$ @8 ~1 Y
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 " q- i0 @" n; F/ I% m7 h
openFile.writeline(sText) b: |* }* |4 D1 m
Set OpenFile=nothing * ]$ V; E) s Q4 k) c
* b7 Y) |, K* F! W& T! R%>
0 c5 w6 h/ U) z+ i. ^<script>
+ n9 l0 q! V5 E5 E/ Salert("静态网页生成完毕");
- \3 ]% D& y" q7 o* Thistory.back(); & b$ I; B* `% y2 |. I2 B7 M& F
</script> |
|