|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14435
- 金币
- 2503
- 威望
- 1647
- 贡献
- 1451
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
$ L& e, d' U& k8 B像www.aspid.cn的主站就采用了TSYS生成html文件!
5 b' l3 D" Y$ x% v: d3 O0 ]+ r& Y所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 9 e# v4 C! O. }
6 A* A( N" e+ G; }; O
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% & {8 k; d. \+ D2 D: G1 T
filename="test.htm" 0 H+ K- W- q# j ]! t
if request("body")<>"" then - I% l% e/ {0 g
set fso = Server.CreateObject("Scripting.FileSystemObject")
5 R# w+ u7 S" Q, Nset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) . h/ [* Q5 ~5 ?! c+ G& A- y
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
* Y- _; _6 f: [* B7 Z( D7 M, F& fhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ( s, k1 }) u5 v- x# z
htmlwrite.close
+ b. l$ I7 D5 ^7 nset fout=nothing
5 H8 }1 C9 d' ^8 N% V) Uset fso=nothing 0 Z1 w1 T' s' N8 m* F
end if 8 a# Y" b/ i0 }. m$ Q
%> - W* O B, d* V) k
<form name="form" method="post" action=""> ; l7 [4 T/ J9 L" v; G: i
<input name="title" value="Title" size=26>
/ H8 U& s! O9 d6 Z0 V. B<br>
- _3 Q: i2 w! b: D8 B<textarea name="body">Body</textarea>
2 T3 `7 T# |0 h. Q<br> 5 }4 I4 r0 {; r8 s) B( a2 O
<br>
! a$ O( R1 `6 o& W8 G/ O<input type="submit" name="Submit" value="生成html"> : R( x- ~* {" V3 Z( z6 G
</form>
# Y. P: ]0 Y8 O' r( J2 @4 ], e2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
* B: q% p3 t1 F0 ~6 R$ _template.htm ' //模板文件 <html> 1 T! G3 r* D, Q6 Q. n
<head> ) d9 \& c: _: f
<title>$title$ by aspid.cn</title>
- w% x; E# d- ?, d</head>
9 ~8 @; p0 k0 p D<body> ' d4 f' K! V" O- E1 a$ u
$body$
& s/ T; v. {# ~2 I</body>
2 @- {: t9 D E7 k7 N" H</html> ? * }& z' {- B9 F5 N, }+ g% l1 f4 r
0 r6 e- t2 h5 O; t3 T; l/ n D
TestTemplate.asp '// 生成Html <%
L. b( a3 [3 {Dim fso,htmlwrite
' H8 M8 v8 h: F8 e8 b6 gDim strTitle,strContent,strOut # u; S( B* b$ u& c. z/ n. f3 l9 G
'// 创建文件系统对象
% ^2 f0 c- a! d0 dSet fso=Server.CreateObject("Scripting.FileSystemObject")
8 g3 Y1 v' K! p* X'// 打开网页模板文件,读取模板内容
5 Q) G h3 M! r2 o1 S0 k) iSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) : O3 m' N8 D1 V6 F8 `& e2 B, @6 Z
strOut=f.ReadAll 3 Z: Q9 b) |# w* O
htmlwrite.close
9 Y# m$ h& A7 o3 C0 P2 V
. ^9 r) H. i& d" \9 u; p. c* A. ystrTitle="生成的网页标题"
1 e/ ?/ `6 M% b" f4 P# N4 dstrC
* `( k" e; ~, s$ e, U
; T9 Q& V9 }7 c7 c8 ?, m'// 用真实内容替换模板中的标记
4 ?$ p2 ~% D }2 ~1 jstrOut=Replace(strOut,"$title$",strTitle)
& H/ Y9 s2 C! r' X1 @, S% W( HstrOut=Replace(strOut,"$body$",strContent)
: U: Z$ [6 [# K2 d$ y$ ^; {
b0 d' f9 w5 r! a& H'// 创建要生成的静态页
! Q o$ i+ @+ @# ]) K) _Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) ) p+ x# ]& s$ P' J- F, W& g
, `! L8 X3 e k# s5 ]$ t
'// 写入网页内容
4 k$ `5 h! @7 B! B5 R6 Q# Khtmlwrite.WriteLine strOut # P/ r! \( q# ?( r/ ]
htmlwrite.close & o) B! H- T# C+ P: D
5 E |5 p' Y- N1 s) n$ w+ K8 f* ?
Response.Write "生成静态页成功!" 8 H3 d6 K3 T# L* Y8 Y) U
9 @& t( |: `( V, z& H/ I( o$ {/ E2 D( S
'// 释放文件系统对象
2 Q3 r0 ~/ p) l( fset htmlwrite=Nothing
1 z/ y) f& H5 ]6 W4 @. h! j1 p+ l. jset fso=Nothing
6 i! `/ [$ `$ B& H! k9 k, P$ Q1 x# |%> $ l4 f8 c: W9 p( h+ p3 M [' U
9 T+ K* \6 o' l# Z0 i9 V
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
6 e" c9 t Z% P: s<% 6 Z% c% t, m2 r
# c: {/ h' Z% Q'常用函数 * \+ G/ g3 e5 V8 Q4 x
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 3 V; U! N) O) [$ g3 _9 X
function getHTTPPage(url) / e+ Z* M( O! B9 _' _2 B7 _3 P
dim Http
9 J8 N1 v$ Q4 ^8 l6 k$ pset Http=server.createobject("MSXML2.XMLHTTP") . {% K; V9 Q9 F: G( l9 q
Http.open "GET",url,false # o Q( s" l- T; u z6 Y) g3 J
Http.send() 5 n+ `- @5 Y ^! ]7 h% `6 T: ^9 M
if Http.readystate<>4 then
& j9 A3 t b5 eexit function & m! g l1 h& Z. S" i
end if 5 L. }9 F8 H# f( N0 ]; c
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 4 T- `+ Q+ O- M! C+ i8 \3 u
set http=nothing # s# X5 Y2 ~3 A. |
if err.number<>0 then err.Clear
" v" O% o$ G+ ~. u) Wend function 8 e, J+ y2 j+ J( I6 o6 M
" Z! [) M, X+ q$ D. [. ]) t( B
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 $ m0 A; C% X6 v. } g8 V5 }1 L
Function BytesToBstr(body,Cset) + r( R. j7 r( G/ ~8 N
dim objstream
4 j& T; l- x2 ^* _- {/ a+ Kset objstream = Server.CreateObject("adodb.stream") 1 L: W7 M( B% K; a! a) U; a/ K8 Q0 ~
objstream.Type = 1 . j5 @7 f' d1 Q3 U( p
objstream.Mode =3 - x8 |% P& x! B' c' @5 N
objstream.Open ' |" ?$ F8 r9 \! K) a
objstream.Write body ( @/ y2 l! S( G K$ o9 O
objstream.Position = 0
" b D' j' f8 m) @# u6 Kobjstream.Type = 2 6 f1 r, p8 F0 ]- A+ y
objstream.Charset = Cset
4 L4 i' b* @$ y9 wBytesToBstr = objstream.ReadText
/ Q! X4 S: H) M5 ^4 ]% a$ A; h: Tobjstream.Close
" \" M( c; J% n! g) J" }) Q; ?set objstream = nothing
; l( N0 v1 V9 z' M; q! ^! SEnd Function
x) H$ K9 ` l3 o
$ b% `$ O/ T+ X' _( }3 ]& d$ n+ z' J
txtURL=server.MapPath("../index.asp") 2 O$ H, p1 ?1 T1 X5 K
" }* {' J! @% c; \( H8 tsText = getHTTPPage(txtURL)
+ ^; ^ P; q% Z
8 C; g+ ~( J A- ?( gSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
! }+ t3 [! `, z' R" F1 Tfilename="../index.htm" * i0 o9 y& E$ [) t. {
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
( _3 D2 w+ e% J3 K6 K' ?2 V) EopenFile.writeline(sText)
. U) i$ H- i: s3 y3 X7 A! oSet OpenFile=nothing $ g0 ]1 O) t9 W7 i' ~4 u& J. W/ G S% p
0 e7 m$ Q8 S' M+ e5 g%>
" L4 U6 F( n* l0 W# } j( O<script>
8 V. ~1 _; c: {+ Aalert("静态网页生成完毕"); 1 k/ ~6 M M6 c+ A
history.back();
3 g+ _; F! W8 |' u* w& g</script> |
|