返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. $ Z. E! y: d5 H& I% `  b
www.aspid.cn的主站就采用了TSYS生成html文件! 1 W! \' H" @; P8 l$ T: m
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
# ~4 X/ R0 e$ s4 i! l9 g
1 \* r: U9 ]6 {1 J. A1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
! A" h( r& X$ Pfilename="test.htm"
) \0 T( i5 w3 Y, ]if request("body")<>"" then
5 q/ b1 B4 e- A$ I4 `4 c4 Lset fso = Server.CreateObject("Scripting.FileSystemObject")
9 [. e6 b2 t& m% [5 s( q0 qset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 4 H& Y# e5 n1 C3 s4 W
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
% _$ {. d8 @/ L4 U# t3 s  n+ jhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" . X1 R  E( T% U
htmlwrite.close
. |: w! f! ~' g+ r- pset fout=nothing 3 o( _& V( H% f
set fso=nothing 6 S& ~; o7 N  {" P1 B9 T0 Y, B
end if
7 C. R4 a% ~1 T/ M8 ^1 o8 t& m" D%>
* }- U- B: a6 i1 a/ T" Z" t; P7 f<form name="form" method="post" action="">
4 p8 E2 E" N$ {: N3 B1 }  d<input name="title" value="Title" size=26>
2 ^( Q9 c& h) X( q" T! R/ v<br>
% K% z; o' r- ~6 |, b5 z2 k: @<textarea name="body">Body</textarea>
6 P. s8 D  r2 Q; D0 u  Z) X4 t<br>
- q" ~5 \, G2 \% e5 ?<br> % z* _6 b) q* g
<input type="submit" name="Submit" value="生成html">
- k9 [; O# y9 z& C* G. K</form>
, N$ V) }& k3 ]8 W/ o2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. & _! r( W5 F8 P, `  Y
template.htm ' //模板文件 <html>
. p  E( W: Z# G2 U, e& f<head>
" U" G: e7 n  }<title>$title$ by aspid.cn</title>
( y- u" f+ z9 W8 f0 R</head> : T+ o8 t& I) u, @+ B& ~& z4 I
<body> : h& o1 @. `; \( j" G" x& N. _
$body$
' A  j5 B: Y; H7 h$ u</body> ( F# x$ a$ ~8 b
</html> ?
+ I, {* o. F, h) i2 l- y# r2 ~. v
1 y: G2 G5 S; h* |1 W# z5 WTestTemplate.asp '// 生成Html <%
1 O2 A- B! l8 l# N, y$ ?6 l: ?* l) ZDim fso,htmlwrite
. |" C# X# v8 {9 T( l4 ^2 ?9 EDim strTitle,strContent,strOut
3 D$ i+ s; X0 Y# y& ]& k'// 创建文件系统对象 1 V1 i8 V/ B' V: {0 n# Q9 u& s3 l6 n
Set fso=Server.CreateObject("Scripting.FileSystemObject")
& u/ f6 o4 X5 D; W0 Z& q'// 打开网页模板文件,读取模板内容 / I! j, \  Q1 j, X0 x4 y. m
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
' {) H( G8 y" |3 ]0 K* nstrOut=f.ReadAll - o1 H* e8 w7 l7 o5 o1 F. X1 ?1 L! T
htmlwrite.close
) D# m. F$ e9 Y5 s5 L/ l7 Y6 J8 f2 W8 V3 j' W0 M
strTitle="生成的网页标题"
& Y. V( e  ?) x: @" HstrC
( A3 K4 k  P& Y0 ~% y/ O6 ~; A+ x7 G5 A/ U
'// 用真实内容替换模板中的标记 , T* a( A; i, B( W  D
strOut=Replace(strOut,"$title$",strTitle) 3 \7 m4 [% J1 c/ _) N
strOut=Replace(strOut,"$body$",strContent)
! q2 p% {4 J; i) d. @# A+ B) r0 D( |
'// 创建要生成的静态页
) u8 \1 F# t; p* n8 _5 CSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
: n" O- N8 V% V+ S/ h! V+ B% Y* E( E" i$ o4 A* }5 @/ m
'// 写入网页内容
7 e1 r+ O8 c/ z# |htmlwrite.WriteLine strOut ) S" b( Y( J3 t/ k4 g% ~
htmlwrite.close
& e: ]* O3 c: L. s2 h# V" p6 @5 ?+ M1 [2 `; E
Response.Write "生成静态页成功!"
9 X3 |2 V3 J* n0 J, m7 }/ k  B- t* U
'// 释放文件系统对象 / u4 {7 q# r0 W( s6 {* g% [
set htmlwrite=Nothing 1 H6 ]! w! W" c, l
set fso=Nothing 3 D7 C5 D/ @8 u$ ^3 m7 g: N+ V9 b
%> 6 r. f1 \, l) M: U( y" ?: W

+ |0 I# o4 g3 n! e. u/ o3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
8 V' w  O2 J5 C  ^+ P<%
$ ?- u5 w- t2 t# V6 c' Y
" A1 i1 B9 K  ?2 i& C) j- O9 v'常用函数 9 ]7 a) v$ Y; R
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 0 X( h4 i* M& N) Z5 I  `
function getHTTPPage(url) * F& M( l4 p- t6 [: e
dim Http
, b& T! ]# Z7 @5 g6 V) Z1 Yset Http=server.createobject("MSXML2.XMLHTTP") ' I7 T! `+ N( Z8 g; m  f  J
Http.open "GET",url,false
' T, J0 J% C  y# w1 H& C: jHttp.send()
# h3 O7 R$ N7 o; R4 W: Dif Http.readystate<>4 then
1 g4 p1 S9 f$ k! _. c. Z% X4 {exit function
2 D$ I" o( _8 Z6 G$ Y& K/ L5 send if ! O0 b9 q. E3 d, N3 z; q
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
5 ]2 i; f, q7 I3 |set http=nothing
( |2 L0 h1 q. l" L, w3 Yif err.number<>0 then err.Clear
0 z& x: g, I+ p  V7 l- {  e  A# d1 Lend function : L- o; B4 h8 K3 \% F

+ a# K" t# R9 i$ ?, R, f. Q* ]'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 ! E, T# I+ \3 A) d2 Q  ^2 L& k6 h$ H
Function BytesToBstr(body,Cset) 4 ^, L, x  q( V6 K) ~* {7 Y1 G& k
dim objstream ) j+ c$ `- [4 G1 ]3 ?% q* V
set objstream = Server.CreateObject("adodb.stream") ; N$ I/ ~( q3 v- k
objstream.Type = 1
- a5 R! u. M+ y7 D% h' |objstream.Mode =3 9 d! t+ o% H6 ~' p
objstream.Open 8 U) e1 W; \( U0 l& Z2 `5 F
objstream.Write body ) l: e; r  K8 Q& ?. O4 g+ E
objstream.Position = 0
/ f% y; q# I: I1 b. q3 y, bobjstream.Type = 2 ; l/ }( ?# T, x9 z% s
objstream.Charset = Cset
5 o1 K- x. I# N. @+ g. OBytesToBstr = objstream.ReadText % ~! L5 h2 t* n2 U% W
objstream.Close
- L9 D# K5 U* Y5 L: T, Tset objstream = nothing 6 J9 ~- ^# o; J+ p! Q, M8 ~
End Function
8 _8 b/ K2 t7 U0 O; K* n
* v% b' t/ u: t4 r$ W/ d7 [) T- i" E, F# ?
txtURL=server.MapPath("../index.asp") 9 _3 a" R5 k  h, Z1 s
# O0 ~3 ?! k: H& b, F; |7 c
sText = getHTTPPage(txtURL) 4 ~* i* q/ c* \1 W$ l
- z" f8 [( M6 L" v3 {
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") + I; O. c3 I8 W/ a' Y0 F8 D8 h
filename="../index.htm"
/ ]  u3 @3 S" j# \: r# h7 p8 q" k' dSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 , R9 I' {- @# }( s/ p' F& ~. _* [; L* _
openFile.writeline(sText) , c& O8 y9 E. m1 Y7 c
Set OpenFile=nothing " b, i$ ?  a" N# f

* P9 l! p% H: ]: A5 q, T' t- z: e%>
! c- ?0 K& i8 @  X& Q" c<script>
$ `: ?! C" k3 c% Falert("静态网页生成完毕"); 7 w& Y# m5 W+ h% I1 S1 s+ k/ `- l
history.back(); 7 i# j+ ^0 E! g7 T. `
</script>

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