返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
( F3 b6 {0 K! T& Q0 Awww.aspid.cn的主站就采用了TSYS生成html文件!
, B0 P: z  F1 M6 x  T+ O所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
7 a) ?# j2 d( g8 U, Q  R
* Z4 p- v5 s& Z7 O+ b8 k' P1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
$ b* ?% K5 L% l' n( X0 e4 gfilename="test.htm"
  [% \: J, f. m, ^1 @5 {if request("body")<>"" then # W3 ^0 X( n7 z' f( c
set fso = Server.CreateObject("Scripting.FileSystemObject") 2 Z2 U$ g$ F9 R. s% u1 @! I7 \$ x9 q
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
- ^* U6 p8 i- U$ c  r, ?! zhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ; R  g4 I( L8 i4 s
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
* @' I7 ~: ?3 Y) E$ G5 B" jhtmlwrite.close 9 `- h6 i. z$ X0 N+ C( [. V+ ]
set fout=nothing 3 C2 A, Q6 `6 X6 z" p( e
set fso=nothing
- c( T8 U5 H( w  q% U" e3 kend if 1 n3 c2 F. e5 z. j& l
%>
6 a' F! B; {; w# ^<form name="form" method="post" action=""> 2 W# j/ f6 T4 e7 ?/ [  l% [7 u
<input name="title" value="Title" size=26> + _8 D4 o: S; X6 R. _( J9 E* e( e: ?2 A
<br>
" @" U2 P/ C) ]8 {3 a* T' m<textarea name="body">Body</textarea>
& L+ a0 }' y* f<br>
% K6 A2 E$ h: H2 K( K6 J<br> & G) _' i: ]* N  k
<input type="submit" name="Submit" value="生成html"> " s9 K1 t# R* N8 Y* C, u/ [: |! S* z7 U
</form> # Z3 ^) M( A# }; y7 k$ X& G' D2 T
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. & W7 u# a" O) c7 v' y5 K
template.htm ' //模板文件 <html>
- _) d8 {$ s% \0 H& h% X6 e<head>
; G7 A! C5 j' h! P( ]6 Z<title>$title$ by aspid.cn</title>
% Y: A4 a2 j" n1 x</head>
* P5 `, c8 F2 c0 B, y; O5 |3 U) n<body>
: b; Y* K9 z# u+ }' H$body$ % C; {9 i) z" \# N9 t$ ^' d* `$ ]8 R. s
</body> # k' ^0 e% ?2 q* a% D  Z, u+ Q
</html> ?
. n* q7 N. v  j/ [5 l! L
% L1 h; C8 p# H+ y' B+ M+ Z4 W9 ZTestTemplate.asp '// 生成Html <%
8 f# Z& w# N* d+ Z& J/ d7 P1 sDim fso,htmlwrite
& H2 L% s* E) DDim strTitle,strContent,strOut
+ ~" |; r" t7 l'// 创建文件系统对象
$ E% \1 J- v. F% f8 l, N: ySet fso=Server.CreateObject("Scripting.FileSystemObject")
1 f: r% x5 d. [* @8 W'// 打开网页模板文件,读取模板内容
, B6 |5 w/ ]4 s6 @Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
6 Y, d0 Y4 J& L' W. x' fstrOut=f.ReadAll 0 i4 I+ T1 P6 w" {# n: A
htmlwrite.close
% N- N- W: Q& G! A% z- n0 F4 K6 L9 G8 M) c& B  e, x" Y2 C
strTitle="生成的网页标题"
2 {( ~. B- K0 }strC
) ^' c3 G- s4 ^" n7 S; E9 q; }$ L: ~( T  O
'// 用真实内容替换模板中的标记
- [" V1 A/ d. e4 OstrOut=Replace(strOut,"$title$",strTitle)
; v9 K' W& m- y( b3 _  |strOut=Replace(strOut,"$body$",strContent)
! ^/ A: P, H7 x3 j! _; {
6 R" b9 v* W+ i- S& o'// 创建要生成的静态页 ( s! z/ v1 N/ u
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) + Z' _1 Y; u  M: a7 R" g

) D5 U4 Y. w6 w, Y2 v'// 写入网页内容
  m" X3 j) b( W$ T5 fhtmlwrite.WriteLine strOut 9 @6 A7 J+ w( S, x$ I
htmlwrite.close 6 ]8 e4 P/ Q" p8 l4 p5 r" l+ u

# X' Y7 K" H6 a, g* D5 H) f, @Response.Write "生成静态页成功!" 8 Y/ W& N) F, ?; {4 R. E- W

% r8 l+ b0 t7 l* ^" h'// 释放文件系统对象
& e0 [' U9 N# uset htmlwrite=Nothing 1 x, n& ~9 c) h) ]8 x% p+ ]
set fso=Nothing
: W* E6 D4 x2 E$ d$ C%>
" r0 h3 J# [+ i' U! P) x  e/ o$ _
# S! d/ \: T% F) B# M3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
- g$ s* {* U- f7 q) J% b<% 5 m4 w" N' J1 `' p

, V3 Y/ H+ _8 i- h( _'常用函数
& |( K" ~) V9 f) t( U' w4 R'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 / ~* l6 I4 ^$ p& `) y5 V8 f
function getHTTPPage(url) * t- w! `$ h  Z& _; ]  o3 \& S
dim Http - s) Z0 o4 c' l+ r1 z0 o$ c  G
set Http=server.createobject("MSXML2.XMLHTTP")
+ B/ q0 {* p' w6 Q! YHttp.open "GET",url,false 8 A( Z2 Z1 M) \
Http.send()
% l# j0 I. t  [9 dif Http.readystate<>4 then
& h- p+ X% V2 l2 C% o2 Q. Fexit function
: i6 i6 T4 O  E1 Kend if # M  ?4 \2 _/ I4 l' p% N$ B/ f
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") ( Q1 z% x7 H+ V9 @
set http=nothing
& w/ ]. j2 |% v! J% Wif err.number<>0 then err.Clear
  j. Q; n9 M6 {- l, b# ]end function
/ K$ R+ m+ Q$ r& i2 @1 Z4 u2 K) K; }  |& e2 j. l) s6 x
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 8 a* s% x& M& I
Function BytesToBstr(body,Cset)
' L( v0 O7 I0 ]. `% z  Gdim objstream
% ]; i& ?) U& X# o! P3 j/ y. u8 sset objstream = Server.CreateObject("adodb.stream")   L7 K; ^, o4 w; x, q* M" P1 A
objstream.Type = 1   q2 @# `4 \& d  q$ T7 h+ W
objstream.Mode =3
5 [% q" s! K. A' U/ T' Mobjstream.Open
! `% `+ T# K1 d) Dobjstream.Write body ' f: V4 ~! u; ~$ Q5 n, G) p" G. H/ B
objstream.Position = 0
1 m" s. S: G% e$ O9 Dobjstream.Type = 2 6 x1 O' E* U, h3 @3 V7 }/ I
objstream.Charset = Cset ) X. X- m. i1 O+ G8 s+ w; o% v: v
BytesToBstr = objstream.ReadText . b0 b9 G8 I5 Q2 O3 f
objstream.Close
1 I+ G- n1 W. z# nset objstream = nothing ) v; Y" q2 _1 T2 B. Q
End Function ' ]2 W. a- j6 L% f
" g% a: h: F+ ~8 N: k
; e/ k- C, t; q4 |8 M
txtURL=server.MapPath("../index.asp")
" T; L/ V2 L4 C5 L8 o- |
+ G% K/ b: @9 hsText = getHTTPPage(txtURL) 2 w$ k' C- {* F+ f; h9 V1 L

$ B* {$ A8 ~0 D7 hSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
7 F. `) e7 j% h$ c0 gfilename="../index.htm"
& R# P- ]. H8 ]- a" ^Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
, Y# y, l; t1 P; d) a. I4 ?openFile.writeline(sText)
" ~! s" c3 ]+ A; ySet OpenFile=nothing
! e$ }, U6 c& J% B! C  _' ]' I) i. p7 M8 K- j: l
%>
5 Q0 n- L5 f' r3 _5 M<script> ) N  G3 Q; I1 d( e# I
alert("静态网页生成完毕");
  p5 z( t6 [4 C3 p8 a& Bhistory.back(); 6 c7 _  f; m3 D% F5 R6 T6 C
</script>

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