|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14389
- 金币
- 2480
- 威望
- 1647
- 贡献
- 1428
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
: H! k2 f7 E# t5 S像www.aspid.cn的主站就采用了TSYS生成html文件! 9 S3 x- y9 v( u! |) }2 ^2 n% v
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. , I0 }$ o# x- l* @( k! S0 q, g( a6 |
; O1 l/ Y5 L- s& W5 ^ w- X3 q
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% ! f( l1 v1 h' K! R
filename="test.htm"
. D' m# v$ Q2 xif request("body")<>"" then
4 O) {& P, z9 J7 dset fso = Server.CreateObject("Scripting.FileSystemObject")
& E* ]0 d1 p7 ]; q% lset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
" e0 U) h9 t: _ Z" O* X: L' Jhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
U7 T: X% ^( chtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 8 A+ I( @* s2 C0 U) M$ Z
htmlwrite.close 8 p Z+ \& u% ?& k
set fout=nothing
. J5 I1 J2 A8 H0 H. c3 G9 cset fso=nothing
) P) G0 J& ?' k9 Z+ ?8 n2 iend if
' @5 K6 q# I r* v$ ~, ~7 C# S+ B( A" S%>
/ U1 S+ ~/ T( d8 p/ F<form name="form" method="post" action=""> : h/ v* t5 m4 B; v) A2 a; K
<input name="title" value="Title" size=26> + O; V: \5 P1 c: q. j6 ?6 p+ n0 @
<br> % R! f+ U, u' P0 ]
<textarea name="body">Body</textarea>
' _3 a* [5 _, C<br>
; h0 z2 h3 f" M* i8 R6 a9 J<br>
3 J/ _( ^( \" f4 z$ R% T! X<input type="submit" name="Submit" value="生成html"> 4 T" c _# X) F
</form>
" |* Z2 ]' j. x9 N2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 8 n+ @4 l/ L8 k8 Z, }+ B
template.htm ' //模板文件 <html> 2 u, H+ Y, j- o) [9 ^6 k4 h
<head> , b$ o1 y4 f r
<title>$title$ by aspid.cn</title> 6 d" z7 J9 |9 X; r# }& E4 N: H9 J
</head> - H* o) I# p' y; T9 E
<body> 0 `5 z1 P5 c+ {) ^
$body$ 3 L8 j5 v/ c f; m, J. `
</body> . m+ n6 J$ Y6 l9 t) f+ X
</html> ? & l4 l. j/ g, L, d- ~. Z8 E5 o
9 W3 \- Y$ l1 m9 {) a: Y) x- Z7 A
TestTemplate.asp '// 生成Html <% - u" `6 w9 z9 H* a2 n$ @# L1 H' B
Dim fso,htmlwrite 3 K6 o6 Q8 o* X6 [
Dim strTitle,strContent,strOut * p2 R. E0 L2 e, p- @1 S8 ^& J
'// 创建文件系统对象 " T4 L$ L% d" V; }3 z% ?3 J# W$ r
Set fso=Server.CreateObject("Scripting.FileSystemObject") & z; p* n2 e' x. [
'// 打开网页模板文件,读取模板内容 0 X: f/ @3 N _0 Y' f6 {) d5 h7 Q
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) # [. P: F' x9 Y" G. L8 o0 c. z: R( L
strOut=f.ReadAll
/ i* J1 @* n2 ?3 k6 i4 b2 B' m* mhtmlwrite.close
]6 w. x4 V7 M5 s- A5 A9 h+ G& i) P# C. @: h3 {
strTitle="生成的网页标题"
# i: j: c, P- a% o+ O% P) L3 dstrC
7 x- G4 M8 m2 Z8 K0 i# k6 q2 M) T5 t
'// 用真实内容替换模板中的标记
2 r- T1 I% p$ g* n0 f( W( R4 kstrOut=Replace(strOut,"$title$",strTitle) % P: y) m1 ?! n1 s% Y
strOut=Replace(strOut,"$body$",strContent) ' ]# X! p7 ~5 ~) r9 M
. b( D6 c. ?: `
'// 创建要生成的静态页
1 y' f0 O2 K! ?5 R! GSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) + H2 \% i! K7 }. H/ K. w( s, x
' X% X7 E" D5 P' |'// 写入网页内容 . V+ B, R+ X1 O m4 Z* D9 o/ d& F
htmlwrite.WriteLine strOut 1 f3 ]: p7 O; S1 r* j6 H
htmlwrite.close * ~0 H. c: Z" F; Y( F0 J* A; I
& Z0 E% _4 ` z# H% N
Response.Write "生成静态页成功!"
- k1 G6 p7 d8 |( `: }% _: k2 \
1 Q/ y t' ?- D/ @. l'// 释放文件系统对象
8 U+ h5 D. D8 T- gset htmlwrite=Nothing
( r- x$ x$ s6 e, Z6 ?set fso=Nothing
9 C5 P6 o# b! H/ w% i%>
- o) G* ~) ?/ L0 A4 h
/ X- E& e9 w- W2 L! d$ E3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 8 ^! a% [ i: U: f
<% . r1 ]* b+ S5 }
/ S: q Z2 t& Y) Z7 l- n" p8 |( i
'常用函数
6 J* Z1 p3 }6 q1 `: ]- Q, z: I'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ]. K: R0 l: F$ z# v' o
function getHTTPPage(url) ) g U, M( g& e
dim Http
" T- B# a- d; G0 z& q3 Z0 jset Http=server.createobject("MSXML2.XMLHTTP")
! B7 X9 X/ @4 ~& r" D* p a! G) J& jHttp.open "GET",url,false ' `( c5 l& |1 t" J3 M/ H0 y
Http.send()
* Z: C/ g' k6 Sif Http.readystate<>4 then 2 y) A2 y! y' x
exit function
: a* O+ _7 E+ ^/ {, m9 i6 ?3 lend if
' M; V+ }# V; ~; y" W( Y8 QgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 2 [# {# P& P( o; q
set http=nothing
- h- f& V$ n& P8 `4 m: ^5 Lif err.number<>0 then err.Clear 7 u/ u" l% P7 a" |3 u, R6 k2 S
end function 5 _6 x; x$ n( b q6 P4 U6 J
- B( B& e5 ^/ H5 F6 ]
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 2 K; ^( m' C/ C; A
Function BytesToBstr(body,Cset)
( T; u" C7 v# W) m1 S; a# ~dim objstream 5 B7 J2 \0 O0 p5 s3 j2 l* y. y
set objstream = Server.CreateObject("adodb.stream") , a3 y# O' s0 p' S4 c5 O7 z0 z# N3 o
objstream.Type = 1 + x8 t* E; ?7 }( p: h$ F3 {
objstream.Mode =3
/ n/ F3 ?% g. G9 hobjstream.Open
1 o7 \7 M3 p) O/ f# eobjstream.Write body
6 i- W2 s! F; Z: n: X; L% vobjstream.Position = 0
) k3 F7 ]! p, Cobjstream.Type = 2
+ W" g3 G# U$ R6 i9 ^& |objstream.Charset = Cset 0 h# K/ t( a( W8 u1 J2 f
BytesToBstr = objstream.ReadText % D# I4 i1 n$ e: W4 M
objstream.Close ( C; x& I+ {. H/ m) P6 Q t8 y
set objstream = nothing 9 I. s& g) ?3 D7 Q- f
End Function
/ e, }& r+ M( e
# f+ M: d& W4 H+ |4 q: m- m4 O- Q" J' ?% W" c8 f6 |
txtURL=server.MapPath("../index.asp") & O$ A3 E0 Q, N$ n: y
) L5 I5 p( Z( ~& O+ x$ Q2 L/ x
sText = getHTTPPage(txtURL)
$ }/ `2 Y! m2 D. U4 a! C. b% w& ?9 A5 H4 I- p. l
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") . J. r# N; I- r; H |0 P! m8 k7 E
filename="../index.htm" 0 ]* U+ w( c: h' h! r3 ` {. z0 [) n
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 & l8 F+ g" M; s- _9 m
openFile.writeline(sText) 6 Y3 O1 f) U& B y7 L4 y; A& Y7 [- q
Set OpenFile=nothing
0 j" \2 \* z! g6 |
3 d8 i5 I# ?9 G% ?' V%>
# _5 ]$ A- \3 s9 _/ y7 }- y<script>
: h( e4 O" G* Zalert("静态网页生成完毕"); 6 y6 g3 d7 ^0 c/ }1 R% D+ G
history.back();
* P2 }7 O+ v1 `- C: E</script> |
|