|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14429
- 金币
- 2500
- 威望
- 1647
- 贡献
- 1448
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. " x9 g' y* \" C) E! [
像www.aspid.cn的主站就采用了TSYS生成html文件!
( M. m! V5 |# ?$ h8 R5 O+ t所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 7 e) \3 O$ Z0 D: i+ F
8 u x9 o' w, K4 W# `$ I+ K- G1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% ' i: r9 O- O% Y9 G' ?
filename="test.htm"
, H% _4 S# l- k% Fif request("body")<>"" then : m ^* N9 E0 q* P: w i5 C
set fso = Server.CreateObject("Scripting.FileSystemObject") ( }0 J/ t0 p" W
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) % K* ]; l. e3 p6 C" f3 H2 X& D3 s5 y# i
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
# [- B. \7 i. Y: q' Xhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
# z/ \# y" W$ M$ y! whtmlwrite.close
' b6 }9 Y7 a7 j. e0 P* I3 a8 t4 u8 fset fout=nothing : b* e# @+ {+ s$ {9 P7 ~" m
set fso=nothing
( E" s t1 u# R3 k8 w' v' Fend if
, p# J6 t9 t& q9 [6 Y6 w%> 9 h3 k2 ^6 x- K! z: t
<form name="form" method="post" action=""> 6 u1 H! p1 E& X' j
<input name="title" value="Title" size=26>
% `# N* i# Z" [<br> 6 ?) ?3 U% v) e2 |* @
<textarea name="body">Body</textarea>
/ c7 @$ C9 R9 Q<br>
5 D( G8 Y a3 C<br> ! G& A" n' R- r5 T1 L0 p
<input type="submit" name="Submit" value="生成html"> # D- p: o* S7 p, L8 @2 B
</form> 2 w9 T! H7 z1 E/ L
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 4 D* n a3 S" {8 P4 b
template.htm ' //模板文件 <html> ! H$ M% D2 P2 f
<head>
$ C. o3 R3 z3 q8 ]" q0 M& w/ O A<title>$title$ by aspid.cn</title> % z0 {, M3 {5 @ t- ]5 j
</head> ! B0 h+ E S& W1 D: |
<body> 6 \( n+ f/ {) J% O) S1 d! [
$body$
( q, K0 M7 @- C& m- {. W% i</body> . ] P8 c# R9 ~
</html> ? * l) x# E% }/ L
6 f3 K2 G% y& _' P* R# YTestTemplate.asp '// 生成Html <% $ O7 h+ [3 R3 ^ Q" o
Dim fso,htmlwrite + q1 Y5 L5 W4 @; C& F) M p4 C' O
Dim strTitle,strContent,strOut 3 D/ D( `$ ~1 p9 p
'// 创建文件系统对象 ! n: R5 S. Y5 n: @- F' s0 d9 s t; h' J
Set fso=Server.CreateObject("Scripting.FileSystemObject") 7 n4 j/ U) l1 E7 q# P, y
'// 打开网页模板文件,读取模板内容 4 l* o4 X" I* l3 f
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) ' t4 f! |; I2 H" c2 R$ \- u
strOut=f.ReadAll ! E" Y4 s" a3 A9 I4 M* [2 m& d o
htmlwrite.close 8 M/ S# q/ R4 V
9 S* k) k% G: o) |, E( {+ c
strTitle="生成的网页标题"
$ J/ H0 X3 R" @4 S/ X; {4 HstrC
) M; P) y+ ~3 p; @6 p0 I
! c0 Q* T! K. _- Y% j6 x8 _6 O'// 用真实内容替换模板中的标记
3 j( h. ?. Q! N# E' j4 NstrOut=Replace(strOut,"$title$",strTitle)
$ C8 ?- f$ o* O+ r$ VstrOut=Replace(strOut,"$body$",strContent)
5 O" \) w) D# h# `% M+ u- @: c) Q2 m0 W1 y2 P) K6 k
'// 创建要生成的静态页 + \' E+ I( D! B% p
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
+ I h9 C# d2 K, c) v6 b6 Y
3 }8 m8 x6 n _8 m6 q'// 写入网页内容 9 Y& ~2 q I5 A
htmlwrite.WriteLine strOut ( w. R. q; t. M4 G1 R( b1 _0 V
htmlwrite.close
5 G8 q; H2 g- R+ j. w& \3 R0 u/ N- A! n
Response.Write "生成静态页成功!"
3 J; P" R* ?/ V- p p; b7 M" l* d0 L1 _
'// 释放文件系统对象
6 R" E2 { F; iset htmlwrite=Nothing ; X8 A* l7 m( w& G7 {) }0 N- K6 d
set fso=Nothing
& Z8 S3 z9 l7 _, g! H8 O+ m6 F%> 2 `+ E5 h4 e7 X8 @7 ~& z$ @
7 E0 l% Y! S: m' t/ ^3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
2 J6 I- T+ F# Z6 A<% * w) b; | t: W( ~6 a$ a
2 p, [ F# }: c( X6 q" S'常用函数 ) _ T/ ?. c1 T" U0 f
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 6 V% N- _$ m, ]& n ^" Y
function getHTTPPage(url)
0 O6 B( g# o Q0 cdim Http 4 ]! t) c' l k( ], I7 Q
set Http=server.createobject("MSXML2.XMLHTTP") * C2 O2 H2 K9 c* B
Http.open "GET",url,false 5 `2 w$ c* U# t
Http.send() 8 e- a" _! }" d9 \! F
if Http.readystate<>4 then
3 W# h2 B' B& n* kexit function 5 ?8 R4 ?& c. V( m, ^2 f9 i, }
end if $ X! Y; Q% b" A
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") ; G3 A" q) F6 Q% S/ A( p
set http=nothing
& t- L( m! W- Z# n+ w j8 Sif err.number<>0 then err.Clear
, u: W3 Y: f7 y1 |1 a8 O/ zend function
& m- {' k; P; m8 a+ H: N
}$ `; J/ h$ }9 O'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 ' X5 N( V. E" } g' q" \8 h
Function BytesToBstr(body,Cset) 9 g, O# r9 Z# \* b4 x
dim objstream
: _' K2 D# i7 M, K) _; k7 c8 Z9 zset objstream = Server.CreateObject("adodb.stream") # {- L2 Z: ]: P
objstream.Type = 1
: h+ ~; O! D+ j& q, G( A yobjstream.Mode =3 1 s/ B, W* [$ u3 G
objstream.Open
9 l" Y! |/ _1 }* [7 T, s9 ]objstream.Write body ) q- G6 L1 T& x/ _2 ?: s4 D
objstream.Position = 0 ! l3 ]6 @6 P, m( ^ v7 S) |: B
objstream.Type = 2
' B& l" [2 z( H0 v2 i, k4 t& sobjstream.Charset = Cset 1 K! @5 L f _
BytesToBstr = objstream.ReadText
5 A. t7 ~' K2 A9 ?- gobjstream.Close
+ K X- j! l& F" S( h2 Xset objstream = nothing 6 S/ I! c$ l: r9 o; A8 A- }
End Function + ]% _6 z- s, R1 O5 T8 S
" {; V3 n; a! O H9 X6 S, |1 B
% r1 @5 Q: ?% h. ^# s0 h5 |txtURL=server.MapPath("../index.asp")
V- h3 @9 L8 h% \/ K, W* T$ v @( s9 l
sText = getHTTPPage(txtURL) ' ?- ^1 v2 B0 B6 q; W) G( ~3 }' q: w
& O) J! n# ~/ Y2 C" NSet FileObject=Server.CreateObject("Scripting.FileSystemObject") ! S: I, ?% }8 \( g, ]6 h( {
filename="../index.htm"
- ?& V9 Q6 [! o, B. [9 B; A( jSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 + s- @3 k! U9 Y5 o; x! k
openFile.writeline(sText)
' x1 {8 k# j! m3 F% p" F, wSet OpenFile=nothing
$ m O b4 L& H, ]4 R: e, s( p5 D! Q$ S" n" n- }
%> : d3 ?: m+ G; A% P; W3 P( h
<script>
" m' I T& | U" lalert("静态网页生成完毕"); ; s! H) { H. F1 W' |- p$ I
history.back(); 5 `6 F6 \2 a `. h) l6 q
</script> |
|