返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
8 R1 {, L# G/ \* y. v- ewww.aspid.cn的主站就采用了TSYS生成html文件!   L1 F+ e6 N9 }' b7 Y  y& m" C
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
# o# _  w9 q) g, v& n# X4 q5 G% O
* e1 \  b( S0 l/ P9 \- s& Y: K1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
& K" E; _. k$ E) z& ofilename="test.htm" ) e: ]* Q0 U* B: s* r  l3 c8 t
if request("body")<>"" then - N, ?5 `1 F4 l: k
set fso = Server.CreateObject("Scripting.FileSystemObject")
7 h- M+ L$ D. L: @( B$ |3 ~& vset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
' F3 A  _. q! x# T8 khtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
# f! l: }- ^1 p" dhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
" c1 ?( R6 K( l+ T/ shtmlwrite.close
) w- Z. _  M9 p1 N) f3 Pset fout=nothing
5 `8 @4 _4 P8 p# Z5 z9 x8 |set fso=nothing ! d8 H- k, a/ W: |
end if ' ~# n4 j$ ~2 P6 d
%>
: F1 C6 n5 Q% O<form name="form" method="post" action="">
- ], P; d  R, t3 W. f0 Q1 R9 n! R<input name="title" value="Title" size=26>
9 p$ B- e' W% i  r$ J' f<br>
8 O: L" @; f' C0 x' X* _( ?<textarea name="body">Body</textarea> 6 S+ I, |$ X  k" R& a
<br>
) h+ c" J9 Y8 X<br> : |2 O1 R' e" ]9 H5 b9 m; y3 E
<input type="submit" name="Submit" value="生成html">
4 Q( d. m2 \& Q</form>   K+ y6 P1 ?, u' ]1 m
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
6 ?, j3 k: z! L/ d& Y. J! xtemplate.htm ' //模板文件 <html>
- D, P" B& E  G5 y<head> $ t+ @3 G$ ?8 a. R0 b! P/ p
<title>$title$ by aspid.cn</title>
4 g4 O+ r8 f- u# A7 w- r</head>
2 T# x; s4 S* R* b/ x: m# p% X<body> % z$ o  a8 |/ c  [5 T& f
$body$ - {) J4 L0 i( R  f6 d9 m; N
</body>
; S. R: J: H+ ]% r</html> ? + @2 X! ?2 I) L9 t+ Q: E
1 E; t7 A2 V  {6 [
TestTemplate.asp '// 生成Html <%
" u8 y' x. f  CDim fso,htmlwrite
/ Y; k) @. L6 @3 ~$ NDim strTitle,strContent,strOut " N4 O9 m# k5 O- \# w* H7 D
'// 创建文件系统对象
& X# i) U3 q9 }, ]7 }, t* N' G% f4 J0 iSet fso=Server.CreateObject("Scripting.FileSystemObject")
1 c4 s9 K1 Y" f: u'// 打开网页模板文件,读取模板内容
' g0 _& S* L. Q+ ?Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
6 \" R) t6 e/ X9 z$ d+ [strOut=f.ReadAll
. e8 y0 `! S7 v; {htmlwrite.close 5 b4 [* N1 d; T% F
3 l, b( \% e' A& s9 x
strTitle="生成的网页标题"
+ t* J4 L5 T/ KstrC   V' [* R3 ~8 \7 O+ K

5 {6 ~/ }0 T' z0 D' _3 b'// 用真实内容替换模板中的标记 : ~1 C2 W' H, i8 Y, C' s
strOut=Replace(strOut,"$title$",strTitle) ( @7 K: E. ^, J6 J$ f
strOut=Replace(strOut,"$body$",strContent) ( ^& V7 y8 Y3 P; N( r: a

# n2 M4 y) a  A* Q  X" Y8 s'// 创建要生成的静态页 6 x0 H: d2 R; p8 g7 W2 \
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 5 ~( v1 [6 F& I, k

- B" H4 s8 u7 C" `2 e'// 写入网页内容 0 \$ K0 i, R9 y% v
htmlwrite.WriteLine strOut ; K% P0 \# i8 \1 \5 Y0 T; w
htmlwrite.close 6 `- v' \' V! q% p. W

9 w% x* ^1 d% N! }# c5 sResponse.Write "生成静态页成功!"
4 \* _8 D# d3 n' I7 o& i9 U
5 N; T& r4 E  E7 X3 J'// 释放文件系统对象
" _  }  Y; c. y! }set htmlwrite=Nothing ! Z3 P. B6 c5 x1 n- o1 a
set fso=Nothing $ ]. S" S7 E2 x  C. u
%> " ?. [5 J4 G( j& H! Q7 Z9 n' j

9 [8 t+ S; @# j' D; k3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
  `( d" _6 u8 N' d. C<% ( ^9 u: L* g) a

! j- p6 M" W& i2 p1 q0 Z'常用函数
1 C0 A& r- I/ S9 O- m/ h'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
) q$ X, N2 S. d/ E$ f0 W* ofunction getHTTPPage(url) ; Y, ^7 o; K6 N0 \' v
dim Http   m! G) R& _8 j( w) P5 q
set Http=server.createobject("MSXML2.XMLHTTP")
( {: q- Z9 H+ k9 l# a3 H' Q3 i; jHttp.open "GET",url,false
+ Q0 u( r( W0 }1 _- \Http.send()
+ ?* {6 u& {7 b. J8 y! Hif Http.readystate<>4 then ' M. }" S1 q* w9 [8 I7 D8 @" F! f
exit function
* C, T8 Z1 f5 K- C4 C. ?end if ) V+ S- E4 f. h2 o
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 1 d8 K+ H" }7 h6 I0 v% J' T% M
set http=nothing
) R2 n0 g+ ?0 C- C, iif err.number<>0 then err.Clear / _) ?! A. j/ X7 F  i
end function
+ s& i, ]- d3 u6 v9 ], @( v: B0 s) V! H4 E# j+ J7 v
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
: `1 A9 \: ]2 M) m  V2 s1 N2 vFunction BytesToBstr(body,Cset)
- n& B' ~( `7 Edim objstream
  l' U( |: D& \set objstream = Server.CreateObject("adodb.stream")
3 E; h2 H: v+ V/ ]objstream.Type = 1
- {6 h( i3 m0 h# x" l# kobjstream.Mode =3 # z& P' Q! M5 W6 E* l0 j
objstream.Open 1 \& z3 X- i7 D7 @8 v  Y
objstream.Write body
# u1 v& ]8 `, X. v6 ?9 _1 `objstream.Position = 0
5 |: Q* s4 I. e. P- dobjstream.Type = 2 7 e- W) \1 b& S7 C' t
objstream.Charset = Cset
3 o/ Y3 \# M! h* C7 ~! x% v# I' a7 wBytesToBstr = objstream.ReadText
& k! N" x/ D' b) _  G) cobjstream.Close 4 O4 c' W( b: [3 }. ^8 W
set objstream = nothing ) G/ i6 r1 m& M6 `) N. R  e  c  a. l
End Function ; F' s# Z% X/ x
; r" M) n+ _. \# I

7 F- l. P9 R: D0 \" P, vtxtURL=server.MapPath("../index.asp")
2 e4 n  g- A+ B/ |
3 a3 p  F3 K# ]* c  a8 J1 B, LsText = getHTTPPage(txtURL) , H& c8 R5 V1 w3 o

1 d7 C$ W. R# Y3 }; SSet FileObject=Server.CreateObject("Scripting.FileSystemObject") & a/ ^0 v2 f+ O& W7 ]
filename="../index.htm" + r* W. n; ]$ c/ p
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ! e# d& z8 Z' M* V( W* D$ g
openFile.writeline(sText) - p4 C  z3 o8 F
Set OpenFile=nothing , x. ^2 B0 {0 ~3 ~

& f' _4 [' v8 Y( M%> ( T+ u: T6 X8 ]% Z
<script>
8 A' a9 X/ v3 Halert("静态网页生成完毕");
/ ?9 w* Q9 B1 F% n6 y7 U6 Hhistory.back();
! q/ {4 u" i) K4 O3 K7 E5 c' t2 q</script>

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