|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14419
- 金币
- 2495
- 威望
- 1647
- 贡献
- 1443
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 2 f- F- @" e8 C" u* H$ b3 k
像www.aspid.cn的主站就采用了TSYS生成html文件!
, j6 S; ]0 [2 V" h9 Q$ x1 l所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. : u8 F( G7 D* K K& n4 u
M$ y4 f) x. T4 y$ _7 f4 H
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
5 S! A6 X& o' X4 Hfilename="test.htm" 9 [2 g( d; x; s: ?# R+ E7 a
if request("body")<>"" then ' B& }: b/ M2 ?7 R
set fso = Server.CreateObject("Scripting.FileSystemObject") : @) K* ]( L q7 y# G) d l- S
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
6 E" t! `+ L2 shtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" / e% ~ m$ a2 H- ?# t A6 j$ i- \
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 6 l5 u; ~8 {9 g' k
htmlwrite.close ; s% U$ D2 E1 M8 C9 H& u) ~
set fout=nothing
# c9 Z' o( `* Y) d: ^$ sset fso=nothing 7 ~1 Y' ~1 ^5 m
end if 5 u; N, S9 f- w1 p3 x$ R
%>
5 t& P P* i$ c0 T" g1 Q: B' |9 O<form name="form" method="post" action=""> $ b* \' ~. w$ G: P
<input name="title" value="Title" size=26> ( d, ^/ j: Z, d0 {; _
<br>
( j8 G9 I* t: b- D: n! n8 r! A<textarea name="body">Body</textarea> . g" N. O5 h; @2 l5 E- G2 Q
<br>
. `( p6 s, R, S- ~4 n& S) [<br>
) w3 ?4 p2 |: R( p _4 e<input type="submit" name="Submit" value="生成html">
4 T8 _& f; o# G2 }, O9 W$ T5 q</form> $ P. R X8 s k3 O2 m
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. * f- ?6 U$ U* N
template.htm ' //模板文件 <html>
% [8 B3 ~. Q# |<head>
" V; O" B' ? |: F3 p/ Q h<title>$title$ by aspid.cn</title>
- r8 X) B# a, Y1 `& R</head>
4 t4 w/ Z* i1 A% ]<body>
+ H3 s8 N$ U ?& e$body$
! w8 j0 c; j, Z7 \! R& [</body>
6 @( R3 f2 K/ b1 H( r8 ]) Q</html> ? + i4 |: H$ u, v: V! H
6 N/ i R8 g$ KTestTemplate.asp '// 生成Html <% 4 ^- l0 e: x0 }: ~0 O
Dim fso,htmlwrite % T L; C( E8 u; u+ [6 Y) i
Dim strTitle,strContent,strOut 8 O L7 {2 N( T
'// 创建文件系统对象
$ l; e* W) u U/ vSet fso=Server.CreateObject("Scripting.FileSystemObject")
$ |* @- Z' M4 A3 m, A1 i: C'// 打开网页模板文件,读取模板内容
' e! j% O; H7 l- o- JSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 7 t d+ @' s* k+ A2 K' C
strOut=f.ReadAll + I+ u8 y0 d$ T2 `8 \! g1 x) f
htmlwrite.close
9 \0 f+ l0 _; l; H+ I9 X$ D# T t( d5 P, B
strTitle="生成的网页标题"
, t2 V8 a3 n6 O. LstrC
. D/ O6 a* ~* ?4 [
1 I x3 A: S5 p4 h3 ~'// 用真实内容替换模板中的标记
$ j" B( l- Z9 J$ o$ z" x& h/ ]) ~$ [strOut=Replace(strOut,"$title$",strTitle) 2 r7 W N+ f9 B; O! p( m" D
strOut=Replace(strOut,"$body$",strContent) # M/ P/ l! b# E9 V+ S4 c% c2 F: `% I0 P
$ K# ]4 F2 u) q'// 创建要生成的静态页 0 i/ `! d. d+ f: Y1 Z1 y# W
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) # A9 C) T+ m0 y$ `- z' ?
, f/ _ I' H) m5 u& H+ P# o8 Z'// 写入网页内容
~8 g& F& ~7 k A! yhtmlwrite.WriteLine strOut I! q8 o; ^0 H4 |: O Z8 o! ~7 A$ x
htmlwrite.close
" c8 W$ J+ L8 k% E
* p/ W+ P7 J* _9 s7 dResponse.Write "生成静态页成功!"
* M% @/ q1 @1 v* K# V8 z$ [, V' I: r4 b3 c" l7 Q! N# A2 q
'// 释放文件系统对象
9 N) B- z( p) U w) r" sset htmlwrite=Nothing 2 H* b7 ?9 V& }+ c
set fso=Nothing 1 y8 h9 n3 j& \! p2 H
%> ) O1 s) Q! Q) e2 e
, M" ?- h9 T0 g6 a
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ! y3 Q% F% t) Y1 b: o! n
<%
4 h" t% j9 i1 u* ^ ?; ?& u m' [1 ^. F3 Z# ]3 w) m
'常用函数 4 X8 H% l7 J6 F8 x
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
1 W+ z# J, k2 t+ j$ y$ ^function getHTTPPage(url) : x1 }& s( d; r5 q
dim Http
6 y6 O0 i$ f4 r0 f- @set Http=server.createobject("MSXML2.XMLHTTP") , S& z4 N. h0 x: {
Http.open "GET",url,false 9 i+ X5 |* I6 t) y6 R* u/ d7 ~
Http.send()
: r; Y% e% e9 x$ @. Eif Http.readystate<>4 then
" G) g$ H7 R/ M) i/ H5 k6 Y! ]! g9 texit function 0 }6 u4 x. ^/ l& i, z- y/ K! t
end if / e/ D P7 ~1 O* H
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
' Z6 s3 f5 t3 a7 ^1 F5 Nset http=nothing 9 s% o5 M" e! y, r# y
if err.number<>0 then err.Clear & r R4 D" c; {
end function 6 ?/ r: T$ a; j
5 u- _4 j! ]: y'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 6 @3 G. W% D8 p! ]6 U0 H" z p
Function BytesToBstr(body,Cset)
# w3 R- t( ~; {3 Y1 x- hdim objstream 7 j& Q# E8 U% }9 s
set objstream = Server.CreateObject("adodb.stream") % d) u/ [9 H% e1 A
objstream.Type = 1
3 Z4 m* |: K; F+ ?1 U) kobjstream.Mode =3
) U' c/ v, w' y% f: F/ o: f3 Gobjstream.Open
8 v v; f# z# t2 tobjstream.Write body ! C0 E! G8 r8 K8 K7 I) h; \
objstream.Position = 0 H2 a Z, I- F
objstream.Type = 2
# W& Y0 W9 F+ E6 ~objstream.Charset = Cset
! T/ A" w$ n- Y+ I! ]BytesToBstr = objstream.ReadText w" w2 c% d' c$ {* I1 f) \
objstream.Close 6 I4 V/ u- F) H) g0 {% q5 U1 @
set objstream = nothing 4 N. ?# z0 G! D7 p
End Function ! T6 v% @3 m. i3 o
& X3 e8 g$ N) s B9 d+ W4 Q
0 ?5 A. `) O6 g @1 ~4 n1 ntxtURL=server.MapPath("../index.asp")
# ^) X9 n5 q/ X! Q6 O
+ C T1 d. H9 U) j4 u7 CsText = getHTTPPage(txtURL)
& W7 T' N/ n- [! W. O
9 n4 d. b) q2 _8 o- v |! _* USet FileObject=Server.CreateObject("Scripting.FileSystemObject") * S2 v: j1 ]) @6 k" q! g
filename="../index.htm"
7 ?9 q$ P0 W8 X/ NSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
) ?" |+ F+ |, _$ @$ n5 {9 kopenFile.writeline(sText) 0 ]" }8 u4 Q$ a1 g9 |1 {
Set OpenFile=nothing
: p9 T- l f+ _% J$ e- h- k4 v% n3 l& u4 w6 e# C0 ^( x
%> ) ^. _6 Q7 H7 D! M
<script>
/ b/ Z3 `* x% salert("静态网页生成完毕");
' Z% h/ C+ U7 d$ Q8 i" ]1 yhistory.back();
3 m* k8 l. T# P9 W$ K3 h</script> |
|