|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14285
- 金币
- 2433
- 威望
- 1647
- 贡献
- 1381
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 5 z, d+ a7 ^0 @
像www.aspid.cn的主站就采用了TSYS生成html文件! $ a+ ?/ x4 Y3 o' s d& U3 X7 w, C! T
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
. O$ n0 |% Z5 [; S
* Y9 a7 }9 T5 [1 }: G' U4 Q7 y$ V1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
3 m7 t& e3 ]5 Vfilename="test.htm"
% w& h: H9 Z8 }if request("body")<>"" then 3 h: t$ K) F ` V
set fso = Server.CreateObject("Scripting.FileSystemObject")
' L I1 A& \7 g2 j- s% d. {set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) * M7 B; |. l8 X+ N; ?
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" , @+ ^6 l; ?, x3 v
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
( S; E+ f" C* ^htmlwrite.close
" Y2 c4 |# b+ i6 n4 rset fout=nothing
4 e: U. q% [- \- e; M* b/ N9 ~set fso=nothing 2 o* m) a( T, T7 ~( B( f
end if
+ u0 \& P. ~, L& ` ~* J%>
1 I' A9 j/ }5 C$ F: h$ F) |<form name="form" method="post" action=""> 5 V. p% o+ j8 O( y# _% P
<input name="title" value="Title" size=26> , v, \4 V2 N9 I8 i; `
<br> ) _, F9 H( \8 c! g9 k2 x
<textarea name="body">Body</textarea>
7 `& g4 ^+ I; L: w) k' m, Z<br> + g1 U4 p" v' C s/ ~7 H$ a
<br> 7 R' c3 d/ e( _: \2 B9 M
<input type="submit" name="Submit" value="生成html"> 7 t+ d) i# X1 _" C2 F: F* N) Y" U
</form>
, l' e+ a0 k& J* ^8 D2 h2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
0 R4 Z& S: X$ g4 Atemplate.htm ' //模板文件 <html>
* |3 G2 x R$ }7 [/ Z1 C<head>
% L; L- m+ {. {9 [<title>$title$ by aspid.cn</title>
/ `- W4 d8 s8 V: c</head>
. E, A- g* u# |3 A% Y5 O* x<body>
+ u5 U+ M$ B5 }3 C5 K$body$
6 v! ], H! S' m: t- }( @, M: B- N</body> ) \% [( s1 U. i0 U& F6 Y* {
</html> ? % e r* ] D8 l" n: T
" Q0 [8 K. A* j; {' XTestTemplate.asp '// 生成Html <%
, S( M+ m& c1 E9 L' e; FDim fso,htmlwrite
+ e7 B$ o: {) R4 a% J6 ]2 H3 |Dim strTitle,strContent,strOut % J) f0 ?& p5 ~/ k) E+ n* P. A }0 b
'// 创建文件系统对象
1 s& j# e' k5 K# r. LSet fso=Server.CreateObject("Scripting.FileSystemObject")
; O. G0 s' Y% N'// 打开网页模板文件,读取模板内容
; z( \6 t6 p# c3 J& L: m$ C. @Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
! A7 w$ ~% Q6 A1 U* u' FstrOut=f.ReadAll
/ e+ L" j9 n' L/ i7 m" V' Chtmlwrite.close
$ B! V- }+ J4 N& w% H
' g3 _0 x9 R2 V YstrTitle="生成的网页标题" + A/ M7 Y' F6 Y/ h/ }: U
strC - B) K3 k. n) I5 e; o
9 j! w4 h" ~0 N# S& k! C5 Z'// 用真实内容替换模板中的标记 . c; _/ G- I* [5 y$ [9 W
strOut=Replace(strOut,"$title$",strTitle)
- Y* \. X* m3 m, RstrOut=Replace(strOut,"$body$",strContent) 7 X3 I6 ^' J0 @9 Y' p( a( d
$ T- U- T2 S- K5 Y$ ]7 @6 ^
'// 创建要生成的静态页
4 ~0 N8 |2 x ~; B: O8 ]& vSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 0 `) X s6 J( @; ?1 a2 v
: ^: {0 u' c7 h% L% R
'// 写入网页内容 / m; f3 R* |2 Z. a0 s5 E9 R' z
htmlwrite.WriteLine strOut
* ^! i" w2 Y1 R- O, t' }htmlwrite.close
2 w& u K3 {: K; [2 f2 B b5 J+ [8 `; c0 M) ~2 z- Y/ X3 m
Response.Write "生成静态页成功!"
; q2 }5 b( Q( k$ W0 Y+ A$ `: _ \8 y( r& `! b
'// 释放文件系统对象 1 y# w7 s6 \8 f" i$ E1 Q2 G
set htmlwrite=Nothing ! b+ G! p9 `: ?
set fso=Nothing
) F" c9 Q5 F8 F, t%>
& n9 v# K2 W7 E/ n2 }; W [6 [+ C' X+ L* g5 p" l
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. & i4 T. X1 Q/ M% ?
<%
. `) J- V3 S* p6 ^" t$ b: G2 _6 m$ E0 i& z% X
'常用函数
5 s! p9 ]5 K! O: r4 C% _5 T. o'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
/ p# T @/ {2 d$ X8 a0 z qfunction getHTTPPage(url) 7 p3 a5 \0 I1 z( Q. y5 L- ?$ ]
dim Http
0 r* r2 ~0 h% G! ?7 b ~# N" {set Http=server.createobject("MSXML2.XMLHTTP")
* O* e2 d2 d9 Q$ @; V3 mHttp.open "GET",url,false 2 L9 y1 p/ S& @ L
Http.send() ) k; Y5 R7 e U& a
if Http.readystate<>4 then
: p# V# Y- B: S" }, O" mexit function
1 ]6 q" u6 Y' O: w- |0 W& Fend if 4 f& c% S1 j% [7 X9 r
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
1 Y4 @% W! T v: o, H1 Jset http=nothing 5 L2 ^+ \% i! N/ B) g
if err.number<>0 then err.Clear
' c/ w- h) |) \" u- w, {* ^5 ?+ Lend function
& v: O* a4 g- u n. F: z! }. r, t8 n2 ]
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
6 O# r3 m! i( @8 e K+ HFunction BytesToBstr(body,Cset) / n- \, ?' c6 A G7 Q* \0 ?
dim objstream
2 s: T0 d5 W8 B- Tset objstream = Server.CreateObject("adodb.stream") ! E1 K% Z/ z1 f; A
objstream.Type = 1 5 H" ]9 I* a2 p0 c. y+ X: Y4 J$ N
objstream.Mode =3 4 u! P1 _, W# g3 d
objstream.Open
' p6 |" `3 z8 |6 [2 s* A p3 C4 N3 [objstream.Write body
( o3 ~! E! v% p/ k Q7 Q+ Cobjstream.Position = 0
+ n) }# j9 A* S- D6 E' o$ q- lobjstream.Type = 2 8 g, u1 c! l- X2 |4 ^) n
objstream.Charset = Cset
9 A, [* ^" l& C* m! u3 [9 ]BytesToBstr = objstream.ReadText & }3 T* P' w5 K9 w2 d- [
objstream.Close
9 S5 R4 a9 h6 j! p' bset objstream = nothing 8 \8 j. g, x, z( r/ m+ ?; E3 b- g
End Function " P; h( N4 f* Q) J+ \4 p
* [+ }( a& H+ [$ Q; p
7 g2 v0 {" ^+ i4 P7 wtxtURL=server.MapPath("../index.asp")
( G& ]8 a. X3 f9 y# g2 u5 R- B, J6 [7 [# @3 o
sText = getHTTPPage(txtURL) 5 d$ P. b4 O+ O- C4 M$ H
) _4 d1 ~' \# \Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
- ^2 h/ r1 }( W3 Z! A% _. ^$ f0 vfilename="../index.htm" $ G' B; p7 p0 c% ]
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ( s$ v. o2 e7 A: X
openFile.writeline(sText)
$ l J3 ~2 K* f3 G, nSet OpenFile=nothing
5 e* w s0 q, h0 [
6 |6 j7 h9 p2 J) D F5 {%> 8 P. }7 \: M5 z# @ b
<script>
9 D7 c' K" `3 F( i- lalert("静态网页生成完毕"); , @! h/ t5 v8 B+ |; G
history.back();
: w" L$ u9 n y: }</script> |
|