返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
/ F8 e, c) }* Q% q/ ], Lwww.aspid.cn的主站就采用了TSYS生成html文件! $ M/ z/ D+ G3 L
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
' S! V7 Q* m; h9 E2 r
& K0 ?; d% Y$ M: h5 q& g1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
0 c# v) T; M/ B; }  zfilename="test.htm" # U5 x! w# r9 C$ P
if request("body")<>"" then 5 u* Z3 w. q8 |1 d
set fso = Server.CreateObject("Scripting.FileSystemObject")
4 ]& e. F) r! s& n* U# Eset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
) J! I2 i# T0 N* k5 \htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" . {1 \$ p( Z4 k& Y" }7 \
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 2 f* e5 `3 E4 f2 |$ ^& N! e" k
htmlwrite.close
* H7 @9 C' ?5 Q1 Oset fout=nothing
2 x4 {* P  i$ `0 O, \' Q* Xset fso=nothing . Z  {; H# a; e. }. W& L) t
end if " X; u; v5 I5 @
%> ( `  P1 P) @$ n* c
<form name="form" method="post" action="">
3 I4 N# k0 {* O+ B+ p$ I" e2 j<input name="title" value="Title" size=26>
% V5 M+ S2 e" y<br>
& F& X% |1 E% q* r2 M( c<textarea name="body">Body</textarea> & Q; |! y+ S# c- u+ [+ E# K
<br>
$ f/ x, D5 E6 ^  [* U/ ]7 g, b4 Z<br>
& Z! \; P- N" e# w+ P<input type="submit" name="Submit" value="生成html"> 4 Q! M5 x% F. G
</form>
8 `2 E3 g7 R3 @* L+ q- W2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
* |  ]* p9 J. q+ c) V( Ytemplate.htm ' //模板文件 <html>
" A% R! J2 R1 p: c+ I$ p8 H<head> " P% M+ q& C& v7 |7 E. f! b
<title>$title$ by aspid.cn</title>
/ L* o& ?1 h2 r</head>
3 @" A) S1 X* S- u) S/ {<body>
0 L7 X1 g+ d* s: F6 G; [$body$ 1 J% v/ l, b; J9 ^: ]
</body> 2 d: r' n5 c0 J: U- `$ Q; ?
</html> ? ' A$ s) k: G6 ^2 z# I( G& A$ N

/ T* V' n! h0 T! j% `3 l. n& ]TestTemplate.asp '// 生成Html <% ! F' t- ^, t' V
Dim fso,htmlwrite
1 V6 B; i7 Q  E; `Dim strTitle,strContent,strOut 1 l- z  W* V9 h& W
'// 创建文件系统对象 ( P5 L1 ~4 U. w6 y" I9 x
Set fso=Server.CreateObject("Scripting.FileSystemObject")
- U* Q$ `- N8 _: H'// 打开网页模板文件,读取模板内容
0 x# {/ r. N! J4 RSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
: p# D2 P7 p  s2 M- F* hstrOut=f.ReadAll ; ^; J# O/ @" k5 z: ?
htmlwrite.close # ]5 l- g5 h7 ~
+ D! ^, R' {# S* U( p' }; }
strTitle="生成的网页标题"
9 a; `$ V0 t/ m6 }* hstrC 3 m" l# O" @! a  s. e$ \8 Q

% l6 ^& G4 W) n/ r. R  ~$ _'// 用真实内容替换模板中的标记 5 u# K* p* L' w' e, Z# a
strOut=Replace(strOut,"$title$",strTitle) 1 ]) X+ n) p0 S* q0 U9 b+ x0 I. g' \0 z
strOut=Replace(strOut,"$body$",strContent)
- d% r0 j" I. Y: w) l! M. V( e( E- V5 j' z1 h7 [9 c' n
'// 创建要生成的静态页
. [9 E- ~) h/ k, {/ x2 QSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) + S  S7 f) g$ P# g

/ f, _0 O0 o% O- x& X3 B: u'// 写入网页内容 4 U- g. ]' z7 {+ n. h- \6 z
htmlwrite.WriteLine strOut 0 L" C2 f* D( q( G
htmlwrite.close
( I5 T& D4 j  {) ~/ t
* n, h4 |0 S- P3 D6 yResponse.Write "生成静态页成功!" ! y: }0 L7 i2 X9 D# @( }* s+ o
4 H/ v$ ~; L1 T, q4 e7 x# D+ k
'// 释放文件系统对象
3 k: X/ t: G5 ]( G( Fset htmlwrite=Nothing + {& r. J$ s/ R$ F# j* y; l
set fso=Nothing " G  R  Y  Q1 ]% I6 n# g+ ^& U! b
%> ( Z/ [. f$ D3 {& |1 q: d1 k- ]+ l

3 R) g2 H  s) E9 }- {4 G3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
0 [1 K) f  `* ]8 S  ]% y<% / O6 n4 B1 Y1 }  C' D( P
; e7 [1 x8 S8 h9 J% k: a
'常用函数 % b0 ~3 Q) f" @, q/ U! C
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 + e2 Q; O9 h) Z3 _& \" K; v
function getHTTPPage(url)
/ N( v5 A0 \% A$ G) q; D  g6 d# ddim Http 8 h7 ~: l7 ^$ R' x
set Http=server.createobject("MSXML2.XMLHTTP")
6 c+ B, k% m( v7 \5 a3 g- kHttp.open "GET",url,false
/ f8 M# ]# s& Q% w; bHttp.send() 0 y( A! W6 N* k3 K3 h* j
if Http.readystate<>4 then
% q" R8 V0 W  lexit function
0 N1 Z! A8 x0 c) P0 A) Zend if
, p% z0 Z: {% l4 N  P  N7 ogetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
1 R. B& x: Z0 M! E8 M* G4 wset http=nothing
$ j( T, U; Q! C, h+ A6 f4 {if err.number<>0 then err.Clear 7 U& a: T0 X1 Z1 x
end function
( m- @# L' Z, j4 D6 e) N% {  E" H. r
& H  [, o$ D6 f( ~! o, o7 O'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
* R' T# |6 M: h" @: eFunction BytesToBstr(body,Cset) - M( L% m% O/ o$ L0 @
dim objstream 0 J: G' \1 U( V0 _/ g' j
set objstream = Server.CreateObject("adodb.stream") ' V0 L3 e3 \$ O1 l) Y1 a. B* F
objstream.Type = 1 & I: t& e  H2 X1 v; g( J
objstream.Mode =3 $ p: W# g& n% |
objstream.Open
6 q. l6 o* B" F; N9 h0 X; _objstream.Write body ( K' v: E* J, I; C' `4 b& W
objstream.Position = 0 ! O/ S7 n* A' R) G( t8 Y' L7 `
objstream.Type = 2 8 x5 r6 t9 x8 ]" M* u
objstream.Charset = Cset
/ ?0 i+ I, e0 S9 i" G/ ~1 w7 ~2 n" YBytesToBstr = objstream.ReadText % A% T; t8 h) N. W  K
objstream.Close
% H; n3 G8 B2 z; wset objstream = nothing # i' x9 m5 f, r3 W- H" U5 P
End Function
" @( N" f6 u$ [, k* M
" C, }3 h3 [! ?  `; @3 P" Q
; {0 B' A  V$ I& a( H: e# f& k# v+ vtxtURL=server.MapPath("../index.asp") - W5 M# ~; u- ?" p6 ~/ c3 _
; x: ^4 ^) Z; o/ [$ g
sText = getHTTPPage(txtURL)
6 s. T) n1 a4 S5 Y
" S) d/ _1 I! |4 MSet FileObject=Server.CreateObject("Scripting.FileSystemObject") " B/ [2 p8 G- D: X2 W
filename="../index.htm"
+ v# I& Z1 {4 QSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
5 h  F6 S6 Z0 M/ aopenFile.writeline(sText) $ L3 L; o/ N6 q# N
Set OpenFile=nothing 7 `  K: H, V3 X: O7 w

, O5 e$ K8 v2 J) e%> 3 m5 ?2 ]( T3 L9 U4 w, U6 X
<script> $ x0 v4 A8 U" t( U( \0 F# Z8 R2 P5 E
alert("静态网页生成完毕"); 9 ^2 o/ H( m3 ^8 Z: e" x" k
history.back(); 2 Z( c3 Q( _* e5 W8 Z
</script>

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