|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14369
- 金币
- 2470
- 威望
- 1647
- 贡献
- 1418
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
& J; L4 f1 j% ]$ v/ Y; }像www.aspid.cn的主站就采用了TSYS生成html文件!
9 R, d" J4 v. g9 a/ e* M5 C所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 2 k' x' ~% q3 V2 S8 r# |
- K+ \ t" {! Q8 Z. s1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
( `9 q7 j% d4 |5 @) Cfilename="test.htm"
6 w* _. k4 ]$ h2 k0 Jif request("body")<>"" then
' v1 ]& e# w! x. s. ?- [9 X4 lset fso = Server.CreateObject("Scripting.FileSystemObject") " q' d5 _" A" o2 Q8 i( ]- d# R
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
8 t. W* n9 t/ Q' {% }( lhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
8 x5 q" C& M' Z+ I+ }" z! ohtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 7 v, x6 C; \! X r. U1 `
htmlwrite.close
O6 L4 ^0 \. h9 v# q+ oset fout=nothing $ p% c% Q' w: g" G
set fso=nothing ' R) H9 p$ D; N& P" h! o
end if % u) e3 k; {/ e" u# g1 {4 C
%>
" a9 l0 ^5 I0 \' Y2 [<form name="form" method="post" action=""> * R. o" J7 s. m2 F, ~3 y
<input name="title" value="Title" size=26> " a3 D9 n0 ]- @( m4 b: F
<br> ! L. R: H( \1 \; Y/ h: ?+ k
<textarea name="body">Body</textarea>
0 F; `) D5 E1 h. m" B) K<br>
3 q) y* ~8 H% c2 F1 H<br> 8 ^4 D; p: K; E
<input type="submit" name="Submit" value="生成html"> 5 e2 H" h# G4 s# o2 D/ L
</form> - V% ~' V* d$ P& M) K' k% U
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
) |3 }% O" L8 t. ?" W) Ytemplate.htm ' //模板文件 <html>
/ s( [& _$ s% E2 {5 C' t<head>
; Q( b0 u% e7 ]( O<title>$title$ by aspid.cn</title> / j: S9 l# x# a ^
</head> % r) n! g o; w
<body>
, ~' N5 k6 o3 C) C6 l$body$
7 A7 l7 L5 S4 b3 C% E( m L8 q</body> , [% @0 H# K& P$ m
</html> ? 9 M- y9 Y$ x2 C6 l3 A; ^( l% Z- @
( N, n8 W& q# w0 c s0 ]7 uTestTemplate.asp '// 生成Html <%
% P' c! |: s5 d. v1 R! j. gDim fso,htmlwrite 2 V$ D4 x4 o7 I% |2 K8 T" I6 d, g
Dim strTitle,strContent,strOut 9 R. V8 _% O. A! W/ _. f5 `; V& g2 l
'// 创建文件系统对象 . ~% d8 {) i, e3 c0 |4 q
Set fso=Server.CreateObject("Scripting.FileSystemObject")
% {& P! f" l) w5 [/ s0 u" y5 V'// 打开网页模板文件,读取模板内容
( Z4 r$ [8 t$ _9 CSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
+ n: B/ O. F9 y, ~$ U! I( _3 tstrOut=f.ReadAll
; J* _1 H0 {4 H) p. dhtmlwrite.close * r+ h( k- Y N3 M$ |* p9 q. _ g, I
/ A$ N( n' \8 x# K9 |: t# Z; wstrTitle="生成的网页标题" 3 ?0 s) g( \+ l# L
strC
1 |8 F! k" \6 K7 W2 A) G$ O ?* I* e
$ t U3 W# ?& }/ g6 N: g' h& d' Z'// 用真实内容替换模板中的标记 0 X8 s7 |& H D$ C8 a- W3 w
strOut=Replace(strOut,"$title$",strTitle) 9 L& m- u: p f0 d$ t3 ?
strOut=Replace(strOut,"$body$",strContent) ! J* n# B8 t! a# H, v, Y2 O8 f. Q' i
& r9 n5 p* b3 X" U" D* a/ E'// 创建要生成的静态页
- ?" u# d6 K3 i. q! F7 KSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
6 u9 U7 v* O6 b1 X3 e
! [( D9 n- Z/ q F'// 写入网页内容 3 z; J5 _3 V E0 n$ D" t9 u
htmlwrite.WriteLine strOut
, m, d/ V) y9 p t2 h& F% Nhtmlwrite.close # U6 i3 O7 s0 R* Q2 Y
: S2 M# ?/ o, p( tResponse.Write "生成静态页成功!" * z, k$ ]! }% a# R9 s* ^: x
% E$ O3 H5 R% G1 R
'// 释放文件系统对象
$ d% f+ e6 W' b# A9 q o. T+ z. dset htmlwrite=Nothing 6 n: f! c9 k+ \. P+ g9 T: r
set fso=Nothing ! Z1 j, ?, {: |/ Y) l2 ]3 {- ~4 B1 W
%>
% j- K7 s s+ @0 |+ D s0 N' w: u/ v/ ?) u+ G
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 9 @5 b5 U% j% J) G' N
<%
, e* C- f- `" H3 x& V/ W# p
! A( ]/ f6 t% D/ O'常用函数
, I4 P# a" i; b! i0 S. K$ u- u3 s'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ; q; v# W3 {! ]7 |- H
function getHTTPPage(url) 3 M, C4 y& S+ Y% \% Z1 _$ u; _! Z! {
dim Http . o& P- a8 B! T; _
set Http=server.createobject("MSXML2.XMLHTTP") % ?/ { g7 g$ X
Http.open "GET",url,false " J5 H* D3 ]5 q
Http.send()
4 x" V3 X) K" v w$ j6 j, ?if Http.readystate<>4 then
# t) i. q# ^) t6 C q5 Xexit function
. W4 v0 Y1 i/ E2 w; ~end if $ B Q4 W2 ~+ R, F0 _- r
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") + p( v$ k* y6 }: O
set http=nothing
6 {4 ^7 _9 S( i" ~+ D. x* l" {! Cif err.number<>0 then err.Clear v9 b5 i0 @0 R3 ^" x F
end function - q) |) p# c I m2 a
5 h+ _# r$ u9 V8 }
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 . J+ B/ s* A. g# S8 o' B
Function BytesToBstr(body,Cset)
2 \' y- ^7 P8 k* A" f2 [dim objstream
i2 ~; G) u' v. b, `set objstream = Server.CreateObject("adodb.stream")
+ l) y' _5 ?2 `$ U" }& ]+ M4 ]objstream.Type = 1 1 U+ o; [" N$ X% P0 V
objstream.Mode =3 " h1 B9 m8 j4 U9 N, u. r' p, r
objstream.Open
0 A0 S+ p$ }- {0 Fobjstream.Write body
3 D5 [4 L' d0 b$ h! K4 t, Jobjstream.Position = 0
' E1 T; Q3 U* L" J; m( S* Mobjstream.Type = 2 , x1 X5 V; K- t9 q5 l. J
objstream.Charset = Cset $ z, f2 y2 T& D$ O/ {
BytesToBstr = objstream.ReadText # a! A" P/ k1 ^* }
objstream.Close
0 X% C1 w0 z1 b" r1 g I+ uset objstream = nothing
. i6 J# @( P+ c i' ~; a6 \9 c8 dEnd Function
; l; a% f. v' L4 {+ _6 h
3 l: T# Z* X! ] @1 I/ U1 i
k$ x& ~; r* f1 n2 TtxtURL=server.MapPath("../index.asp") / F+ U3 R0 h: B0 t) V( @4 R/ v
8 v3 a G! N7 @8 f* L4 lsText = getHTTPPage(txtURL)
j4 W# ^# P: {; C5 m' v/ w% t7 P: O: F
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 8 e9 |' |5 r8 N% ]+ X+ i2 S
filename="../index.htm"
' L# D1 F+ }0 j) p& \# u; G0 kSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 & v3 ?( O# p7 Q* p% i. {7 I1 C* B) Q
openFile.writeline(sText) 7 d9 Z# C8 E Z7 `0 e( x+ B7 G
Set OpenFile=nothing
% i a4 [$ j4 q: V5 x: H# I1 B
, A6 e2 [5 B' K; L%>
8 F; p; j- M w8 `4 g- ~) d. ?<script>
3 R* l6 h/ _( H8 Xalert("静态网页生成完毕");
, w/ Q5 y0 W Hhistory.back();
# L& P |0 E1 A) H/ U; p& S% o</script> |
|