|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14389
- 金币
- 2480
- 威望
- 1647
- 贡献
- 1428
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
6 x2 L R3 E- h V像www.aspid.cn的主站就采用了TSYS生成html文件!
! i- K4 {* X$ I2 n o, x所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. $ J( Y. c- ?( [/ \
8 |" E# s% f5 S5 s! X1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% ' ]5 i/ t, r; K4 p7 r2 }
filename="test.htm" ' U \/ ]% ?" o! U) d
if request("body")<>"" then 6 b" S8 f2 N Q' M5 Q4 d
set fso = Server.CreateObject("Scripting.FileSystemObject")
' f+ Y K* ?$ Lset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
) Y9 c) n% |, y) v; f! a; Bhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" + ^0 p7 K% @* N# v4 r9 B4 n+ S
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ! V8 I: w5 A; X) d5 J" q
htmlwrite.close 4 w& @0 l" K2 @
set fout=nothing 7 X" k+ Z% v; I% F" x
set fso=nothing
, x5 e3 H1 v" Y" iend if
* f( p( v( m: p9 ?( G$ c8 f. ~%>
! h" Q" L1 L5 H3 I<form name="form" method="post" action=""> : N& B7 a! {8 n5 O9 i
<input name="title" value="Title" size=26>
) l' j& M. F( x/ x* i<br>
/ y2 h, B- [- z& \" D9 O<textarea name="body">Body</textarea>
1 [; d# n2 u3 i6 h# D! ]9 P, O) u<br> . z- ~; D2 e. A3 t. D+ E+ a8 `2 s
<br> 5 U4 j5 V9 B- h- m0 g! ]0 j
<input type="submit" name="Submit" value="生成html">
* b( J& m0 d, A5 Z) a</form> # Q% K# Y, V* {& i0 ~; p
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 2 j% F; Q6 D- ]$ L. g, C& ?
template.htm ' //模板文件 <html>
* H' L+ G# P4 O! |; F* S<head> 9 H" a. H i6 e5 {. [' O
<title>$title$ by aspid.cn</title>
9 Y' s# O4 c2 r</head> # Z' s# s1 c( K; u
<body> , t5 {% F$ T9 h) i2 E
$body$
" w( O& k+ S3 c" Z</body>
' e% o7 \' \3 D</html> ?
7 a7 S% x% J. G7 r) d0 v
8 k" p5 x* R! ]( }9 ~2 UTestTemplate.asp '// 生成Html <% 2 s) L5 q! }/ Q% Z2 W* r% k5 e
Dim fso,htmlwrite
0 }$ n0 F1 U$ l0 l5 [Dim strTitle,strContent,strOut
% y0 n; f( \+ k7 ~! s# F5 {/ ['// 创建文件系统对象 9 l$ w, d/ R3 {3 [4 K' a. w; x7 d" j) G
Set fso=Server.CreateObject("Scripting.FileSystemObject") 3 d6 K2 @4 b' }
'// 打开网页模板文件,读取模板内容 5 i7 K- F5 Q7 {
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 9 c1 g6 R1 p( j5 G
strOut=f.ReadAll
4 o3 k# _6 C9 Q3 Dhtmlwrite.close $ i; |9 }! ]4 P6 K- ?1 q
5 ?% I5 [. l, ?' v
strTitle="生成的网页标题" $ b6 n( x4 \; p- r- q
strC
1 r+ h. h2 g, ]4 |" |+ S8 P
% i$ Z: W( e$ n/ |% z1 N# I'// 用真实内容替换模板中的标记
1 N6 m% K$ Q4 b2 ]! f7 n" y) XstrOut=Replace(strOut,"$title$",strTitle) 7 y" T! i* \/ ?
strOut=Replace(strOut,"$body$",strContent)
% C* m1 t( i2 l; c& T' b1 x$ V- y% q' u, a+ _! \
'// 创建要生成的静态页 - t. u5 S9 b& e& y: l C
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
7 Y) d2 Y8 s/ `2 L3 v) Y6 r. ^) x# e' p$ ~. \$ J* q
'// 写入网页内容
3 u) a5 }8 o# Y. ?5 Y( Yhtmlwrite.WriteLine strOut 3 G3 |& E3 Y4 }+ a, X5 r
htmlwrite.close
1 x' V. ~! @$ Z0 f+ O \) N' ^( ~/ N5 V- B
Response.Write "生成静态页成功!" ; e: C0 {; O/ Z: |% y
1 X e6 ?1 n, U2 ]6 }
'// 释放文件系统对象
2 ]( {$ [8 X: Z+ m1 Uset htmlwrite=Nothing
$ Z B2 b3 x- H9 aset fso=Nothing
* H b1 d: q& s4 L%> * I1 X2 m/ a# ]1 \) d
$ L! R+ Q5 {/ S
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
& k* i6 A/ ]. |: y9 N<%
2 G% W2 f# U9 ~, p$ x8 J4 b( {/ T" ?3 M2 P# T
'常用函数 ( n+ H: O+ Z/ l
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 * r q N) z+ c: b
function getHTTPPage(url) / c2 G% T8 w' S4 I, A
dim Http ( M) t/ S/ d- A9 ?
set Http=server.createobject("MSXML2.XMLHTTP")
7 D+ s, n$ a1 u1 X/ c- i" yHttp.open "GET",url,false
3 V' f v$ n6 h kHttp.send()
$ f _/ J, X2 d; z( m% g# pif Http.readystate<>4 then / _7 A# s: D6 ^+ j' ]
exit function
5 V# ~3 C! Z: }& Y J" h! Z/ pend if " w# o3 \; U; i& p5 V
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") . `/ A. Y6 C0 F
set http=nothing
. L& A. l5 W1 }& h' sif err.number<>0 then err.Clear , l' r, E$ w+ j2 l: ?. r) M8 @
end function
. u* C/ S+ T( w t
. Q6 E9 E+ q3 Y: w+ ~0 R# P" H, T. z'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
/ D9 c# E; V' Q9 P M: `" HFunction BytesToBstr(body,Cset) $ N+ w. j) I% H, p9 X
dim objstream 7 q2 Z4 |5 B4 H4 B
set objstream = Server.CreateObject("adodb.stream") ( G0 \* Q, l+ i. I0 D3 j7 b: H
objstream.Type = 1 3 J8 z8 r" Z- { H' F
objstream.Mode =3 ! p2 P0 y7 i- E, f" g
objstream.Open " {0 l7 F: B( O$ }
objstream.Write body ) x* A( T/ X* ~$ g9 I
objstream.Position = 0
) [0 N1 q" ~1 g9 W+ n& \* l Uobjstream.Type = 2
8 [ m* |, E5 @6 y0 v/ o cobjstream.Charset = Cset
/ G3 L3 a5 Z% t2 q' PBytesToBstr = objstream.ReadText % Q! g- M+ g% f: t" A+ e' `
objstream.Close $ k4 _2 ]8 }4 F
set objstream = nothing
8 y( r5 z4 ~+ V, A% m5 A8 OEnd Function
# z; w5 h- A7 {0 P% l
! Y) X; [: z( M7 Z
6 H- ?9 n$ ? [: `4 {+ xtxtURL=server.MapPath("../index.asp")
( K1 b9 p* N# h1 _
2 F+ x; ^! L3 jsText = getHTTPPage(txtURL)
! X. ]' Z6 C" ~/ f" X& w0 s4 ` m5 q
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
7 x, I0 p) G. pfilename="../index.htm" ( Q9 F. H$ b' S7 l
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ( d) m p7 E4 ?* O7 d( Z4 |
openFile.writeline(sText)
& d6 f [) H4 |) ~Set OpenFile=nothing
9 C: g- t# W( ]9 E2 ]# {6 E* x! u4 C7 L( z/ J9 A
%> 9 e2 }1 r4 w9 G F% L, n
<script> 6 f5 _& W6 K: c) Y7 Q$ L
alert("静态网页生成完毕"); 2 b& v# \/ J8 c% U% u
history.back();
/ v; D3 h# Z# Z4 D0 m N" L9 j) a8 t9 w</script> |
|