|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14325
- 金币
- 2448
- 威望
- 1647
- 贡献
- 1396
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
1 V# T2 k7 P* K像www.aspid.cn的主站就采用了TSYS生成html文件! / ]1 G- M" a! I7 U
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. . F! B4 r3 d1 i5 k- v& v, L
% m; W: @. J6 T$ B. h3 Y3 E9 ]1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
- {7 E% H; U3 I( F7 [# b4 Q# v) ?filename="test.htm"
/ z3 ~4 U6 J8 k# V6 Zif request("body")<>"" then
8 A* d( n i- C! a0 \set fso = Server.CreateObject("Scripting.FileSystemObject") ; O4 ?' M8 N7 Q8 d1 F
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ; b& w7 q; S+ N8 [5 l& A- y7 T
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
6 [4 V) J7 [# Whtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" % R0 `6 L) n# D( _+ l) y8 [+ @
htmlwrite.close 7 i+ Y4 X- q6 [/ Q7 T4 J
set fout=nothing . [. q0 T" p c) j G
set fso=nothing 0 _& Q& g( H1 p8 j# p
end if
: H0 B& j+ ^; l, [- I9 W0 {+ l%> + j' j) t: M& J- ]% v0 v# z# n
<form name="form" method="post" action="">
3 B& s( D0 m1 E* n<input name="title" value="Title" size=26> 0 m( Z/ ]6 H8 b! o$ W) I. b, c# B
<br>
4 a% M2 f) Y5 m8 [<textarea name="body">Body</textarea>
9 l g# D' h( |" q6 @) P<br> 9 B( `8 X2 J; m8 D3 {
<br>
' l& I) |8 _7 ~& f! W% H8 l, G<input type="submit" name="Submit" value="生成html"> % C! v, \" T7 O: M7 D* w
</form>
; }0 R) ~. U$ _- y- H( l2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 4 \7 {; A0 j) j U, R/ E( Y% V
template.htm ' //模板文件 <html>
8 G) m/ R* _1 |1 Z1 S7 u% ^6 s<head>
5 v8 _9 h2 E6 c<title>$title$ by aspid.cn</title> - }/ T' u- P$ b9 r7 l2 h% @5 E- r
</head>
2 ?( B. p/ B, u% l" Q& |<body>
! \; x9 M7 w: l- K9 }& }, a" Y; s$body$ 0 m0 C0 j, D; Q. }( v, ~- c7 w2 c# t
</body>
. @6 s7 d. V2 i: x: u</html> ? + s% e3 G6 \$ k5 Q7 V7 u- Z* d" h
0 D. m" j. W3 p8 t% }! R! q
TestTemplate.asp '// 生成Html <% $ l4 J1 A8 h- p3 V8 F/ F1 l
Dim fso,htmlwrite 3 g/ v' W4 |1 {. {# M# E$ j
Dim strTitle,strContent,strOut 2 g2 O* h( l" V0 L
'// 创建文件系统对象 - \3 w: O$ y8 {$ J3 q
Set fso=Server.CreateObject("Scripting.FileSystemObject")
6 m+ D! p) b5 i'// 打开网页模板文件,读取模板内容
) ?2 I( O+ u2 O3 i$ r1 a% jSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
$ L+ C, }1 @+ ?' dstrOut=f.ReadAll
7 L g, i3 a- [htmlwrite.close % j* M: R) l9 h" O
5 d- x: e+ { D: {6 WstrTitle="生成的网页标题"
7 M% u5 [7 a! r; T9 UstrC
% I; `0 r, ]8 x: `9 s# X/ ]' o+ g
- d7 @% B3 k6 ?- a. h+ V'// 用真实内容替换模板中的标记 6 H3 Q1 q% r+ c
strOut=Replace(strOut,"$title$",strTitle) & S$ @. I: B9 g
strOut=Replace(strOut,"$body$",strContent) 1 U# \2 p6 O, E( F3 G
* a5 ], c% s0 G6 W) @- }'// 创建要生成的静态页
# \$ b) G$ L) c) z: Q- ~Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) & }7 v1 v6 B1 c! o
% F$ t. d, a8 r$ X'// 写入网页内容 . S; r% H5 R! }8 f. t! R
htmlwrite.WriteLine strOut 4 j+ o x2 J) q; D/ j* v
htmlwrite.close
# V6 o* g0 ~+ C( {* u; I" \5 d V( F
Response.Write "生成静态页成功!" 9 h4 k1 H- ~8 P! U. {/ n9 Y
" _: x3 D( F: C: U; X4 s'// 释放文件系统对象
/ S" z: @) L0 c4 T d% B% }6 Vset htmlwrite=Nothing
; C5 G# [7 H4 R( h6 M& y7 A) i. t2 N) Gset fso=Nothing
: D" D* e$ ?! R$ a; w- E& f% k%>
9 A9 Z% D; s1 n5 h/ {/ K
h2 M- J; K& c4 |; C w4 b5 [7 m3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
! z; P8 E8 m1 W( t<%
2 r- L+ V& B" c7 u* u4 M* \+ L$ I1 `* `
'常用函数 / d6 B0 a% P8 S) e* S$ V
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 / V: R5 W% C1 h/ ?. n( I$ k! |
function getHTTPPage(url)
" Q: ?; K3 x- b3 b! Y" ?) ?5 k- }dim Http
5 L1 `4 I4 s% u3 h) wset Http=server.createobject("MSXML2.XMLHTTP") ; x% t- m9 v4 h( q" T. r
Http.open "GET",url,false
, G- n3 d; }) ?' h3 F4 y$ ^6 Q, RHttp.send() # E7 T2 W: Y+ F* u& N- R
if Http.readystate<>4 then
+ A; d6 [4 i3 O$ Texit function 2 @) I2 }' a/ w
end if ! T) U( K8 A5 d/ e2 D/ ~
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
- f+ a. r+ g9 |6 v- B; M- ?5 xset http=nothing $ J5 }0 j3 @: o# U- o
if err.number<>0 then err.Clear
' J& o' M1 e5 S6 J send function
1 A9 }/ r4 r* e% ]2 o5 d" s1 n: j* N, ~; C7 u, I
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
( R5 A: _& g( G! C! N" L/ L9 vFunction BytesToBstr(body,Cset) ' p* O A, ^+ Q% ?$ U6 ]
dim objstream 5 ^/ P' O* R1 W. Z4 ?' d) u7 `
set objstream = Server.CreateObject("adodb.stream") 4 a* x0 B* z3 m6 d P& }9 B1 Q0 y
objstream.Type = 1
8 ^2 |/ }$ ^8 H# s7 O3 xobjstream.Mode =3 8 i1 [$ s) q0 A" {' Z
objstream.Open
5 [1 y7 o3 S- ^$ Jobjstream.Write body & G- f8 }3 |) h% M* U, i
objstream.Position = 0 z. r: v. @ q9 |' a* @
objstream.Type = 2
8 m' d( c9 a: l# iobjstream.Charset = Cset
; a, `9 R6 G5 |' x) D4 V- kBytesToBstr = objstream.ReadText
! k, m% q, }0 eobjstream.Close
, o( u: V- ?0 D& x- \. F3 i5 D9 Xset objstream = nothing ) T, V) L5 x# p! i) }
End Function ) v& `5 \9 e# ^6 @
+ F' U4 I3 R8 O1 r& H
" u* F0 ?; w( o$ X" ^
txtURL=server.MapPath("../index.asp") % Z* V* J6 W4 L8 {% V0 b' v% W" C
8 ~( t3 V) m: W2 D% T) XsText = getHTTPPage(txtURL) 9 h, P* a0 ~/ g$ l7 U( t' [) z" E
! i# V$ q3 u2 W3 _Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 0 G' \) T& R- c. \3 a
filename="../index.htm"
: s" L# L: k, U3 qSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
5 q* d; O0 ^7 @! s( v) W: `openFile.writeline(sText)
! H7 k+ T2 M9 oSet OpenFile=nothing
. ]2 _8 R5 w. |" O
* D, n: f: j- x g%> $ ^. b; x3 Y# W' v- h! ~1 n4 ]! H
<script>
! \* j, c" U! e6 \- ralert("静态网页生成完毕");
: x9 g+ a, M" Q: n0 Vhistory.back();
8 H) q. C3 F/ H0 x& O</script> |
|