|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14443
- 金币
- 2507
- 威望
- 1647
- 贡献
- 1455
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
* W( Q' y6 G7 s5 p像www.aspid.cn的主站就采用了TSYS生成html文件! ( r* T5 d/ r/ k% z. _& x
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
1 {8 x& V8 p0 q2 z; d
( t$ y* Q j/ v, D2 I5 }7 t1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 0 j7 y5 t4 \' N
filename="test.htm"
" `% U+ ^! |# K6 S# Z$ @if request("body")<>"" then
. J3 m2 ^+ P( N. n& mset fso = Server.CreateObject("Scripting.FileSystemObject") ! x7 \; q9 H; X1 y3 K
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
1 l7 E) P2 N/ }+ M' @" Qhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
& {; C3 o9 ~0 q7 H! z' a* bhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
5 Q9 `' f. H& P( _: ehtmlwrite.close
$ ]* ?5 q; e% K8 q5 ?' p: |+ ?set fout=nothing 9 Y: N7 V# s: `) M& Y8 Y; O# r
set fso=nothing
! f- D& |3 ?% d9 zend if , b# }# K& T# p! r/ n1 r" a
%>
: F, F) f6 b* a4 ^3 }1 Q9 I( x" C<form name="form" method="post" action=""> - `9 M. y V( d7 s0 O' ]
<input name="title" value="Title" size=26> 4 U& G4 D; x2 V" w1 M/ x
<br>
, }* |$ \0 I4 ?* \ O! p, @5 k, P<textarea name="body">Body</textarea> 0 H5 |, f& x$ y2 m- h
<br> 5 O+ x' a- p: }1 s. N8 C+ v
<br> 6 r) f+ d5 r$ {( k" @* W) i
<input type="submit" name="Submit" value="生成html"> * ]& v6 t- E' ?( w$ S) r
</form>
5 \ h; s# _1 B" v2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
1 o9 A. T5 E% ]9 D! ~template.htm ' //模板文件 <html> ( H6 W* P' L4 f$ ~1 K
<head>
. w) z; y: j1 ]! ^0 B<title>$title$ by aspid.cn</title>
$ @7 u$ U$ o0 _, U) @</head>
+ h2 Z6 L, z/ l1 g<body>
8 S+ w. T5 y4 }& w9 P$body$
6 y0 U3 J) W6 R- X/ ~. q/ w</body> 5 d" [. B3 q0 k) W8 i
</html> ? $ L/ g/ B: Z! p" F! k/ S. p0 N
2 q! t& E2 t5 g' i" b ]6 Z
TestTemplate.asp '// 生成Html <% 8 R7 G( J6 L: K9 I+ l# [% K
Dim fso,htmlwrite
, ] Z2 i/ m/ V% tDim strTitle,strContent,strOut ) F- G2 B" } W# t4 o
'// 创建文件系统对象
: P3 Q9 r! T1 M# d5 BSet fso=Server.CreateObject("Scripting.FileSystemObject")
- r; s% K. c4 l/ x, C) N'// 打开网页模板文件,读取模板内容 / z3 j1 Q0 C: R$ B s/ T& L
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 9 ]+ N& j9 ? B4 H( d* {
strOut=f.ReadAll
% J+ f4 @. k% e8 {* hhtmlwrite.close 5 j9 {+ B% a" j& P
, g: z/ I/ U* I' g9 b/ _2 u
strTitle="生成的网页标题" # E" s+ h) u9 S6 \6 {- D# \
strC
# y2 m5 J. W( R5 ^* r8 G2 W% }/ R0 r8 ~% D2 U$ v: S. n
'// 用真实内容替换模板中的标记 4 g* R0 d, ^ q, z2 a) |: C
strOut=Replace(strOut,"$title$",strTitle) " ~5 U, \: {/ R8 k) q0 d* `" ^5 I
strOut=Replace(strOut,"$body$",strContent) $ P% R* o5 e/ ^: q* c
$ L/ r% l& w! z) G8 q
'// 创建要生成的静态页
: V. J- o; }" PSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
( N& v+ I+ D. f8 f% S/ E3 ?/ u+ o+ S$ B- }) |8 t
'// 写入网页内容
, c' m9 r$ d) zhtmlwrite.WriteLine strOut
; m& ~' M6 b- Q9 J) u1 shtmlwrite.close 4 x6 u4 ]) N1 P& f
) w: P6 ]9 u/ N0 {( ~9 E5 Q
Response.Write "生成静态页成功!" : g: g# ?8 E3 L
! H% G" j3 P$ X% w$ Z: R'// 释放文件系统对象 2 Y" ?" G+ B. G" v9 e4 V
set htmlwrite=Nothing % c# J$ t! v- H& R1 P3 ]# z
set fso=Nothing 7 N# f) O6 K) w6 D7 |
%>
# I& ?) _- l( P0 H
4 o; v- ?! v! E0 y! t' Y9 Y3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ! E& S6 x' T8 R4 Q
<%
& Y! c3 g; g0 l8 \
6 [0 ^' e9 B: ^% ]2 S& Y% T v'常用函数
7 e7 f% T# P0 \) I. J'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ( g+ N, L. W3 ]5 s# V* D; F. b
function getHTTPPage(url)
0 b- L9 h1 J& Kdim Http 1 s+ m) {+ Y g! [
set Http=server.createobject("MSXML2.XMLHTTP")
, l* d1 A1 d0 t8 m6 z2 a4 D2 O4 `Http.open "GET",url,false
2 o* h8 `0 u7 A) V/ oHttp.send()
( k$ f5 R, H7 Aif Http.readystate<>4 then 4 b7 A0 D4 B9 G4 r5 r
exit function % H5 k0 K$ Q, c6 V6 [! _2 h2 |
end if
7 d1 H( k1 ~0 k' wgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
m0 s1 \$ ~" j, L2 H5 kset http=nothing
* c0 X* i' Q+ y' v. Hif err.number<>0 then err.Clear 6 g6 I8 v/ ?8 c
end function
4 P1 }7 F% y7 c# d5 g x0 B+ K
" b% G! h% c8 I, \'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 ! O- }' C$ S+ H8 r0 V9 Q7 g
Function BytesToBstr(body,Cset) + P% Z* Y8 t* K/ \; [1 R
dim objstream
/ H# _5 \2 \. w0 M9 g) j/ m6 zset objstream = Server.CreateObject("adodb.stream") - a: S* ]# n5 v* T9 k, z. M
objstream.Type = 1 ( o7 k7 s' u9 `0 C
objstream.Mode =3 6 o5 w( ?" l7 e8 i1 F/ ^
objstream.Open ' s, }8 ?9 @' @. S7 K
objstream.Write body 7 R% P& ]8 o1 g( L: \. d6 E; L+ P G
objstream.Position = 0
0 l. A. }: ?' E+ }5 ?5 E/ Fobjstream.Type = 2 4 _8 l+ t h% }/ u& v$ T u
objstream.Charset = Cset 4 g9 d; [0 n; f& i) m* h* {
BytesToBstr = objstream.ReadText
7 N2 ?6 c8 {' Y+ Eobjstream.Close # f$ ^2 e' l0 }( p% ^; O
set objstream = nothing ( [3 i( T+ i3 P- c; x
End Function
# p1 C- T7 J; [/ Q/ ~( y
& \" F" F2 o, Y& i) A7 |" w4 w* ^ A. O# j3 e# L7 N6 J M! C
txtURL=server.MapPath("../index.asp") ) O4 l) I1 C/ C# o
" A4 R9 R+ n' H# ~+ b" s. ssText = getHTTPPage(txtURL) 4 ?6 h$ D! r" p* }- {
# G6 c) `7 Q" Z+ G1 j. T& p
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 7 h1 V* P, i2 D1 M
filename="../index.htm"
5 E! w0 K2 U6 w8 [; FSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
& H& @/ r, y3 |% q; g# l* oopenFile.writeline(sText) , I4 u$ S2 C, E6 d6 t# B
Set OpenFile=nothing
1 I, J! ^' x3 k2 y" h, O- q5 Q% O( m; j6 h- @. ~, d5 K! P( f
%>
/ ^6 t* `9 a, ?5 y+ |. S& C3 J<script> - d% C7 B1 J& B9 k, z
alert("静态网页生成完毕");
# Z8 [! Q1 t V9 H# }/ Lhistory.back(); 6 x, O0 K1 |! b
</script> |
|