获得本站免费赞助空间请点这里
返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
; o1 t+ ^( s) X, m5 H2 d; o$ \www.aspid.cn的主站就采用了TSYS生成html文件! ; t7 z! ^1 v+ A" I
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
  x8 a' V9 s2 G/ I$ ]# t  O
1 G, P7 p; }3 c" g1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% ! J5 S4 J0 g8 U" H" B
filename="test.htm"
1 {# w! s* X$ f9 f' ~& }' f7 eif request("body")<>"" then " c7 t9 v$ r& p( j
set fso = Server.CreateObject("Scripting.FileSystemObject") - E- S9 [+ y5 z3 r7 ?3 P0 \
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
, i5 y9 A3 l* _% y  ihtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" $ \$ q0 ?+ [3 i, C: Z, Y8 `) _) V
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
# B% v- j/ r% ?' N+ uhtmlwrite.close
) G: h4 _' {4 G  n. D) `set fout=nothing
1 g+ y9 V( W1 tset fso=nothing / `' F6 M3 k. R
end if
4 g1 t3 _5 M6 J: {2 O%> 4 @: n! F7 d& e# T& T3 ~
<form name="form" method="post" action=""> ! Z( I6 r9 \3 `1 V; Q4 H+ m9 n2 K6 |
<input name="title" value="Title" size=26> 1 X8 h$ i  Y# k
<br> * {; d- e- y: k
<textarea name="body">Body</textarea>
* i. \8 ~9 K, H$ B( C7 y' R<br>
- ]+ F+ V! J+ H  H- `6 s; s<br>
7 E2 P5 B" e) x5 g- }$ T<input type="submit" name="Submit" value="生成html"> & E" s& D0 M/ u. C
</form> $ s4 B: @- j% V9 v$ W( i/ s& U
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ( E+ |# d; o& p( r) V
template.htm ' //模板文件 <html> 1 o/ I. d( O% k+ c' \
<head> 4 N, p# R4 K" L6 t1 m
<title>$title$ by aspid.cn</title> + i* ^- C- `. {* R+ Y7 c
</head>
3 o  j7 L( t* I<body>
0 S2 b. q( |, x% t$body$
8 l7 l# \5 V6 m9 \& j) R</body>
7 w, }/ U; ]+ \" p8 b</html> ?
8 d( ]% S% _  y2 l8 [3 v$ G
, l2 g5 [3 ]7 v% x0 C2 B, P. iTestTemplate.asp '// 生成Html <% 6 z) v% S( P* L; ]9 E
Dim fso,htmlwrite
1 T) f& F' J0 S1 t4 T( QDim strTitle,strContent,strOut
- g) B. R! P, G9 x0 H; d% h'// 创建文件系统对象 . W' [) Y( j8 G, n
Set fso=Server.CreateObject("Scripting.FileSystemObject")
% G# a  q1 |2 T! p6 m0 Z% A! g) H'// 打开网页模板文件,读取模板内容
" @' y. k) y5 |* ZSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
9 T8 {" l1 y; H; F- D# TstrOut=f.ReadAll
4 ?& |) E4 [' c$ n. A' r' `' dhtmlwrite.close
+ s7 D. {5 Q2 [
% Z! ]; }' X/ A' H6 qstrTitle="生成的网页标题" - x" [8 c2 b+ a' N8 D3 C4 U* {, ^
strC ! z% a2 D! A) y8 l2 `! B

# K1 F/ z( T3 X, T6 m  N& F'// 用真实内容替换模板中的标记
- k5 v3 E% K; L; B* e* }. [strOut=Replace(strOut,"$title$",strTitle) / l; P$ W3 }( ~7 r
strOut=Replace(strOut,"$body$",strContent) . r$ R( N) b6 W$ Q. m1 g
% C- g9 d+ C4 Z9 i7 l
'// 创建要生成的静态页 * I3 w7 c) i/ B/ h# s0 @2 T" f8 V
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) , Y8 b; L. u- S) a4 D& E" C
+ E5 w1 G2 R+ l4 H; d& W
'// 写入网页内容
+ t9 \" S8 Q, i6 D3 Qhtmlwrite.WriteLine strOut
, n: ]7 h" v$ ehtmlwrite.close ! q: E# u4 h1 `7 i' @

4 {8 ?" F5 t$ a+ D! I2 @# U/ aResponse.Write "生成静态页成功!"
2 q/ j! ^( q8 y; a
6 n0 m$ w+ d9 l+ y'// 释放文件系统对象
( t, v  |* h5 L/ O: fset htmlwrite=Nothing % D8 P# l; i2 Y! [$ @; t0 G
set fso=Nothing ; r' p6 C3 l4 ]+ x) T
%>
' z" B, P) I* i3 F( G
' H4 m+ x" X+ f$ T8 m, T$ S& |$ a3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 4 h; R! d2 z3 [- [" \: E2 e  ^
<% 6 C  G0 s$ }( C
2 R: Q! f7 |  Z- E4 z
'常用函数
' s5 ?) k! ?) s$ ?* }8 s'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
: I2 Y7 F+ m/ Pfunction getHTTPPage(url) . `- ^7 c* c# u2 ]
dim Http / F5 k. j* k6 K
set Http=server.createobject("MSXML2.XMLHTTP")
- r5 O6 a9 ?& rHttp.open "GET",url,false
+ i' Z2 N8 Q( ]Http.send() 0 y+ v; M: u  `
if Http.readystate<>4 then 4 C' r- ~: c' [! i$ B- ~
exit function 7 U7 m" N0 G7 p8 Y: i
end if
  C. F4 W: a# M5 [6 fgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 0 X" _& T6 ~; e6 R
set http=nothing
1 ]+ G1 b6 x; i% a/ M! E8 d1 jif err.number<>0 then err.Clear
6 Q) c$ Y5 u6 W. \3 yend function - i# E0 J, q- R. t+ h

* u% d; b* Z% [5 Y( S! P8 i2 Z'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 3 h' h+ r0 m: L  O# F$ t5 `: ~- v$ }
Function BytesToBstr(body,Cset) ( k$ z/ l5 x5 G
dim objstream
8 D& j2 y( p2 _% z* mset objstream = Server.CreateObject("adodb.stream") 6 Z/ {2 e% E0 A0 S* @; t
objstream.Type = 1 6 _* Z) z7 a# m" C
objstream.Mode =3 1 ?" W/ ?: m9 _5 F
objstream.Open
8 P& f; j  X* xobjstream.Write body . r4 M9 d' b3 A! x) P
objstream.Position = 0 0 `2 O- V$ S4 L. m* W. G
objstream.Type = 2 ; z2 U3 T% Y1 l/ s: B# d6 d
objstream.Charset = Cset   s+ [! j6 F+ P. K  ~% _3 V% b0 y
BytesToBstr = objstream.ReadText 2 g0 q9 ^4 K( G( y' a1 a
objstream.Close 6 M" z7 D9 W; a& y9 z
set objstream = nothing   |; P) b% g# p9 O: |
End Function 4 z+ u, w" R  x! N, q3 B

9 m4 t8 j  ~, a! W3 _' K1 [4 C. }. n+ n+ A
txtURL=server.MapPath("../index.asp") ) o( X, V7 ?: g1 r, S

: \  |1 S8 _9 a" psText = getHTTPPage(txtURL)
/ n6 A3 T& }7 G. F( a8 T% B2 Y% ~; m6 c. B" D9 z2 j
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
' P) i& t* d7 N1 l8 s9 A2 H. kfilename="../index.htm"
) b1 H2 t5 n# ?" K* r- B. @Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ) g, B( X0 {5 X% [: f) `
openFile.writeline(sText)   e1 v  z! a, m7 H
Set OpenFile=nothing
- A0 a1 p/ b3 n& |7 m* l* }
1 }5 K* o4 M! h+ s# C3 `2 U%>
' @* q' h' C$ Z  W<script>
* @  p; J, T7 V. X/ Yalert("静态网页生成完毕"); " _0 U, q. @$ y) [9 i
history.back(); 4 k) a; d& E9 L2 p+ E
</script>

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