|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14329
- 金币
- 2450
- 威望
- 1647
- 贡献
- 1398
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
* w) ^! l3 s* N像www.aspid.cn的主站就采用了TSYS生成html文件!
5 J. I e# o2 z. K所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
+ I8 s* z/ q+ n& L9 @7 o1 }0 m& m: l# y# ]) P3 z2 `5 R, k
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
( Z1 {( k4 V3 f" U3 V$ ~' Pfilename="test.htm" ' \& B* X% {! F% ^. p2 j
if request("body")<>"" then : S- L+ b0 s7 ]/ ~4 T7 _/ u+ ?) @0 }9 V
set fso = Server.CreateObject("Scripting.FileSystemObject")
8 f2 ^9 l" Y t; q9 rset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
3 K) W0 p4 t& R7 S rhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
; K( L6 h& x2 P" ?: z) qhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 2 R) z6 Q) g9 M7 f& T
htmlwrite.close 8 v, s2 G& ?$ v
set fout=nothing ( h5 E' Z: X4 E# U, B
set fso=nothing ( J) S4 C" Z0 r! L9 d$ `
end if
- T, h. T% s3 M7 c: n9 S. C" P) p1 X%> a6 [% x0 D; j: X* [
<form name="form" method="post" action="">
( R& P- e( I9 ~# H, r<input name="title" value="Title" size=26> 9 W* d: s. T1 E2 V4 J+ v9 H
<br> 3 n: V# Y6 M& w: Z
<textarea name="body">Body</textarea> 7 W; a5 f/ [; O
<br>
: E8 y$ S* C3 h* s0 T<br> 6 Y: s' w3 e9 \8 P% K. Z: F
<input type="submit" name="Submit" value="生成html"> $ q% d1 J" C* V
</form> 8 `/ q7 w2 W% @4 q6 X
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. / P6 b: e3 K* C2 B
template.htm ' //模板文件 <html> 7 c4 v3 Y" ^5 `- s- K: r; t( G
<head>
+ s1 ~/ Y( |* K P( M& ?- b<title>$title$ by aspid.cn</title> 8 Y6 h. l- r1 \* N. o
</head> # h& J3 E2 K6 N. d5 O* ~
<body>
3 P! g6 a. x% w+ D' t$body$ 6 Y- s- P5 \4 Q- M% V% L% f) j3 k
</body>
6 S9 t! \: O2 \2 c8 N; S& ^% n</html> ?
' F1 S! u$ j8 D- g: s" f% V. ~; _4 y+ M5 Z. x3 c# V* }0 G
TestTemplate.asp '// 生成Html <% 1 Y1 T: F% H8 b3 m$ v3 B
Dim fso,htmlwrite ' C, v n- O2 m: P0 Y. F
Dim strTitle,strContent,strOut
: L9 _$ V5 T, `; _3 W3 a'// 创建文件系统对象 3 S) }( R! s+ M5 {
Set fso=Server.CreateObject("Scripting.FileSystemObject")
3 }9 x' X& y* g! z6 C9 F& m'// 打开网页模板文件,读取模板内容 5 K5 H' T2 a0 `$ |. }. q1 r
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) , V2 K# m; s0 W2 i7 w9 L. i0 s5 X5 E1 f
strOut=f.ReadAll ) H ^( r' M+ U* w7 Y
htmlwrite.close
! v" F) n8 J q8 h( a3 w& ?2 m }' {% k2 ~1 R/ Z
strTitle="生成的网页标题"
& p$ Z) _5 Y5 m4 E! P" s" rstrC
8 T' l) Q9 {% Y# I- k* J
; S. p: w& S1 |! O'// 用真实内容替换模板中的标记
0 Q; E' ~" _4 L9 R4 ~, ?strOut=Replace(strOut,"$title$",strTitle) 1 X" S9 Y# ]7 @. \) Q
strOut=Replace(strOut,"$body$",strContent) # ~3 v' t) k4 W; f" A3 W$ C
: Q- m8 J9 c$ ~
'// 创建要生成的静态页 2 A& q' R2 C B' F7 @
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
# W: {1 q9 p' N' ?( f5 X7 _ S& q: N! x( A" |! k6 `( K
'// 写入网页内容
4 t1 _4 o; K8 I0 Khtmlwrite.WriteLine strOut 7 p8 }$ K" o' U
htmlwrite.close # E% v5 u% i% k. K0 y7 m: q) F( g
# Q: E. C* _0 `7 [) }Response.Write "生成静态页成功!" + j2 C" P9 n; v+ i
' z4 Z3 q4 H/ I'// 释放文件系统对象
3 n1 x( I2 }( r* K( Y0 S* h1 N* s) [set htmlwrite=Nothing 1 y# x n; F2 k/ y+ ?: j' U( w
set fso=Nothing . |+ \, O7 e/ n# ^: ^
%> - }4 v6 v1 M/ i! R. G
! @8 F4 F ^8 D5 ]3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
6 B }2 N- a- s% e: X8 a<% ) Q8 M3 e r9 o" h$ X: c
) F" f4 D8 x- C8 ]# F$ o( d'常用函数
; ]& b3 j( Y5 r% l" E+ E'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 1 x- [% i% p5 ~9 o- s5 Q
function getHTTPPage(url)
; W4 ~) L9 d% p* ?# G& x, l( fdim Http
+ K& w" D1 ]7 g9 Dset Http=server.createobject("MSXML2.XMLHTTP")
7 h0 u! F' f$ p1 SHttp.open "GET",url,false $ K7 g" c9 M5 n, J+ E9 F
Http.send()
+ f# g' c& }, o% m" v: l7 oif Http.readystate<>4 then 3 H! r, c, c P% `" b( n
exit function 4 A c8 ]5 }: ^5 w1 e
end if 5 P& N0 F: b/ I3 a1 u, q
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") % v! |+ w, q+ C
set http=nothing
7 C7 W7 E2 C' L; h4 Z Qif err.number<>0 then err.Clear
. S. S# t. w6 q5 S- g( B& Jend function
1 w1 N( g) m3 k% i5 V c
3 |2 o. m; g) i/ I'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
% N- o1 r; N4 ^9 ]' iFunction BytesToBstr(body,Cset) 6 X5 f' l% K, D- {- S" E
dim objstream
v% V% R5 Z8 o3 F1 ~set objstream = Server.CreateObject("adodb.stream") 4 h, W+ {& y& E1 t8 S& P
objstream.Type = 1 ' D) v5 r; P7 S; V+ `: f: G
objstream.Mode =3
" g }$ q& S( C5 u7 B; b( I: C2 Fobjstream.Open ) f5 Q* X! W: ]
objstream.Write body
% j5 u: v/ U, H# F+ Pobjstream.Position = 0
; v& e' C+ w: @8 Kobjstream.Type = 2 1 N* e' r3 L2 [# V
objstream.Charset = Cset
& f1 s/ L$ @- I! ^, T$ kBytesToBstr = objstream.ReadText 3 t+ a% B& D! N" c g% \) w
objstream.Close 1 v. ?7 f7 O0 i
set objstream = nothing
+ U1 P# R8 t9 u5 K" x9 tEnd Function 9 {4 v0 k6 T `4 b5 c
7 E' P0 K' A) _! J7 s1 z
1 p7 J0 I5 _$ e! D: W) o* OtxtURL=server.MapPath("../index.asp")
4 s+ t) m4 V, f) j% V; S+ d2 u5 m6 P
sText = getHTTPPage(txtURL) $ b1 P9 b2 L( s' W$ A
P0 |9 M9 L# _, `: V: @5 y3 y0 wSet FileObject=Server.CreateObject("Scripting.FileSystemObject") : L8 Q+ ]0 o7 I4 n3 `
filename="../index.htm"
9 N5 [. B3 \: f* i* @0 K. `8 J4 MSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
( S8 X) l' v# {$ ~openFile.writeline(sText)
5 j5 }5 `, E# PSet OpenFile=nothing
- u2 x7 I9 K; h# D8 X. f; Y: y5 p/ \( ~+ R2 E' M: B4 A. [
%>
. M$ h! r- Y3 ~<script>
; g/ `* T# i- |! f+ P7 ~alert("静态网页生成完毕"); , ?7 `6 ^, y- u! h2 w
history.back();
! \4 \% D# k. F</script> |
|