返回列表 发帖

ASP生成静态Html文件技术汇总

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
# h- i# t& l. |& A( F7 }www.aspid.cn的主站就采用了TSYS生成html文件!
! @7 j3 T* l3 A; J; R8 \所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 1 z. v! |" j; B

* j- }' R' B& Z$ N' Z: k# \1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 0 \0 p& |! `1 E$ K) ~
filename="test.htm"
% O, F% w7 Y$ Pif request("body")<>"" then
4 W1 r: N; R9 C* j7 x9 h: ]- F4 O9 mset fso = Server.CreateObject("Scripting.FileSystemObject") ; O* W& }  K5 M, S" B) K3 P# u
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
$ O# f' W, Q7 dhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 4 x  x# b8 d0 Y' d* }% d. A
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
7 N: u" L. t9 v0 e% c8 n! [: t% g/ Fhtmlwrite.close
: C, }. |- G$ p1 J/ Hset fout=nothing 2 A' }7 A6 ~2 q& R
set fso=nothing
) s7 t2 X- u; @" G: qend if
6 ^& f6 q: d" F%>
- ^8 _2 ^" |4 o8 t<form name="form" method="post" action="">
; D; v  O+ E5 D! _5 w% {<input name="title" value="Title" size=26>
  J6 d, D0 g$ c- l! O+ y' _<br> ( e) d( J* T2 I6 K$ q1 k
<textarea name="body">Body</textarea> : y1 |2 k) g0 k6 g7 E
<br> # ], {3 c. l( s; d5 H" Y
<br> ) n# r8 I& j) f, b0 h& ?, w0 F2 h
<input type="submit" name="Submit" value="生成html"> , k/ s: Z2 w0 Q4 _* }; C
</form>
. F8 i. G. N) ?1 S0 L2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ) N' ]# P! [; s. c; L( ~
template.htm ' //模板文件 <html>
8 B$ c/ H8 M9 T* i4 o<head> 9 X' n& c9 [9 P1 }7 s% U4 j
<title>$title$ by aspid.cn</title> * p/ I0 D% d4 F0 f; x  Y& v* t
</head>
0 g" K) ]+ R5 S4 i" g7 A' p+ n<body>
9 j- C9 F$ C) K4 r- t4 a' R$body$
8 `2 ~, N0 L5 B- f9 B3 {' y</body> & \: B/ r# h% P0 }! @9 }
</html> ? 4 _9 r& @# Q1 R+ q! b8 ~
' T% F/ `- l& ?
TestTemplate.asp '// 生成Html <% 2 G# j7 r6 m, V9 \: H
Dim fso,htmlwrite 6 R# F: ~1 l6 w9 \$ W/ M
Dim strTitle,strContent,strOut
, k/ r  S; O( y'// 创建文件系统对象
- k- k' ]8 Q$ }+ BSet fso=Server.CreateObject("Scripting.FileSystemObject") , l% ^  s2 g% x8 l9 r4 m8 P" W/ }6 n
'// 打开网页模板文件,读取模板内容
9 X9 |9 g" `( a( N2 c4 USet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) / Y* Z. h5 V, W* g/ F3 J2 i* A
strOut=f.ReadAll . M9 l( e% Q# ^$ X1 ~0 _
htmlwrite.close
+ }3 D. d2 ~. N1 n  G
8 e% g4 p  j+ ^# O( Z! d+ fstrTitle="生成的网页标题" 1 B) c( O* W8 N4 x5 R
strC 8 o; g$ k5 N' u3 I" t3 a6 x7 p

& C2 V" G# P: D2 E'// 用真实内容替换模板中的标记
2 X7 X6 ?2 q9 p* a4 lstrOut=Replace(strOut,"$title$",strTitle)
) e& j' D6 c" E7 UstrOut=Replace(strOut,"$body$",strContent)
' f0 k6 c; M! J* K" E9 Q1 d  a2 `
'// 创建要生成的静态页
& r# h& p2 U; bSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
9 d; O9 T9 ^0 r6 T5 }0 ^3 ^% e& V; z7 M' c
'// 写入网页内容
4 y  T8 P  b: A, y% ~8 |htmlwrite.WriteLine strOut
; V# }# ]1 F( T5 F$ Fhtmlwrite.close
$ K+ T; C% ?0 X; M
7 Z  b) q2 z# Y; j5 f1 DResponse.Write "生成静态页成功!" $ e  p6 \/ P* H* q3 y+ e. T# D) N

3 X( z! I/ s+ H9 Q* e7 _* W) t'// 释放文件系统对象 ! ~, b$ V, t$ k, g( v
set htmlwrite=Nothing 3 v! \( L' p: U4 [! y, _- D
set fso=Nothing " T  {/ S+ j! v
%> 1 Z# J* A3 @$ ?- n, _

, c9 X( u& R9 \% X3 b3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. , q6 Q4 i8 \9 P. V" I8 d, X
<% 0 D1 T5 s8 }! p" {, y5 l6 l3 Z" l
  U9 b! [& n# m9 _4 m! V
'常用函数
! v  y5 k9 c) B  _'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
0 x  Z8 B( N; H( Jfunction getHTTPPage(url)
' {2 }' ~+ H8 L7 u/ N/ B% rdim Http
3 ^/ i" ^! A, D) |set Http=server.createobject("MSXML2.XMLHTTP") ' `% C5 D: x/ G+ J, R# Z
Http.open "GET",url,false
1 N* L: n2 X; D) p; }/ wHttp.send()
. H8 j: E. q) X1 Qif Http.readystate<>4 then , T1 P) [: ?& u. ?. ?% d# D
exit function $ }! x0 M6 X0 |2 `4 E8 x) D( A
end if 9 a- D$ X" A/ o. A% x7 `4 l
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 8 x6 s, P* ]* @
set http=nothing , L7 e9 {$ T" l, |4 m
if err.number<>0 then err.Clear
" i8 L& ?6 W8 c3 E" `/ ?1 Rend function 6 J! w" z: E1 L
- h& i/ x1 T$ G: e6 x
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 9 D# y3 [7 I( s
Function BytesToBstr(body,Cset) - W% N, W, s8 T' N
dim objstream
( X3 F) i+ q+ uset objstream = Server.CreateObject("adodb.stream") * g& ^) D7 M: N( z: }
objstream.Type = 1 " E: H0 Y# v) }. I2 s
objstream.Mode =3   P: Y  P& F/ `) Z, k
objstream.Open
6 w- l& v& A1 m4 K  e8 N4 f1 fobjstream.Write body
; A* R: x+ u8 Kobjstream.Position = 0
: u8 B: W* Y; Jobjstream.Type = 2
- m) I, g' \5 }! c- qobjstream.Charset = Cset
* R) i0 l7 o) vBytesToBstr = objstream.ReadText
! X; i( |. o* R9 v$ ]) eobjstream.Close 0 b  V/ @' o  E/ n" [: }
set objstream = nothing " [2 x2 f; k' o5 P7 M  O
End Function % z! }6 h/ p* u, Z

# M* s- i" b0 ^! o* y
) [; e) S  k  W7 NtxtURL=server.MapPath("../index.asp")   O" ?" A7 M7 z! G0 M3 A
2 S0 |: [$ r8 `/ K, Q
sText = getHTTPPage(txtURL) ! f: e: l, o+ f" ^4 N
+ T  e0 W2 m: |4 v9 e
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 8 o1 H$ M& a6 S( e1 o
filename="../index.htm" # q7 R9 j: x% o& g: y" C
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 4 i- ]( n+ M- ~. H& R
openFile.writeline(sText)
& [$ n7 Q; M5 h5 F( l$ a2 S, dSet OpenFile=nothing
+ a' p7 K4 L# B1 J" {$ C
- J6 ~7 C1 }0 p) K9 B" h- ~%>
+ }3 t7 j! |2 y# a. E7 {% T<script> $ I% r/ w* k# F  J$ U% |
alert("静态网页生成完毕");
. F" e5 R1 {! E, d- h# [( dhistory.back(); * W+ @; s6 D$ A
</script>

返回列表
【捌玖网络】已经运行: