返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. * k6 H! o; a) B, Z( _2 K& b% N" n
www.aspid.cn的主站就采用了TSYS生成html文件! 6 t) U$ ]5 [; P. U
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
, g" e. L! q9 r; F$ \4 A& \& W/ _
! q' [6 u1 @6 ?: ]1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% , c( X1 @- s& ]0 G
filename="test.htm" ( h6 F( I0 o  h, B/ K
if request("body")<>"" then ' N) r2 q4 Q7 u. j1 @
set fso = Server.CreateObject("Scripting.FileSystemObject") " @4 u1 `" _: f) U1 N4 c
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) - y/ \  w( Z$ L. i/ \
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ) }$ S; U' D; q. g! G1 x
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 9 G% D! q9 [  P4 e, K% I
htmlwrite.close
6 `! b3 }* {2 `$ ~  c6 H" Tset fout=nothing
4 Y/ L7 u, o& f; O2 pset fso=nothing 7 Z, k6 N1 x9 o9 }8 l
end if 6 l* V2 ~$ d) t( h
%>
& n5 L. k2 o! I* D( ?) Y+ d<form name="form" method="post" action="">   v% X8 ~) g. r8 ]4 h& G
<input name="title" value="Title" size=26>
3 I9 x5 c, T; f% F6 v<br> - H. M1 V: O/ R
<textarea name="body">Body</textarea> ' R" P* g& I6 A$ I* F
<br> 1 ?4 T+ U$ \! x$ C+ H  d4 d
<br> & [: s3 V  _- C9 C9 C; Y, q
<input type="submit" name="Submit" value="生成html"> " S5 b+ p" i$ Y
</form> " R5 X- B. P! l9 _2 B$ L9 i
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 0 `+ t2 c' ^6 L$ k
template.htm ' //模板文件 <html>
# T9 r$ J: n2 [. \<head> . v9 Z3 {0 K& i6 b; `1 k# ^
<title>$title$ by aspid.cn</title> $ a: I, ^& T; G4 U# c
</head> * E3 g' j* \0 D* ]0 B2 k9 \
<body> % u/ s; W2 }4 f$ W$ Y# W
$body$
- d& P9 R1 p* B* d</body> 0 G! d9 ~5 Z  I8 W6 P8 N
</html> ?
( s+ o5 \: f  u1 a8 E1 I# l+ A  l9 b! R  ?* O
TestTemplate.asp '// 生成Html <% ( U2 n. K( y% ~' i, s
Dim fso,htmlwrite
+ t! w! O4 u  w) O+ c  u; A7 EDim strTitle,strContent,strOut
3 b1 @8 m" @1 Q'// 创建文件系统对象 " x% i# C. s; l  H! l+ P
Set fso=Server.CreateObject("Scripting.FileSystemObject") 1 Z0 m6 @0 M0 p
'// 打开网页模板文件,读取模板内容
4 r' Q) M8 u1 U' q% ^1 x! uSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
# _, P: c& j1 X) ?strOut=f.ReadAll
, e& Z( m$ S/ s, ^5 ?+ nhtmlwrite.close
8 `; y  A, o5 K
6 p) ]3 x' Y# l) M( A0 ?2 ostrTitle="生成的网页标题"
; {: n: o  s8 j4 _. h3 \8 D% ^strC
! S7 |$ {! u4 X; f1 v
& X. \/ x; E8 V' O2 s( M'// 用真实内容替换模板中的标记 * b) C$ q6 ?& L5 p! D( j7 b
strOut=Replace(strOut,"$title$",strTitle)
9 ]& ?" f; c& x6 cstrOut=Replace(strOut,"$body$",strContent)
6 |  u- y2 w0 h, v% B& Z$ f1 O- Z) [. `+ z9 U5 H
'// 创建要生成的静态页 : g" P4 v' I$ H( m" o  p! d
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) * ~/ Q/ b! o2 h; ^

6 H) `# o& E% D; y'// 写入网页内容
  z* C- V5 V/ w( @6 k8 i! }! q3 vhtmlwrite.WriteLine strOut
  F  r+ h* q8 @) W& qhtmlwrite.close   v9 b: w7 T- N0 |1 R) q6 I. A
; A# U+ [4 U. R  Q' `# r) g
Response.Write "生成静态页成功!"
8 ~% A+ T- q2 _+ B+ G: ~9 I8 I
' u: K3 [4 K9 `/ N'// 释放文件系统对象
5 k7 y8 n. W( p$ J6 S/ ^set htmlwrite=Nothing 6 D+ G! [+ g+ h, [: p8 t
set fso=Nothing 6 x% P+ |& k7 ?' r& X/ f6 m$ f
%>
2 `% K- P8 E7 o. ?( C& c. }# c+ R/ b5 ^3 e3 n
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
6 {6 T* Z- T5 [- R1 K% }: k) J3 d4 v<% ' m2 j$ ^# B3 P: M1 y4 ~

# M+ G7 t# v6 j'常用函数 3 }4 ], E1 V3 u+ ]  p6 ^; |
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
/ ^+ \" Y" M/ kfunction getHTTPPage(url) 1 t1 \; v" t& t9 |0 h
dim Http / L+ `" W/ h7 _
set Http=server.createobject("MSXML2.XMLHTTP")
# x, F. {3 P4 q6 n8 ]! PHttp.open "GET",url,false
9 u( p1 z) e0 d* fHttp.send()
* v: X. T, J" Y  t' o9 w3 gif Http.readystate<>4 then   H# ^/ Z! S, c% Q! Z1 X
exit function
" ^6 U' s" e) p6 Y$ x0 Pend if 0 \1 A3 @4 c6 l3 K
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
9 J+ i3 s# v2 Y+ gset http=nothing
9 Y. U& @0 B3 f) c, O# @& {if err.number<>0 then err.Clear
8 P# C5 w' W2 z2 |9 W, W: w. Gend function 1 {  I/ Z) w! y6 x* J# \( Y
" i5 q, R3 R- J& I) ~7 ]
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
! \8 v8 S: e0 \% a. mFunction BytesToBstr(body,Cset) 0 g# X+ ?4 {2 t; k0 ?
dim objstream
" ]( I  x& Q! C! F) `* \8 t' p1 p9 nset objstream = Server.CreateObject("adodb.stream") 4 \4 u# s% Q! `! J0 ]( c- @
objstream.Type = 1
1 k% v$ G' I8 V$ h/ T/ o$ L# U$ ]objstream.Mode =3
+ |* p0 t7 d; b, zobjstream.Open , @8 O  Z% C  V  Q
objstream.Write body
# d* b: n; h; B9 {  _objstream.Position = 0
+ R8 }5 ]8 h% b( E: q' |% Z7 tobjstream.Type = 2 ' W# B& G  [  a2 Z
objstream.Charset = Cset
6 o4 D& L9 n0 ?) o! @9 ?! gBytesToBstr = objstream.ReadText 5 n. X+ [! |- U$ o
objstream.Close
# _5 o0 N4 Z5 W- u3 M$ H5 p4 H8 u0 Hset objstream = nothing
& k/ i$ `4 }' q+ Y# MEnd Function
9 Z) h9 Y2 y6 ~6 l" K' N
5 }/ m4 @" P- b7 Q4 j6 L1 i: C! M
txtURL=server.MapPath("../index.asp") ' s+ r8 Q- M) _$ ?8 c7 C
8 f* u& X6 M8 @2 Y. U7 B
sText = getHTTPPage(txtURL) + o: j& x( B5 }4 u+ U  F/ X# C

- b, k; q7 y5 G/ ESet FileObject=Server.CreateObject("Scripting.FileSystemObject")
0 e5 y+ f: t/ U, r! P0 ~) Cfilename="../index.htm" . ^4 \  D( a+ E' \! @2 ?% [1 s
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
  H# ^7 X/ q+ F9 ~5 RopenFile.writeline(sText)
$ _8 ]6 I8 J, j+ DSet OpenFile=nothing - M0 d" x+ m+ y, i1 s; i
5 q! B0 w$ C3 f! l! F6 \) R4 G
%>
; x$ N7 I! E  X- ?<script>
: Q* Q* Z! A) ?2 c# w; y( Ealert("静态网页生成完毕"); ; d+ \8 K6 F6 v& T4 S6 d
history.back(); % p5 c) v/ Q! Q- x& V; Z, h4 `
</script>

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