|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14393
- 金币
- 2482
- 威望
- 1647
- 贡献
- 1430
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. . E6 P- m8 Q) M4 g8 @
像www.aspid.cn的主站就采用了TSYS生成html文件! ! ?6 G# x M3 n, X% s
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. ~8 @ w# K9 c- E! \; C; o4 U3 [
) e. n4 a- I% \2 X# i9 t1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% ; d# q& A8 k8 F4 B
filename="test.htm" o& } j) D3 O0 n
if request("body")<>"" then , q4 p6 l: V8 I# N- [& z9 e3 s
set fso = Server.CreateObject("Scripting.FileSystemObject") , D D3 y- ]/ P
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ( _+ r' `. c; a {
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 7 j/ Q2 C7 m W4 M+ N
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" # c" u" }$ r3 l0 n" W% i$ v# d+ c
htmlwrite.close
; y7 b: z q* R. q0 Z) I% Kset fout=nothing
I+ J1 P, o& fset fso=nothing : F; z$ Y! Y0 d3 d/ k
end if
* F4 g& `2 b F7 s. L%>
9 `/ t& F7 X2 s8 p2 w3 P/ z<form name="form" method="post" action="">
) |5 `7 e0 g7 j8 s) S6 b- N<input name="title" value="Title" size=26> 3 M3 H. F8 d% }/ m! |2 I Z; |
<br>
, B8 R) ]8 Q1 v: R$ g8 h<textarea name="body">Body</textarea> 3 x6 f* J- S P' h& B8 j
<br>
/ H- T) G2 ?8 i2 w<br> 2 p, T% [! Y' B7 ?
<input type="submit" name="Submit" value="生成html"> " |% l3 u3 ~$ Y+ `
</form>
8 }4 ~; w2 D+ U+ B+ p2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. / G" q K) f0 k3 v& S
template.htm ' //模板文件 <html>
+ [+ t; P V( u/ n* d9 \: k' C<head>
) ?+ Z- m/ a$ c+ ~<title>$title$ by aspid.cn</title>
' K# O H0 {4 u0 ?, g% k! H6 e3 m</head>
! d( Y; C7 M/ R( J2 O d% {<body> 0 v A) {7 z3 ^4 ^
$body$ , d6 D" I8 I9 m7 r3 S9 `7 B
</body>
& R4 \: U* r5 X; x) z7 J</html> ? ) u7 }4 A: z, m N d+ q
+ _5 j" b K0 q/ L6 J
TestTemplate.asp '// 生成Html <% ) a& ~1 h% m$ A* Z
Dim fso,htmlwrite
. b8 n% B0 }0 n8 A" q& u/ pDim strTitle,strContent,strOut 7 g/ A) }, R+ S, F
'// 创建文件系统对象
) v) l- @0 y U7 tSet fso=Server.CreateObject("Scripting.FileSystemObject") & f/ F& @+ B$ D; v3 g
'// 打开网页模板文件,读取模板内容 1 b) G9 B9 v# ], X/ I6 H
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 6 N7 m7 n8 K0 c3 l1 b7 K
strOut=f.ReadAll : R& z# U& b0 m, [, W: g' u# c
htmlwrite.close
! G; J, d9 j3 V. M3 c" N( p3 k5 c% X
strTitle="生成的网页标题" . r" B' m8 n d1 ~
strC
4 `8 U2 `1 P/ I# ~1 m; Q( R$ O# U+ k F; e
'// 用真实内容替换模板中的标记 " C/ p9 Z( E& i" f& i1 | H
strOut=Replace(strOut,"$title$",strTitle) . i q! R5 l+ n4 q- ]3 ^' `/ h3 O
strOut=Replace(strOut,"$body$",strContent)
! N3 K3 M- O) N: `9 \" P, j8 ?% W3 z1 }* }% x
'// 创建要生成的静态页 , T0 J/ m% m# T8 r$ v; ], G
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
5 A" U3 V' j; k1 @9 T# z2 h7 }' z& P
'// 写入网页内容 * [, ~3 l) d2 c+ P
htmlwrite.WriteLine strOut ' j5 O0 Q6 E3 C8 i' ^" f+ T3 N
htmlwrite.close
$ _" r7 V% W% u' v! l% u# U4 O n* `" I( G: O" Q3 {+ Q
Response.Write "生成静态页成功!" / i* M! Q. [: u/ l( o- `
0 q2 T- y+ a3 A- }/ s
'// 释放文件系统对象
, D- N4 r* ]9 K, {( D/ m Sset htmlwrite=Nothing 8 v" f+ K# f: I6 y, t/ \7 ]
set fso=Nothing 7 s7 }; w) c& _: Q( P# C/ |+ [0 N2 s
%> 8 S; V' D5 H+ s. H& n; i* H
3 J4 f7 Q/ c4 K* Z! s! [( q, Q& A
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
) Q) ^$ u' F% o4 l, d* C' X<%
' G, e. j5 m5 p8 o
7 A( W k3 f; Z! ^'常用函数 5 M& ?5 n7 |! c0 P y
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 + Y7 B! ]4 C9 ~$ Q
function getHTTPPage(url) : I) k/ m6 t5 C
dim Http
3 J% W( ?+ n, J% bset Http=server.createobject("MSXML2.XMLHTTP")
, Q" b8 T4 D6 E5 v T( uHttp.open "GET",url,false " a+ r; u9 R4 R5 L, o1 E. O1 D" i
Http.send()
4 W1 |5 N9 w. k+ s4 X O4 @7 iif Http.readystate<>4 then
0 J# e7 l$ ?' A3 W! xexit function 5 |9 K+ Q) M% }% H, r3 p
end if ' K0 H( {' Q! l% u; L' ~
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 7 ~( O2 z J$ h, i: u7 S8 D% W
set http=nothing " g& |" e( E6 C& f, x, R4 F
if err.number<>0 then err.Clear / l3 o, Y B2 W' l( c: i
end function
8 f! j2 S( C }9 Q( w! Z5 X: \
/ T. t1 J- v7 A'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 * `$ Q$ b2 _5 x" q. t
Function BytesToBstr(body,Cset)
& t+ F. y/ o5 m7 t) {dim objstream
# T' z& E2 I7 F3 P" |9 N/ Fset objstream = Server.CreateObject("adodb.stream") 7 c& @/ ^, j$ T7 @
objstream.Type = 1
# r! {4 T) z* F% F" cobjstream.Mode =3 3 p7 n/ |9 a6 x' |* U
objstream.Open
' b( F2 U0 L, r! s. U0 Z8 A9 `objstream.Write body : W( ~: K2 \ E* D+ g" f
objstream.Position = 0 3 i2 r d8 c+ m! P
objstream.Type = 2
& @! F s+ ]. y* Xobjstream.Charset = Cset
9 k2 w# T1 t) A! v, P* uBytesToBstr = objstream.ReadText ( q$ q" Y- C3 i p% A
objstream.Close 2 o3 _5 T: X: G8 X; {, i7 v
set objstream = nothing
' r6 t2 b% E" D! S. R1 V% dEnd Function
! H. y0 p. [0 W: n7 {, p/ J. [& M" @& t& d& s" h5 x
$ N( ]6 M$ |' l8 U' \
txtURL=server.MapPath("../index.asp")
, E+ P! p8 N# H' j
7 P" u" k8 V' q+ MsText = getHTTPPage(txtURL) + j$ d7 ~+ B4 R7 J
$ Q* x1 U' F9 `" i$ E7 YSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
$ K( U9 _+ Y- x: V! R% \filename="../index.htm"
; v4 P7 v; T6 M3 |. R7 NSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 3 `1 {+ R9 g4 b2 v# i3 _/ D
openFile.writeline(sText) / T( R8 b; P1 y$ n
Set OpenFile=nothing \. _% a s, z+ @4 l9 r. u6 S
$ ?/ g4 A$ Z( i/ g" Q7 D# p2 R%> t4 n) z6 u K L4 o% _
<script> $ O! I( |' c/ Z1 X0 G3 `, u' H
alert("静态网页生成完毕"); 1 h) ]: ~2 a8 z! ?; C
history.back(); , ?/ ~& P2 w3 ^3 K; D2 P3 C
</script> |
|