|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14329
- 金币
- 2450
- 威望
- 1647
- 贡献
- 1398
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
) E0 c4 U9 b0 Q像www.aspid.cn的主站就采用了TSYS生成html文件!
# Z6 m" p5 [/ |3 c b- ^9 P# @所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. ' k" E8 j6 w; b# r, b1 b6 C) W
+ [( Z& ~+ h2 t( O) s/ }* e1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
! f, m! l9 K* I8 _filename="test.htm" 8 M" g3 o1 ?, h) `2 z4 _2 [
if request("body")<>"" then
' f) K# o2 s M7 k% N% qset fso = Server.CreateObject("Scripting.FileSystemObject")
; u" y r t- N& Lset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
3 o7 l) P; w1 W8 D7 Dhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" % j3 s, E( l6 ]' v7 {/ a
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
" ~" K3 O4 n3 y* l: q( A' q4 Shtmlwrite.close
2 p3 R% v* j4 b; U+ z. X4 t) Fset fout=nothing + v' Z1 y7 w9 _; Z4 d+ S
set fso=nothing
8 @: E5 O$ y$ C9 Y' m) cend if 9 `9 r" o5 x! b, [
%>
0 q: M& `! D6 f8 B<form name="form" method="post" action=""> 9 a+ ^+ [4 V$ `7 i+ q' G
<input name="title" value="Title" size=26>
0 B/ P# ^ i, i<br> & W/ S b9 c( w5 e, y5 ?$ c9 ^
<textarea name="body">Body</textarea> ' S- {; Q1 `* s$ H& A
<br> 5 @+ ?/ h# g0 G0 x3 v
<br>
. ^- l2 o9 d1 U( x' Y) v, Q<input type="submit" name="Submit" value="生成html">
& d- W; h2 [6 Y) t: z% Z</form> 1 y4 ~2 u/ E# U6 o* d
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. " U' M' q! p, N+ _1 @/ ?6 k
template.htm ' //模板文件 <html>
6 N T$ E& g6 G8 U- @* a4 p<head> / l0 L# O$ G- x8 k8 {
<title>$title$ by aspid.cn</title>
T8 A4 x/ n4 I0 N4 \</head>
6 l+ l1 h) N: m<body> $ s2 N2 |) o6 S8 H5 z) J5 u2 F: a: S
$body$
8 k9 [+ V, d% z$ f. N8 ~' ?1 M</body> # z6 n1 x* ?% H
</html> ?
' `0 i1 f2 x# p% \
7 ~! {# S; @1 o4 V( h3 CTestTemplate.asp '// 生成Html <%
" x8 z6 x" b. F1 XDim fso,htmlwrite # W+ L6 ?- [4 Z4 X2 ?- l4 N9 i, e# E) E. @
Dim strTitle,strContent,strOut
4 J, o2 l5 m1 o# s) Y, D'// 创建文件系统对象
5 \' Q( F D5 G G3 z) pSet fso=Server.CreateObject("Scripting.FileSystemObject") 9 [8 X3 x* w6 s! Z
'// 打开网页模板文件,读取模板内容
: B6 C( _2 w& ^1 C8 z: GSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) % S" E# V" T" |, D) @% O- C
strOut=f.ReadAll # ^5 Z% V# R9 R0 n
htmlwrite.close
! U. D* ~' V6 W! V4 R* ~+ O! k' P2 X! c4 l
strTitle="生成的网页标题"
+ i+ D. k/ T. C! J+ tstrC
9 w# V- F4 M" ?
, k3 f0 x$ V: W3 n1 ~0 o'// 用真实内容替换模板中的标记 , j' u; ?% o M3 y& k7 Z: v; p; d
strOut=Replace(strOut,"$title$",strTitle)
/ n5 `; ?) B# _# x& b7 d6 SstrOut=Replace(strOut,"$body$",strContent)
* x2 U6 E' |2 V" b. w$ O; G! A$ J- ]
" L/ Q) P3 h8 K, h5 A'// 创建要生成的静态页
: r) t4 ^" Q; l$ OSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) $ z6 C. ^( J. H. L, m
5 T) Y- r; x/ B0 F3 g3 X'// 写入网页内容
; N- N k) }' S, F( F0 @htmlwrite.WriteLine strOut
) c- b# ?- w* Q8 \7 P8 chtmlwrite.close
* v' @4 O4 A) u- q
: |) t1 `8 H P1 j% @( e/ [Response.Write "生成静态页成功!"
8 b. R3 F4 t$ B' C9 y* j( Q2 [
- N6 F! N) Y/ Z' T'// 释放文件系统对象
/ c: ~# I+ }3 {2 W. k: T- _set htmlwrite=Nothing
- R3 | C5 O7 v# D% ~set fso=Nothing ) n0 `5 r. B% {( g4 Q0 n
%>
3 N: C4 B+ c: n" y1 f: a! O0 C1 Z) E: X* [6 ?8 G2 o
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
7 B/ S$ N4 F5 m' M- ?- u+ ]8 \<%
' [4 g5 c0 \8 T) l6 N) X
. d* a; a$ o# `: q* R0 I0 [% f6 d'常用函数
: X" e+ f/ y+ O( S'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
1 \4 }" r5 W: [2 T, c- g2 ifunction getHTTPPage(url)
2 O1 M5 `9 _ j4 H. j1 Y4 udim Http
& ? M4 [. E, h* qset Http=server.createobject("MSXML2.XMLHTTP")
% F5 n9 A: n, n( P( [3 q4 yHttp.open "GET",url,false
! ]3 C* x: ]8 i& \$ \1 iHttp.send() ) O; E A: A. E; B. F
if Http.readystate<>4 then
! I) ?* M7 t4 Z- Xexit function
, j' U* n" e) @) L" Bend if
% w! R% K% {' _8 `( _8 u' k* T" OgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
! P+ M. [6 e! C" S& y; ?' Qset http=nothing
- M$ A4 e- n! R3 e6 A6 d- P% `if err.number<>0 then err.Clear ! y. h% K- Q5 @& P/ N' C
end function
& ?1 n$ a6 T5 V4 |& n5 U. G
1 M* r. S+ Z j- \$ c$ Z# ['2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 4 K& \ p% G# u: b
Function BytesToBstr(body,Cset)
8 G7 u! e9 z v' @8 Jdim objstream
* m4 o( y8 N4 z- [+ P5 gset objstream = Server.CreateObject("adodb.stream")
7 F0 c! c* t! w+ a1 i! u5 ?objstream.Type = 1 % m/ E9 J0 Q9 @9 n
objstream.Mode =3
$ M6 i* m; o3 e" L; W9 xobjstream.Open 2 |9 S; ]3 k2 y1 K
objstream.Write body % w$ P# P, H0 q& H; K1 L
objstream.Position = 0 * D. S* y$ u! M6 M' e$ b% C+ }. r1 K
objstream.Type = 2 ) l+ P: l9 K! l/ n2 l) S. K% i
objstream.Charset = Cset
3 ~1 \! j5 t5 |3 D d6 M1 OBytesToBstr = objstream.ReadText . j/ f5 N2 c* y2 q
objstream.Close
( E# `( r: Q8 D3 T- {0 J( h# s7 yset objstream = nothing 3 c6 e6 { F) }5 Z1 [& c
End Function
3 o' d3 R- h+ F8 J
9 W$ r9 Z2 }. M! K r. w
; w1 q% F/ N( E3 y# P4 q! itxtURL=server.MapPath("../index.asp")
+ z9 R+ ~" p1 S
) R9 ~6 o0 j, d4 w4 p3 R$ |sText = getHTTPPage(txtURL)
# a7 E3 ?) v' w% w& X* l' H& `% C
* T q. {: U5 e4 `Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 2 o, Z% T' c7 j6 |
filename="../index.htm"
) @) Q% T+ v; H- L* NSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
* t8 L9 t/ u6 }openFile.writeline(sText)
. l; c9 w- C$ {" O* A# o* B6 w8 rSet OpenFile=nothing
. w2 j# V4 M9 ?$ L% Q; B3 n) N! l
1 v( ~& h4 t. ?9 I [%>
/ g* v3 d" D4 h( J: ^<script>
4 w& E9 T0 l6 j# T8 u5 G, {4 ^alert("静态网页生成完毕"); 5 A. k- U# \8 D
history.back();
. ^4 E4 [( ?" b4 ]; O0 |</script> |
|