返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 1 C/ N9 w9 A9 }
www.aspid.cn的主站就采用了TSYS生成html文件! ! D/ H7 ]/ m. B. I$ F( |) |8 u2 r+ I
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 6 m: L1 m4 }# g- S; K: g8 `4 C

/ }1 v- t$ Y  F: F1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% . h$ N0 V8 ~% C! L
filename="test.htm"
9 y+ Z; j, [, `3 W  k5 Tif request("body")<>"" then ; @/ j5 k  P! f9 P% k( \
set fso = Server.CreateObject("Scripting.FileSystemObject")
* }0 b+ R* y2 G6 }8 `. yset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) / K# y  i; {& Y+ G
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" - N3 a9 B$ g- W% l2 i2 v
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" / t( }$ {! t: ^* L: T) `$ ?8 }" H; {
htmlwrite.close
4 J& O2 j* E- f) ]$ I% pset fout=nothing ( F! D, f0 V9 ]5 J
set fso=nothing
5 b$ `' J$ |1 l0 u) W6 j3 W6 cend if
' J4 x2 ?& j- J; Y8 j- D/ V%> - }1 _: X, k# P$ R& i1 G" \
<form name="form" method="post" action="">
+ a: r0 M6 M) D- O  G1 n* l<input name="title" value="Title" size=26> ( d3 l, C! O4 a
<br>
. w( j3 Y% i" Z3 _+ l' x<textarea name="body">Body</textarea>
0 v8 ^; J5 m1 ^6 u5 o9 y<br>
! X# i8 O) B/ {' {' y5 F<br>
- F5 j( Q8 T9 Z% ~5 i# \<input type="submit" name="Submit" value="生成html">
" J7 j. }" {' u6 }</form>
( w0 y3 A* k* u) A8 @7 Z2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
, _# y2 F* l& |3 E- I5 Y! Rtemplate.htm ' //模板文件 <html>
- J6 {1 D8 R, f1 e. s0 G. {! L<head> / x- D; R! l9 Z5 Z
<title>$title$ by aspid.cn</title>
9 |: n2 }: T7 \( R) ~8 W8 E</head> 7 s& V3 x; L& l: L7 `5 l6 O
<body>
9 C- c' R, y3 U4 y$body$ + o; N( P3 M! d
</body>
/ O4 B3 a2 _% c) o  d</html> ?
( @" `4 g# w" u# ~! L6 H0 p
3 A: Y0 X, x! ^# H! `TestTemplate.asp '// 生成Html <% ; I9 E* ~2 _+ G5 E6 ]
Dim fso,htmlwrite
  Y6 V' s6 x% [. D+ _5 }Dim strTitle,strContent,strOut
* @, s# e- z/ U6 ]) G3 m& f+ S'// 创建文件系统对象 4 V7 w& f5 J7 H% |. o2 ]- d
Set fso=Server.CreateObject("Scripting.FileSystemObject") 1 ?9 k0 N* B0 R1 n' F* o
'// 打开网页模板文件,读取模板内容
; p& T' K6 l* h) dSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) # c& Q7 u5 u& P
strOut=f.ReadAll
, x  r' b% k' C, {" Y; D' Xhtmlwrite.close
, ]7 H" X  g2 k0 k0 p  d$ n& F0 x; b' d" }, \" g- F
strTitle="生成的网页标题" 2 C1 Y. l# i# V0 s0 h
strC 8 x0 `% t$ r6 R" V
9 p8 ]4 y" v6 e
'// 用真实内容替换模板中的标记 $ g/ A# O3 n  p0 Q
strOut=Replace(strOut,"$title$",strTitle) - U% P+ D' z: r* }  a% }* `4 f$ q4 h! w
strOut=Replace(strOut,"$body$",strContent)
8 `# Q# ^  J5 G$ ?# |! x1 F( `7 N; f: j- I/ j* }$ I
'// 创建要生成的静态页
" a8 V5 A7 t, f( x! j& y! hSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
* Y5 B* ^& L% X6 w/ b$ h; U8 ~+ o2 Y) r( d$ B3 `0 `
'// 写入网页内容 ; z, F) Z: {, X  X3 s, }
htmlwrite.WriteLine strOut 9 Z& @& A1 R2 ?0 N1 {! _3 k
htmlwrite.close
6 L3 S  D& @8 {- ]4 G
/ G* A* B; K$ e( T& HResponse.Write "生成静态页成功!" , ~7 o& U$ k+ Z) J3 f8 C
+ R& P* @% {1 Z
'// 释放文件系统对象
  h9 M5 e, J4 ~7 ?0 Gset htmlwrite=Nothing % K& a) \/ W# v! v* H2 U- C
set fso=Nothing
' Q/ i9 d9 ?0 d. s  ^( g! j8 a%> & J# d+ s  n1 S; M( Y% |& c
7 S8 S& i3 O( v* s/ ?/ E
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
$ n* X: j4 C( c8 |, l- O) o( p<% & S! ^. Y5 v5 {; \9 ~

- W5 B' m7 }  Y. U6 C' g* ['常用函数 ; r5 r+ F$ G3 q9 T. ]
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 # C; z; _. |+ t; u$ P- W
function getHTTPPage(url)
! J$ l# j9 B! G8 J% ~dim Http , H  O6 x2 }" U" N( \
set Http=server.createobject("MSXML2.XMLHTTP")
: ~3 J# x9 _" z+ @% I, @5 f: oHttp.open "GET",url,false ' U2 u2 C! |# h8 u! c3 I& m3 k! K7 G
Http.send()
" F. p% b$ v9 p; mif Http.readystate<>4 then
" n: N  V4 Y. ^) c' O3 x0 ~exit function
& N, T: `1 l4 Hend if
1 C# W9 P+ S& r, n8 o! g( igetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") - V" L2 F) R- A: g5 a+ s$ l* v
set http=nothing
3 e/ R+ k; j( E6 n. I1 I3 b" N- I+ Bif err.number<>0 then err.Clear 9 d3 A. \( K2 V0 z' y
end function 8 }3 g; }! t# C2 z- z! ?; u
: I' ?7 \$ h5 }$ ~
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 : i6 J' s. }7 I2 Y! C$ e/ K/ z
Function BytesToBstr(body,Cset)
, L# O3 y1 u* q/ Idim objstream
1 b8 Y# _) i7 u+ K' E2 B$ u2 Wset objstream = Server.CreateObject("adodb.stream") # c( T  {% ?1 S/ D, N; J9 J& r4 f
objstream.Type = 1 + V( V, c% P0 D0 z- D  a
objstream.Mode =3 ! ^) ]/ ?, ]2 _# _' \) ^- S  s
objstream.Open # _' U" B% [$ [
objstream.Write body
5 j+ k2 m' a9 s9 {- Tobjstream.Position = 0
  q; i9 ~) X+ }6 _  `% a# q' }. wobjstream.Type = 2 " C- {4 b% W' V
objstream.Charset = Cset / B1 W5 a# j8 S8 a  K- D* u
BytesToBstr = objstream.ReadText ) T. k3 n9 \7 W, H3 |" n
objstream.Close 9 n* v9 ~8 _" n2 n. \1 o" Z
set objstream = nothing 3 @" \4 I* }0 a2 o* V
End Function 3 s# R+ J6 N( t/ s4 ~5 @( O4 e1 h6 W
" }4 H% Z* R6 }( d

# Y8 H' e7 l5 E$ d" g  ttxtURL=server.MapPath("../index.asp")
8 U/ q0 u. U% v$ L$ l- w2 l5 b0 i+ r6 ?' d, b& |. r; W
sText = getHTTPPage(txtURL) : q6 l  y8 p. n, T6 [: U

" c0 ]2 R1 ]2 H- y! }" eSet FileObject=Server.CreateObject("Scripting.FileSystemObject") 2 x& @) S1 u9 ]) C/ `
filename="../index.htm"
/ V# h; Y5 S5 L- z- d5 ?8 BSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
9 A0 k0 ]( R0 D8 z! ~0 MopenFile.writeline(sText)
# K: b0 x1 w( P, P/ QSet OpenFile=nothing
# V# s0 V1 R$ X/ d' y) @/ y; \! ]5 C4 h  z# B" n' A) Y9 @
%> 1 J: d8 I. O7 ~' K
<script>
# S$ Y+ ~  G7 b0 j6 [alert("静态网页生成完毕"); 4 ]+ Z1 S5 G& z0 `6 G% r* a
history.back(); ) S7 ?) h) \& X6 H+ K2 m
</script>

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