|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14357
- 金币
- 2464
- 威望
- 1647
- 贡献
- 1412
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
" \. S7 P2 w' }6 a" e/ v7 u像www.aspid.cn的主站就采用了TSYS生成html文件!
/ j3 p3 p( k6 i- [% z1 m2 C- s所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. - X5 X- Y& R& m4 R+ f" `0 ?6 Q
3 N- M0 ^2 K2 d* P. O, q
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% , R2 u) F* q# s. l; k1 G" d
filename="test.htm"
7 ?% {8 `0 a" E; ?4 P( g+ ]if request("body")<>"" then : J5 ?# m! Y# F, N$ A
set fso = Server.CreateObject("Scripting.FileSystemObject") 2 O5 O2 @$ Q7 `0 z& y- _- S
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
! \ h; i t) Y7 Khtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ) N. P& |9 w) n. i6 p5 g- A
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
8 _. A- L+ |/ l' E0 Uhtmlwrite.close
4 G: Z* d8 v0 c% S+ l2 R- ~ Xset fout=nothing
& n% s/ t/ I" ?; v9 uset fso=nothing ( V9 T6 b. G/ W' M/ h
end if
e( t4 {: x+ p# E4 y# O%>
1 m3 m+ e. }, f2 m! q<form name="form" method="post" action=""> 3 i$ M1 N" x- V4 n; c
<input name="title" value="Title" size=26>
8 {4 G; ~5 W3 d6 W$ s+ q! ~<br> ~) O3 `# f0 Y* V
<textarea name="body">Body</textarea>
# Q5 `8 F# v: M9 w0 S. j6 F<br>
7 w! m) c2 n7 I) ^<br> 9 Y0 B6 R0 [& A0 Y" z/ {6 {" L. q
<input type="submit" name="Submit" value="生成html">
2 L* `! [7 O5 O</form> $ ?. N& ]% C" G# B5 J
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 6 c* ^5 G1 C5 U' T, |
template.htm ' //模板文件 <html> : Y" H2 ?9 O0 \% [
<head> # O, }4 h" {! L9 k0 n
<title>$title$ by aspid.cn</title> 9 D: L- k4 F: f( N9 n5 x
</head> ( J8 d: W/ Q6 Y5 A, I+ r! J
<body>
/ @' ^+ ]6 Z- `% B# u2 o6 D, {) S$body$ ) R, u5 }9 [ n9 b/ |) d
</body> ( I2 ?& ~, E8 ^7 h- B7 s4 V
</html> ? ( Q' h( A8 _' o, I8 j6 e4 P+ a9 P2 c
$ R$ z2 f3 v4 F9 O, j
TestTemplate.asp '// 生成Html <% " f: D- `8 u/ d9 R
Dim fso,htmlwrite
7 ]6 E$ V: g% R8 VDim strTitle,strContent,strOut 0 h7 `, ~6 ~! s+ ?0 I
'// 创建文件系统对象 # _5 Z' g& \6 C1 u& h
Set fso=Server.CreateObject("Scripting.FileSystemObject")
L2 q5 y l, D. f: N+ U'// 打开网页模板文件,读取模板内容 5 i/ e* {' u6 b& [. S
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
/ k, r8 g) D. K1 u$ S1 zstrOut=f.ReadAll
8 R6 ?7 L5 ^& H& r5 nhtmlwrite.close
8 _7 A, @0 c j* n2 ]. W7 K* R/ ~7 g9 ^; X% m' ]# `! K" c
strTitle="生成的网页标题"
- X8 j' q6 w# Q& UstrC
8 T( i$ B2 {. @) w7 D& W/ C8 Z/ g
'// 用真实内容替换模板中的标记 & x/ l5 X5 f; |5 y8 n1 n
strOut=Replace(strOut,"$title$",strTitle) 4 J3 r6 [" V5 h: H5 |8 C. b
strOut=Replace(strOut,"$body$",strContent) % D) `# q y$ C0 D n1 [# n; ?3 ~
, B5 C- p( o7 q" b5 b: S D'// 创建要生成的静态页 + R. V6 W2 V" j" G6 m( q( y
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
, N. G% Z& q( }0 p& A, z
. i, g4 f* }6 t'// 写入网页内容
+ d* F$ \0 q$ U g. d& K2 mhtmlwrite.WriteLine strOut
9 T& o/ Y7 `4 s2 D2 \htmlwrite.close ( q1 t9 L; @1 l* }: T
* U$ V6 \$ }, }6 l
Response.Write "生成静态页成功!"
$ X! {: I8 C4 o& |1 k7 e; ^. l) A7 c, y3 k2 Q0 @4 M8 G! \
'// 释放文件系统对象
, E- I& Z3 u7 D: k5 {& Vset htmlwrite=Nothing
2 p" D2 B" g- L- Z' m; sset fso=Nothing
4 Q$ @1 @- o4 b: h7 g$ d$ K/ Z%> 5 Q7 ^& w& c5 p9 a- N: c
) ^/ e9 \9 e: c; p9 S
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. d/ P: B' g* y9 u5 O1 Q1 @
<% 4 f1 I& t0 F( U9 M
" h3 ^( v7 y7 e5 [ [* w'常用函数 ( f( j# W) h3 O3 Z7 g( B2 Y
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ; S$ \4 M9 ~ l: [; U
function getHTTPPage(url) , @4 I* o' B# [' g2 A3 n
dim Http % f% P) `# J7 Z
set Http=server.createobject("MSXML2.XMLHTTP") / n. j7 i0 f: F1 N( u0 X
Http.open "GET",url,false
) T$ l: |0 s. J( y9 @3 GHttp.send()
) F$ r$ g- v$ ?if Http.readystate<>4 then 5 `8 Z! Z8 v* ^, @8 {
exit function
$ G: [+ @/ L/ n$ F; Z/ v! P+ qend if . V3 _$ J0 T0 ^: T
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") P7 v+ N8 h, M% L. S
set http=nothing 1 \6 z9 K5 m4 \" n' ~
if err.number<>0 then err.Clear 0 D* v! U9 P% ?
end function
: O& {$ F ~: U5 |) z
o" e$ b; K% F, L: d% m8 g'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
: g. J( M& G7 VFunction BytesToBstr(body,Cset)
, `7 A8 h; ~+ X3 `! z' sdim objstream . I; Y% K/ n9 p" L t6 C9 h0 F
set objstream = Server.CreateObject("adodb.stream")
3 Y3 d4 \) m6 z+ e! aobjstream.Type = 1
8 q+ a$ {0 @$ ]% X- M( ^objstream.Mode =3
3 v! ~! l! x) a+ S& [objstream.Open ! m4 Z# o" {4 n4 {# G2 b' u! C
objstream.Write body
( M; v, y6 m6 p6 H* l% hobjstream.Position = 0 3 Z. H& D7 \0 m" G$ Z2 v; `% ?
objstream.Type = 2
4 K( g% p$ x; \- O2 R$ z, c, Z# Nobjstream.Charset = Cset
, p, n Y% S8 P6 A5 N: W( T0 IBytesToBstr = objstream.ReadText # c! u. l3 i7 v
objstream.Close
1 e8 J' q1 X" kset objstream = nothing : t; c3 D( @) E9 ?
End Function
7 p$ f. Z7 O8 d& J6 A
W5 W& F$ j* |
% k& r5 H o- rtxtURL=server.MapPath("../index.asp") / M$ L! {& u7 |6 k( @6 H9 P4 h' h6 M8 w
2 m% \! x+ {9 f" M: b9 Y/ o* [
sText = getHTTPPage(txtURL)
5 A5 M: E- G; e9 d1 W7 g& ~7 W9 z1 T" Q
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
+ t2 `) z6 J, p/ C! Rfilename="../index.htm" * g2 b' _" y& h @% n$ d( T0 f7 z0 a
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
, Q1 l6 O ]! m vopenFile.writeline(sText)
R! p, k w$ @/ _0 _) \Set OpenFile=nothing & `" F# G; ?# C5 B5 J1 o" V& \
( S& O$ D7 k& D( _1 Q8 b9 b%> " q6 ] t. F. L0 [
<script>
0 W/ p; f h, L# x. F! r2 X Kalert("静态网页生成完毕");
! p+ A! a" M* ~! n( i+ uhistory.back(); " w1 x4 j+ Z( ?( Z7 m
</script> |
|