返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ; |( u# T2 F- M" C) }
www.aspid.cn的主站就采用了TSYS生成html文件! ( _% V3 E5 A8 a
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
3 }4 j# t7 j7 g; O
* L( |' c: P! }% u( x& V$ `" Y& ^1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
  ?, u) A& M8 L4 B+ y, ]+ E3 |filename="test.htm"
7 J* _$ e" E! V. K  Sif request("body")<>"" then 3 ?) m7 r) W. Q% F& M
set fso = Server.CreateObject("Scripting.FileSystemObject")
8 s" T3 ~9 w; xset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) . F5 C4 x& x' |2 \4 X
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
! ], b. J5 G/ \  Vhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 8 M2 m! [; [8 b7 _' ?6 C1 _2 X9 a" H
htmlwrite.close " C2 G2 i5 f/ y' }( v+ }. ]
set fout=nothing
8 n' c4 M& \8 |9 _2 [set fso=nothing
* t. d& w+ Z( N# y: jend if
' n9 \# C% J- g1 V0 ~+ p%>
+ T6 y6 ?, A5 W! r. [0 L+ s<form name="form" method="post" action=""> # y' a! j( U" i$ f0 B! o
<input name="title" value="Title" size=26> 7 P, J. w, f3 {3 r0 U# h9 s7 j4 v
<br> 3 H& D$ {7 C- ]7 f+ ]) Q
<textarea name="body">Body</textarea>
8 E9 {& d1 }) B' I! q<br>
9 i6 [8 L( l% `% g$ t3 N) z3 Z<br> ' ^+ J+ S, V0 j& k
<input type="submit" name="Submit" value="生成html">
& l) n: L/ L- X( Z# ?</form>
) l: L5 W* t6 y8 m. O& @2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.   ?& u  j9 P" M+ i
template.htm ' //模板文件 <html> : L, }$ B1 k" E0 r9 N* M9 M) l
<head>
) I# N+ Y7 k- u3 d! g. m' H<title>$title$ by aspid.cn</title>
& x2 v+ k# f+ e! m</head> - _/ S" F; z" W" J2 K
<body> : {4 e4 K" w( O" D6 s
$body$ 1 B% p4 l2 ~6 ?, b) T  n- k
</body>
8 ~4 m$ g2 I4 a4 Z</html> ?
6 C1 L- T6 J" a0 `9 Y; C8 r2 W! D
TestTemplate.asp '// 生成Html <% * {9 z* w3 q, {$ ?/ W* J7 j6 ~
Dim fso,htmlwrite 6 a  N+ `6 O- _9 M, A. [" A. N8 j
Dim strTitle,strContent,strOut
7 M: s- V# z& W/ n6 A'// 创建文件系统对象 6 a, e  q8 _. }# P! O
Set fso=Server.CreateObject("Scripting.FileSystemObject")
& n4 O  s+ u; m'// 打开网页模板文件,读取模板内容
$ i% N7 j5 d! @) P0 s/ d, O2 |  jSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) , ~3 i$ |5 P% x% I- H. F& D
strOut=f.ReadAll
% C: S4 G5 U% D) l8 Lhtmlwrite.close
- D$ @" a  k9 P2 _
* z  d0 I2 f" m1 p( e3 x# qstrTitle="生成的网页标题"
6 |* S9 O4 ^5 B+ D! @strC
/ \* _% q6 {$ u4 ~9 M
7 \! M! C9 q1 x. R) y* D9 c7 L'// 用真实内容替换模板中的标记 , I; N( P* l: M: n( p5 S2 E
strOut=Replace(strOut,"$title$",strTitle) 4 _, a3 E& p3 W) J! i$ G1 t
strOut=Replace(strOut,"$body$",strContent)
  p% o/ V6 D4 Q4 t  X/ J$ t5 Q% G5 O! Z5 J
'// 创建要生成的静态页
  i+ c; D, [" E- rSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) & t  x8 x  y- [8 M2 E

5 R% [. p0 J. K" |3 }'// 写入网页内容 " x3 V% N* P( j9 g
htmlwrite.WriteLine strOut ! ^( V! S4 w$ A' }# Z9 W4 z
htmlwrite.close
  S: M4 I6 M0 h/ i- V5 |% S% G; M  f8 M/ R) C! Y* r
Response.Write "生成静态页成功!"
6 V% e+ Y/ `8 e0 h8 L3 U
$ T  C8 i( W+ H) t'// 释放文件系统对象 * d% P) ]  U; y* u
set htmlwrite=Nothing 6 }9 m& \+ w! f
set fso=Nothing
/ [3 }/ E6 T* ^5 ?, E5 ^%>
  h4 G; S3 D: E+ Y) V, t0 u. t' w$ D
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
1 S& c; z4 d9 V# i<% 9 T4 u. W% o9 m9 y- ~& d
' y/ }- g! a. n+ |; i( j7 H
'常用函数
# h% `( w) t% x5 g: n; X'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
, g0 l3 A% S0 F! Q3 Z3 x# afunction getHTTPPage(url) ; z" i  M4 G  J
dim Http . K3 }! V/ k& }( Q0 k; C
set Http=server.createobject("MSXML2.XMLHTTP")
% v: |# |' _* O* t# Z) W4 q4 ]Http.open "GET",url,false $ m+ l# R+ D% l
Http.send() + |( ?; a5 D9 k; N! w
if Http.readystate<>4 then
5 h; X) K% w. l% z, W1 zexit function
5 w, f5 g, m+ e" s3 g# U% d& h/ Mend if
4 ?& k( `& N' X" }5 vgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
  O/ q8 ^! }/ g# P# D/ jset http=nothing
& p5 Y4 g0 `& e4 o' B8 e/ pif err.number<>0 then err.Clear
( E, Q9 l0 {; R# ~& v. vend function : t6 L9 \! z/ J  u* ^! B4 r. c$ c7 p- h
; S6 N! H- a+ o- w* r8 O- R8 l
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 * t9 ]( K  d" U: J# Q- v2 u0 W" ]
Function BytesToBstr(body,Cset)
( y; X: R$ W; W  l" i  ^dim objstream
8 T! A, ^. z7 M% Sset objstream = Server.CreateObject("adodb.stream") . ?: `! B+ L3 E' A+ Q
objstream.Type = 1 + U+ }( v7 @8 k+ D0 H
objstream.Mode =3
7 _+ c8 R7 S; t" T- `! ~objstream.Open * Y9 l# O% Y7 t! v$ g, Q
objstream.Write body ) F3 p7 h# ~) B1 a2 Z* h
objstream.Position = 0
, ~) K, |3 u+ v+ ~objstream.Type = 2
2 C# ]* h& l2 i# y6 a+ @1 F9 wobjstream.Charset = Cset
. F. @7 U8 E. b& C* ]8 c+ V+ j" dBytesToBstr = objstream.ReadText 5 c( a% V/ O. n% w1 P; ?1 k
objstream.Close
: B) G, _2 J5 t3 d% _' P" E6 ^set objstream = nothing + I. R6 d5 ?  s, }% k* A
End Function # z5 X% a- h; d; k

+ v$ u& @+ P, I
+ M0 T' O6 @/ n8 s- `txtURL=server.MapPath("../index.asp")   u- U  d! D) ^+ L$ K5 E) `$ v+ h
8 H3 y2 [3 e3 g1 [8 E
sText = getHTTPPage(txtURL) 7 Y* }4 d% {6 l$ I9 f1 V* T7 d
# [2 K4 b0 f( z2 M1 `
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") + U$ [, ]# S# F8 J9 i; D
filename="../index.htm" , I9 `# R. s4 v+ M% V( @
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
0 T" r2 {/ |6 S$ Z* mopenFile.writeline(sText)
% v2 R; t2 h5 O  K. s3 b( C( ~4 tSet OpenFile=nothing   s3 M# b( A7 z
& V$ v9 }9 ?7 e. y9 p
%> # n) G* T5 f9 ~% N' Y! g
<script> 5 M6 ?# a% m6 w  c* [
alert("静态网页生成完毕");
/ v) M$ n; ^- _$ M* T' d% ]( ?2 Lhistory.back(); . b+ ~* s" o" h7 S& N4 W
</script>

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