返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
2 b( B( I  J) e9 c* P! ?+ D' o6 [www.aspid.cn的主站就采用了TSYS生成html文件! ( G; Y) O9 R  W) P0 T* h' D5 H5 l
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
- u6 B$ {% Y0 _# K; d8 L1 |/ L: b- S- r- x
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
5 s3 ]' P3 o) P4 Dfilename="test.htm" 2 X) `/ F9 d2 J9 D# c: U) Z
if request("body")<>"" then
' p9 i0 |& I' o# Fset fso = Server.CreateObject("Scripting.FileSystemObject")
) r( ^; o' B, r3 Iset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) * n1 ^3 W6 E) c( U
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
# v# N# z) x4 {9 u$ d9 M9 Vhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ! M! U0 U; m4 T  H1 y( X
htmlwrite.close
$ l4 i8 `/ o0 r6 e, Hset fout=nothing 3 m" W3 ?" H& W2 ^+ s9 k- T
set fso=nothing
# r8 D# b) b, F( L! t# eend if
  l3 L& A( Y! z& c7 @* l+ D9 ~: [%>
4 M5 o$ z6 P: _, S( u<form name="form" method="post" action="">
. N3 V" @( r& n% z! Z7 I2 J<input name="title" value="Title" size=26>
4 E/ F4 {2 Z8 P/ s, o6 H<br>
0 O9 I" R5 ^- C+ A( x<textarea name="body">Body</textarea> 4 M- c5 F/ ?' }% d3 u+ @8 q
<br> ' h! t# G, |' j. Y
<br>
. f- \. A: q# e' f4 E8 ?! i<input type="submit" name="Submit" value="生成html"> 8 B+ Z, K" K% K4 q3 W% J2 V
</form> / Z7 U& c3 Z% g4 ?( o
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
8 `( c! D3 B: ^& L& Q: v: Utemplate.htm ' //模板文件 <html>
1 o) X- R6 v" X; M* O* ]2 N<head> 1 [0 F2 b9 W* `# p
<title>$title$ by aspid.cn</title> 6 y1 o3 S# U+ [" E2 R
</head>
; l) @$ j' Y) V  ^) x  B<body> 2 R5 m* n. a! V
$body$
8 m, T* O5 \( B9 U; ^6 ]1 \</body> % [' Q6 }( p4 m- V
</html> ?
6 x. ~! n( x9 G' R8 ^0 M6 h! s; g( G* ^7 A/ B1 E
TestTemplate.asp '// 生成Html <% 4 s# @+ E# ~, H6 V
Dim fso,htmlwrite $ Z/ ~" z- z# P) Y9 k* ^9 P
Dim strTitle,strContent,strOut 7 a  I# R0 X! Z: D3 m: l, P4 B
'// 创建文件系统对象 + l& z* V! e: W$ S: S# }
Set fso=Server.CreateObject("Scripting.FileSystemObject") 1 ^" y( v: O' R1 U
'// 打开网页模板文件,读取模板内容
; K* ]$ M# o0 D. QSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 0 f$ {6 Q/ l- J
strOut=f.ReadAll 4 ^6 |; e* F8 n) U3 J
htmlwrite.close
' B( V3 H( c+ E9 ^" i
% Y; @8 S: o+ h2 w5 vstrTitle="生成的网页标题"   Y2 M. D4 v' f) K, t" c
strC
1 }8 V! d$ a2 h5 `8 V1 x" ]
; a6 _+ r/ {3 N'// 用真实内容替换模板中的标记 " m; C0 ]. Y7 ~
strOut=Replace(strOut,"$title$",strTitle)
% k- Z- I3 Y8 |) h$ UstrOut=Replace(strOut,"$body$",strContent)
( x7 X: V% \' t4 X4 M9 }' E: F# X! Q, k- Q" i& E# Z& w9 m
'// 创建要生成的静态页 3 ?$ l# @- `4 X" J' `
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
" U5 `" Y2 y* C9 G) k6 h7 o7 n6 |# ]3 y5 i
'// 写入网页内容 - I- B& B- F$ h3 ~  w
htmlwrite.WriteLine strOut 4 H# G/ X( @# y1 ]! h1 a* V
htmlwrite.close
8 T! o3 d; }& z: k
6 [3 R) q( H) _4 K. FResponse.Write "生成静态页成功!"
" T2 U) W# x- m. Z  }0 A. o: i* c1 q0 C6 H9 _* `! g/ J0 H, G
'// 释放文件系统对象
. K$ D4 I2 J8 x, J/ wset htmlwrite=Nothing , ~1 P# l0 O7 `! T2 R& t$ G5 a7 `! Q
set fso=Nothing 7 w* f3 q- E8 a0 K9 @
%> 2 p  s. X+ f& \/ I& p. Y& K
( h0 r& K* d% Y! C6 M. G8 {
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
, D/ E* j1 L' S  S0 H$ e<% ! D5 J$ j2 Y9 J1 v) X

6 o' [4 z' T/ ]8 D2 c8 X'常用函数 * D: P/ W0 N7 X
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
& ]. h9 g6 d$ j) q1 M% Bfunction getHTTPPage(url) . F( L) E+ D8 x7 o. R
dim Http $ B! N  M! b  Z9 ]  R
set Http=server.createobject("MSXML2.XMLHTTP") ) ]2 a# @. Z: r1 T" r( Q
Http.open "GET",url,false
& p1 a# e# e) t, O  O- y5 f2 `+ \Http.send() 9 D- t0 ~7 \$ z; X6 W+ Q
if Http.readystate<>4 then
! n' B" A9 ^9 {0 O: Rexit function 5 `: [8 o) e* g
end if
) X3 ~; ]9 m: N8 h& w1 n' OgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
- v. \& ^* Z3 n+ k- }. [set http=nothing
$ P/ v2 m5 h8 y  qif err.number<>0 then err.Clear
0 {: D+ }2 s9 {* ?2 P  oend function
" e; g4 @% L  w, }2 j) q% q- m% {$ f4 f6 c2 ]4 j  n  J! w+ q
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 3 B- U) y2 ?; j. L' P
Function BytesToBstr(body,Cset)
" e. W! R; p7 G: p! S7 g1 Pdim objstream
5 y. V% ~. l& ^set objstream = Server.CreateObject("adodb.stream") , W2 ]& q% _: Z% P. k! w
objstream.Type = 1
6 A: g/ j  Y, O8 D2 xobjstream.Mode =3
3 K: O+ K( p# R/ ~# n" I  Vobjstream.Open ( U8 `& t. a4 h5 v2 w  z
objstream.Write body
: q. z! ^2 R/ }# K) \objstream.Position = 0
$ w2 e, a2 ]9 Y. j! y  G8 robjstream.Type = 2 2 k7 E- [. j# V
objstream.Charset = Cset
4 J" H+ Y5 v" Q+ a  k- b1 \& s0 qBytesToBstr = objstream.ReadText : W* `2 B8 o- h) {% T  G3 E
objstream.Close 8 T/ `5 P, b1 @1 }7 Y+ O
set objstream = nothing 0 L9 P5 ^5 _4 [) g8 V. h* ~4 D, s
End Function 2 P- b3 e/ ?7 W6 d. P
7 _6 T( H- |" l% Q9 O1 J
: P+ i0 Q- f- r
txtURL=server.MapPath("../index.asp") & ]0 ]; `* z- Y3 @: m, H- `+ {5 B
- n. s  C, I0 `6 U
sText = getHTTPPage(txtURL)
9 D; x* X$ s- [/ u" F- E6 s& Q  h2 d" a4 l# ~' c& _) e% G" c* X
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
/ b: o% W% T# ], A7 f: D  k' L% }filename="../index.htm" 5 D' F/ t1 }7 ^* o  d
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
% U, }, E$ L( p. _% O/ [openFile.writeline(sText)
) y4 O0 @1 f2 ]' _2 KSet OpenFile=nothing - w+ q: ]8 D9 T7 C& H
& S2 U0 v4 T. T! C0 L& [7 E
%>
' l! U5 T5 J. W7 \4 Q( }<script>
0 A- ]$ Y1 y8 R/ [alert("静态网页生成完毕"); ( n; e1 @3 H( ?& O7 a( A" k
history.back(); 1 ^2 c. k/ o  T6 \
</script>

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