返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
, J8 k2 `0 V" W& H5 u& zwww.aspid.cn的主站就采用了TSYS生成html文件!
6 k$ I  V) D, O1 ?# J, S) \5 v所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
7 F# V+ X# l+ [, ]8 r6 T6 w) I8 H0 w0 e( S: V
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
7 {" i7 N7 N  p% efilename="test.htm" 5 \- K+ [6 w/ n* v, L8 u! W
if request("body")<>"" then
+ H: Z7 h4 a, h. Cset fso = Server.CreateObject("Scripting.FileSystemObject") ; w* w1 L+ ?% {7 f9 z
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
. @/ K" \) y% ^" b# Whtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 7 j4 l' @4 ]/ }5 q8 W8 n
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 3 g9 k. Z: }% x: o, Q! X$ f) k/ h3 J
htmlwrite.close
5 j2 k/ w* F  [# O: t% c7 xset fout=nothing
+ `  Y2 ~. ~- X/ `3 D* l5 X2 eset fso=nothing   b$ k; X4 g2 [, \
end if / J* e. u& X  B, }
%> & @* r7 L$ A* H, C7 n  c3 a5 A9 M
<form name="form" method="post" action="">
. ]8 U( K' {4 T( K- n) a<input name="title" value="Title" size=26>
# w- d/ Q- `- q3 A<br>
4 U0 |+ Y+ P: q! _8 B# R+ X7 z; C<textarea name="body">Body</textarea> 6 p+ y) y: e# ~
<br> / M! H# R7 s4 ^$ @
<br>
4 k; K$ t& _% t<input type="submit" name="Submit" value="生成html">
2 C' J/ a" d7 d: ?' o1 j1 g' f</form> : a' e$ z% K  p7 H7 B+ R
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
+ f% H5 U- s9 g! T2 ltemplate.htm ' //模板文件 <html> 0 R7 |4 s! O# M/ z0 b
<head>
! l, @, q( ~3 P7 z: S<title>$title$ by aspid.cn</title>
8 Z- W4 \' l; L& L</head> " }% C9 |( }8 N( i* S
<body>
, O* |6 F/ ~  x- _# S+ B9 p$body$ 2 e& ?4 A7 l; e; A9 [& @. ^
</body>
5 ~5 K7 b$ d) ^/ m: D# A</html> ? # w9 c2 k: C6 w  h6 g

, G# K8 x5 N" N2 {) y, dTestTemplate.asp '// 生成Html <% ; P; f. K! l8 f) T0 }. k' w
Dim fso,htmlwrite
, p2 |4 {" x* v  Z. n) y! X% pDim strTitle,strContent,strOut
8 _# f+ E7 Y/ V+ O4 J5 R'// 创建文件系统对象 8 a' t' o; K* l
Set fso=Server.CreateObject("Scripting.FileSystemObject") 0 c7 V: [- I1 D( s. N) X1 V
'// 打开网页模板文件,读取模板内容 6 Q8 e1 M+ x' m; v% U
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
+ n5 @" R0 l3 v* @) }# I7 @; V9 QstrOut=f.ReadAll " O1 g0 J. E  V& F- r+ B" Z; s
htmlwrite.close
+ t; F: E8 j  P! ]) H* K
- z( j5 \. k2 b8 qstrTitle="生成的网页标题" & C( Y9 h9 `$ i8 F' ~
strC
9 _& y7 h) i0 S6 _0 j5 M
! D0 S. ^% H, P1 e+ B'// 用真实内容替换模板中的标记   m% ~( _" e% t7 R( H
strOut=Replace(strOut,"$title$",strTitle) ! P' M1 i5 h  |- {: W
strOut=Replace(strOut,"$body$",strContent)
1 W: n+ \- w6 r$ i
2 w% C: {5 X: c1 ['// 创建要生成的静态页
- F4 H! D; N7 p& X' ~Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
# U3 V6 n' z+ b8 T. w# ~! C# L. O3 {, y+ P, [# s4 t
'// 写入网页内容 & a) w! K' `" E/ `2 g9 z! v
htmlwrite.WriteLine strOut
/ j* U9 D1 k+ v7 e9 T. Bhtmlwrite.close & A! Z* U- V/ G& M6 l9 R
9 U& f. d) V+ x2 N1 X. J* b
Response.Write "生成静态页成功!" 6 m/ e" h% b: f9 q

! c" U1 r4 M% z* D- H( \'// 释放文件系统对象 * r" P# v0 G9 z. ^3 \. w
set htmlwrite=Nothing
+ a% ], r% w, g2 t. uset fso=Nothing 8 b- A5 A  p0 M; N* {, }
%> 7 [, C- G- `# ~% G" n

7 f, p4 n3 @, r5 g* k3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. + }3 j- b5 y- b: z" |4 F) J2 n  p
<% 2 `7 V# ^3 c# x& k
9 g" I* p/ `& w3 ^
'常用函数 % s# T. O4 ~  F6 q6 @; W
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 / k0 o+ n# A5 x8 q$ Y% p/ f
function getHTTPPage(url) ) |. B! p" c$ L7 ?, F: w  b, Y3 w- x
dim Http & l3 y/ K: U4 }% B7 ~" p
set Http=server.createobject("MSXML2.XMLHTTP") 0 Q  c+ [/ V6 `& L+ x
Http.open "GET",url,false 6 I& g+ ~2 y( W$ J% h: @
Http.send()
- J  a8 L. Z& }' g% j, I2 O* f( hif Http.readystate<>4 then
( |0 d0 I8 a/ r4 H8 Jexit function 0 r0 x) s. d3 f7 V, b- b
end if ) X, f4 T6 ^- R/ z+ X$ u
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") & b1 E5 \9 w; U$ }* m! d
set http=nothing 1 S0 f. f4 L) x2 Z
if err.number<>0 then err.Clear ' ~* _* f; _2 h+ [
end function
  O" R7 P: [$ q( L; R
7 B- X  F! i6 Y& J- h'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 / \, w6 e7 |+ [  `+ b
Function BytesToBstr(body,Cset)
1 m; @' t0 k3 Z" h9 f& _2 F# @9 |- k% Z$ Ydim objstream
0 L: J2 Q0 h% L. C/ Jset objstream = Server.CreateObject("adodb.stream")
, Q: _* o' J6 r( J6 qobjstream.Type = 1 $ P) \' H4 B$ ?4 _
objstream.Mode =3
3 P6 j7 o5 p1 Y9 z, N5 m7 ]objstream.Open
) N( {( Z# `1 M8 ^objstream.Write body : E( o  Y3 K" k0 n3 X: r/ _
objstream.Position = 0
7 ?. [9 L2 W& ], i, y7 }7 N) pobjstream.Type = 2
5 S& Y& x7 j9 \' ~( aobjstream.Charset = Cset 1 z) ~9 z* I! K
BytesToBstr = objstream.ReadText ! o& E( d8 d. m3 \
objstream.Close . U9 m) P! E! E  ?& `  u
set objstream = nothing
' v& Q) b' g% d% _4 OEnd Function
; x( N* J: o  S" H, Y) f* x) `+ F4 Y+ K# ]
& k) t: O" E4 o: p
txtURL=server.MapPath("../index.asp")
. Q% C; E* l% Q( `; h* e2 R1 S
3 ]3 `. w, L! Z; u3 q5 K1 y, v; dsText = getHTTPPage(txtURL)
; E! {) E0 w6 o2 p8 A! ?, Y- G. ]' ^) g% o+ r) h& w3 M0 @$ T
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
" A' x& S# ^7 C6 R9 o3 P3 }filename="../index.htm" 2 w' y" l" |3 }, ?
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
4 t- D3 M% M- d, zopenFile.writeline(sText) & F: s+ p9 ^( R4 B* S+ ~
Set OpenFile=nothing
* \; G9 B2 ^  n' i1 K9 x
- r5 h# p7 K# V%> : b5 f4 l/ c2 @5 [
<script>
+ u  I! S0 p* M" j7 N* xalert("静态网页生成完毕"); $ u. w  L' V+ I& h; L. N' _8 j
history.back(); 6 `, i; H0 p/ a$ y9 R  l3 i4 t
</script>

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