|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14403
- 金币
- 2487
- 威望
- 1647
- 贡献
- 1435
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
/ x9 q9 I2 ]# w0 u+ s- A# V像www.aspid.cn的主站就采用了TSYS生成html文件! 2 E) j9 q! n! e' B( h1 G1 v: G6 M
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 4 q5 h- {# |" m% c; ~$ P& s
8 y7 o$ Y) G% B3 R( k# k) b
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
$ c/ M7 A$ F1 y4 I8 `1 dfilename="test.htm" - y. u% E; X7 v- R
if request("body")<>"" then & T( b) ^( S: W3 f; n
set fso = Server.CreateObject("Scripting.FileSystemObject") 8 ~2 K( M1 o! x# V0 m
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) & q* R# d* K8 |) M7 i
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 5 Q! O% d' c- X: [; ]
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
& \7 {: u/ L; n6 ?1 o9 [htmlwrite.close , A6 Z3 U9 f. y) Y
set fout=nothing % K$ K2 y, x4 [! Y$ S
set fso=nothing . N- o3 g" ~5 d5 h& T
end if
- j+ b/ S) O% G) u8 N5 {%> : A0 C& K9 R' I9 G1 K
<form name="form" method="post" action="">
/ h" l+ ~. [* T& p6 |! a& e* q: r<input name="title" value="Title" size=26> ! y% N4 f& x8 T* i2 s5 f
<br>
! n6 u; Q# Z: O# J4 n7 ?<textarea name="body">Body</textarea> $ m) L" t* s+ F# b S1 a: Q
<br>
' j: W. r% o) k* f3 I' A1 R<br>
1 B7 _% n+ ~! Y! ?<input type="submit" name="Submit" value="生成html"> * {% ^+ E1 y& W& q! s
</form> # @1 _4 S2 H/ n1 g7 }# n
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 9 @' }) b) o- }' z6 ~" @3 o: e
template.htm ' //模板文件 <html> ( Q! N8 L* y" B# s4 f# a$ o7 x" r
<head>
+ T/ M# J1 a2 m0 w4 s \<title>$title$ by aspid.cn</title> 7 {7 ? g& s k0 r }* \+ y: i9 [
</head>
' m, c, J2 q+ b- u<body> 1 l1 ]! A1 ~& g8 S
$body$
" ? F H$ f$ W</body> ! w0 R5 G8 u8 z) t2 D
</html> ?
4 D5 H b5 }4 A& M/ r: N, P; G8 K
& I6 l/ m' G8 cTestTemplate.asp '// 生成Html <% 8 V9 _2 B4 X3 Z8 ~$ c7 S$ E
Dim fso,htmlwrite
1 `7 W# K4 y! ?9 bDim strTitle,strContent,strOut
; v+ n) ?% b0 {' g! l' q$ {" y+ g'// 创建文件系统对象 2 q8 w# O9 V+ u) a) w
Set fso=Server.CreateObject("Scripting.FileSystemObject") ) d# U4 T- Q6 j! i2 C
'// 打开网页模板文件,读取模板内容
& o0 x7 N! }$ X( q2 U# WSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
7 L% _5 g7 C w: fstrOut=f.ReadAll 3 j6 I# f; P' b# E" I
htmlwrite.close
8 W+ J& q- k ]+ f6 i6 Z
! f' N o) P5 V) @! ~strTitle="生成的网页标题"
9 {* Y& q. O" D1 N. \6 L7 _, E- NstrC
5 W0 @8 v7 y$ U6 |8 y9 l
1 ^6 Q$ s$ v* o& b'// 用真实内容替换模板中的标记
/ _) J0 H( T1 @, C( H: z9 ~strOut=Replace(strOut,"$title$",strTitle) . Q3 ]- Q+ r3 n) ^ G1 q: G
strOut=Replace(strOut,"$body$",strContent) 5 X2 g* H2 G' r; d ?- t j
$ D. `, T( t+ n5 G; z/ d'// 创建要生成的静态页
t0 K. _0 O7 m7 B/ ~9 h. ZSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 3 q' p& {' W: v0 m8 O
6 ]* e, ?# l2 X'// 写入网页内容 5 A M0 T9 p& t4 p ]: k
htmlwrite.WriteLine strOut 5 z Y8 N5 e& u o, N
htmlwrite.close , u8 V9 Y0 A8 a8 f; c& I7 @2 W1 J3 _
2 N/ f; J4 a1 O0 w
Response.Write "生成静态页成功!" ) q; r U+ C$ C
0 P9 T4 P# q2 N9 h
'// 释放文件系统对象
4 W$ z: ^/ ]: g+ v6 tset htmlwrite=Nothing
0 E6 A" K6 j4 I9 v* M2 Cset fso=Nothing 5 F o" r. h% \+ j7 r* q7 v
%>
: k8 w# X! f$ K! H1 v
& H! y* b7 E6 i. L! u. V3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
& W+ A5 r/ a. B. ~; f3 }6 x<%
4 j9 J4 `( O+ k" O6 d! t0 _5 Q, Z, ~9 z0 n e
'常用函数 4 o: D5 M( Q+ V
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 0 e& D. Y; f I: J
function getHTTPPage(url) 3 M# K$ K! D( O; Z0 j4 A: O' g& U
dim Http
5 D/ G8 P" J9 W9 oset Http=server.createobject("MSXML2.XMLHTTP")
$ [9 M3 z% q( a! z6 \" e( ~Http.open "GET",url,false ) R! j/ S9 W/ N0 b S, a Q2 D
Http.send()
- ?: U1 C' w" N2 V' g' E9 z8 C* f& Rif Http.readystate<>4 then + b# B) B6 k/ x0 `
exit function $ J! w: n6 N4 ?9 z; b e6 F
end if - u" l2 r9 x# l% Z& z4 f$ h+ h1 u5 D
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") ' h5 T: t+ [+ T+ x( ]& E
set http=nothing
/ ]* i; @9 W3 |& H1 v! [if err.number<>0 then err.Clear
" M% ?$ F/ f* P: oend function
) ]7 m8 n. q% P4 L% y4 N" ~; K& D7 ~2 l8 J9 F Z V. |
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
7 c: r8 C$ K7 E& o) p" vFunction BytesToBstr(body,Cset) 4 | {, @5 @# e9 `7 Q& ^2 P
dim objstream ( t3 A: t$ x' V& Z
set objstream = Server.CreateObject("adodb.stream") 5 a. Z; @7 }4 M& S1 d, F
objstream.Type = 1 + R/ c9 i) P% @6 k0 K
objstream.Mode =3
7 P t K' C4 w$ Vobjstream.Open 1 I9 g4 D% D% {1 M& @9 M1 Y( Q
objstream.Write body
1 e( N& O! Q W1 A0 n- tobjstream.Position = 0 - ^" |* F+ \5 h3 Q/ |
objstream.Type = 2 3 C# }" a. Q& ^8 z
objstream.Charset = Cset
8 {, }# {# D: P; n. G& A' d3 `BytesToBstr = objstream.ReadText
& D6 C0 |* I6 r+ r5 q8 l8 q b2 Sobjstream.Close 1 \1 r# _# q/ G8 j. R2 w, n1 P- V
set objstream = nothing & z, t* U% }8 ^2 }- c; J( _. j
End Function : B! s$ e" e0 z7 R" I
Z+ R# _2 m9 o7 X [$ q0 P* ~' ^
5 c# l3 w1 p* s J8 n
txtURL=server.MapPath("../index.asp") 6 \& p$ {% K3 f5 t. F. k
9 i g3 Y8 a( {. @$ R' @sText = getHTTPPage(txtURL)
P3 u+ G9 g" }" ~9 ~9 j4 D' G9 U/ @, e/ ^: ?3 Z
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 3 U; y/ r% P) @
filename="../index.htm"
5 \; ?8 I- `2 g4 e; T6 aSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 1 Z+ V; `( F6 F
openFile.writeline(sText) 7 G; _4 a, P: [) ^2 ?" ~( ?
Set OpenFile=nothing
1 Q' c. l( O" F7 V8 Q7 [. b
, q H" J- G' c, x+ W%>
# l$ e$ b" C5 ]0 O9 y5 t<script>
4 O- M! [' p3 |alert("静态网页生成完毕");
9 ?7 I u1 K( R9 Khistory.back(); 7 m d4 b, X" U8 F) T7 ]* a
</script> |
|