|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14413
- 金币
- 2492
- 威望
- 1647
- 贡献
- 1440
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
" Q7 Y8 E) w% {- e* B像www.aspid.cn的主站就采用了TSYS生成html文件!
, |: ~5 p( c! H4 M: a( Y所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 7 c, S, p& h5 u. b# h
( x) I0 H5 L4 Y$ [* \1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
( f y( `( A% V8 Ufilename="test.htm" 1 b" s1 l/ B! s% ?
if request("body")<>"" then ( v2 Z. }6 U p y. e( }9 R' b$ s
set fso = Server.CreateObject("Scripting.FileSystemObject")
+ b* B& ]" A. U" qset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
+ d9 I# S5 Z" ?% n: G7 Lhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
& q3 t1 c* \. C6 d* I! I, R% O' }htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
6 O+ S: q! N& \5 K: _htmlwrite.close " u# Y9 p" d' k; R1 k1 Z
set fout=nothing
! B3 u8 c, }7 f( uset fso=nothing
7 h2 B) ?. k) J* p8 _; O1 h, bend if
8 z3 m: t# E+ s2 X, H+ [%>
2 ~- \' Q. ^+ H% p) y<form name="form" method="post" action="">
% |0 Q* S0 h, {+ J/ `* s/ v# y<input name="title" value="Title" size=26>
* ?# d% z0 h4 E( z1 I<br> ! d, b7 l; Z3 g" q. X( y% C8 l
<textarea name="body">Body</textarea> 4 u2 R* N: _* \; B
<br>
, ]5 L w/ \- s. q<br>
* H* {# @5 C1 p2 h<input type="submit" name="Submit" value="生成html">
" S2 M7 X6 R- j' [! \: @) k8 J</form> # v, F# Y% \9 G/ ^! S' O+ b
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 4 U, b/ |/ X& W% k! V
template.htm ' //模板文件 <html> 7 K0 X8 {$ n/ \: [: W3 \
<head> ) H/ G8 x2 I& o6 q' Z3 I8 K
<title>$title$ by aspid.cn</title> % B/ q" z- C* \5 b: ]3 I$ Z: x! m
</head> 3 o! Z- @" c$ |3 s, i
<body>
+ Y1 t1 @* ?) x: Y$body$
0 s' S* u3 @; r</body>
$ b8 [9 S% c* ?. u. S9 z$ c5 y1 h! [</html> ? 4 v& q! I: |. W5 B# n3 o
0 X" w) Z( x9 y5 ~9 M7 r9 g+ C
TestTemplate.asp '// 生成Html <%
. a/ d u" E! E* iDim fso,htmlwrite
& w. s2 j- g! k% [" U! ZDim strTitle,strContent,strOut % |: Q- t5 {- f
'// 创建文件系统对象 , H0 H9 W7 i7 o+ N3 V l- e
Set fso=Server.CreateObject("Scripting.FileSystemObject")
8 M9 ?+ [- U, p& d'// 打开网页模板文件,读取模板内容
- }3 \$ `7 F( ?9 G( {5 C/ a' m# ESet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
4 o5 o1 V x4 U) j2 ]) GstrOut=f.ReadAll ( u0 x2 Z4 o( E
htmlwrite.close
3 C9 ?) l+ a! F% T' ?) L( H( Q9 C1 S' L8 o0 ]. o
strTitle="生成的网页标题"
& O9 [- C! u! m: I, rstrC ! h+ c0 d& y5 {
* P) g5 d6 U q7 w3 @9 l' k7 M" k'// 用真实内容替换模板中的标记
, F2 H0 m6 K0 x+ C: e: W estrOut=Replace(strOut,"$title$",strTitle) 4 i7 V/ O- M, V. f* ^3 y& k
strOut=Replace(strOut,"$body$",strContent) & n" @. `. K$ T7 K4 n2 f
& O/ q' r1 z5 U'// 创建要生成的静态页
7 r. F! n; |8 f9 RSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 9 R8 N- [% F" X# F. f$ E+ o" F
3 s: W8 W4 K7 {- i3 V/ @8 _" b'// 写入网页内容 $ E, q0 b% v0 I" y0 E
htmlwrite.WriteLine strOut 2 t2 a+ P" O+ [7 @
htmlwrite.close ' w7 o: R0 Z( q- z& P6 O6 f
7 y2 X, y4 J, l! t9 e$ K
Response.Write "生成静态页成功!"
' X6 z7 G+ p' b& L, w, k
: w# u% S1 R, T'// 释放文件系统对象 $ q, A8 J o& P
set htmlwrite=Nothing : \9 E; H7 c) d0 `5 ?) j/ V
set fso=Nothing
3 h& N( |, T* a( v1 f3 P2 L& ~%>
- i% V. V4 K7 L3 Z. A3 p
* w2 _6 |, C. u, z. Y* t3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. w4 q/ ^/ [0 m0 y: f+ X
<% % t5 b/ w: [! W, Y9 d
5 y" `2 g" v3 x: e' s0 B4 ?
'常用函数
6 H( m2 e; D9 m+ [9 r( [9 ^2 H'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
! x7 W" ~6 N3 `' b# Wfunction getHTTPPage(url) . i: ~9 \- I Q5 X( f
dim Http * M6 {0 O# e5 F- A
set Http=server.createobject("MSXML2.XMLHTTP")
; z1 \3 t- ~; _7 j; lHttp.open "GET",url,false & D7 o7 H( P" F) q6 P. U2 |# e
Http.send() $ n% { q* D/ u0 R9 ?7 I
if Http.readystate<>4 then
+ i7 o, C9 ^4 K) p, E& G* Jexit function
/ W7 l5 d8 l6 H) i9 S% eend if
& K! Q( T* S" A9 u: E* x6 pgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
& I% z0 ^6 L4 p* pset http=nothing " I& K3 a! `. k" c6 }! W" `
if err.number<>0 then err.Clear
! ]7 [& g! [) \8 b1 Z- e, lend function
% t4 h( ]" N; m- S& p5 e* Y
7 \$ b1 k5 `- ~, F/ Q0 K$ @'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
# y$ D" C0 l1 p1 i/ S/ ~5 V; @Function BytesToBstr(body,Cset) 6 }" G' J7 J& K4 i
dim objstream
8 K8 X Z/ o- Aset objstream = Server.CreateObject("adodb.stream")
% ^3 u9 l% N/ i; q2 k7 ~$ O, s- ]objstream.Type = 1
3 R1 A$ n; @; S( kobjstream.Mode =3
' M0 W% v: M/ Jobjstream.Open 7 Q: M0 ^. t, c F
objstream.Write body
3 W+ }9 v* y5 y/ n+ u1 u; K+ D; [objstream.Position = 0
- M4 ~: f* \) t, x1 X% p0 T& Tobjstream.Type = 2 " A/ Q# D$ N( X
objstream.Charset = Cset
) f. J( U; z; m% ^* LBytesToBstr = objstream.ReadText 8 N) A1 J; @$ N: F; k" w! V6 t" \
objstream.Close
6 ^ ?% y# ]( l7 u1 I% `set objstream = nothing ; X9 P) G3 T" n8 @
End Function ( b" l! ?9 l( U4 L
3 _% G6 `0 O& ^' ]8 r
2 I, m5 B" r3 {
txtURL=server.MapPath("../index.asp") : p7 v/ f! d; O' A
/ m# Y6 }3 ~& V" A# k5 B
sText = getHTTPPage(txtURL) ' C% I$ x- f. M; _1 O/ X3 I
) o& p# h5 q Q$ g
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
( l0 x9 Q, S: X" S Mfilename="../index.htm" `4 I$ S7 z& l6 |# F) `0 H/ L
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 / |* O( Z c+ G8 Q) l
openFile.writeline(sText)
% ~# k0 M8 c- P" ?% A$ dSet OpenFile=nothing : e- x1 l0 s0 m8 o, L
1 G9 h+ a$ i- M7 F" ?$ l/ t%>
" U! }+ o( y' Z9 i$ P3 V: b<script>
* x% `6 e8 P! P% S: aalert("静态网页生成完毕"); % \ E4 }* `/ d% s8 U Y4 |* ^
history.back();
4 O" \# w0 |5 {% T2 |</script> |
|