  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14221
- 金币
- 2401
- 威望
- 1647
- 贡献
- 1349
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
8 o5 }$ h5 @: O像www.aspid.cn的主站就采用了TSYS生成html文件! " K) D: ]' p# j
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 8 R% j3 X" K& v! L$ Y& ?
9 R- W% c0 A+ M' J( u, _2 \ X1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% , p4 F4 ^0 |1 A w e
filename="test.htm"
% ]6 K2 U3 b5 y# P- B0 r! wif request("body")<>"" then
* |) q, ?6 H( a4 Vset fso = Server.CreateObject("Scripting.FileSystemObject")
6 }1 X0 _. ~3 S Vset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 0 C/ S7 G2 `5 `, m2 y8 L, p) r
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
7 Z) m9 l5 g: t( N& Q& e9 {htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" + E. r% b& [+ o, w
htmlwrite.close ; A6 G0 e0 H* X
set fout=nothing ]2 h' m& f6 P0 Y; N
set fso=nothing
' R6 _7 h: ^1 h: E( w/ m. Q+ hend if
9 L/ l" }8 Q' B& w8 I8 W%>
3 W/ `8 M1 t5 A/ c6 |<form name="form" method="post" action=""> 3 A1 N! y! { [' F
<input name="title" value="Title" size=26>
3 v1 u% L7 h; i/ K<br> 0 u2 i9 i& Q8 f9 _
<textarea name="body">Body</textarea>
! h$ I' @' c2 _. H& z! p" q<br> * }! J* i& q. a
<br>
% T4 L5 b" `) y$ U) I$ m<input type="submit" name="Submit" value="生成html"> - R B# \, b6 d0 H
</form>
8 \0 s+ [" N2 Y* I' B2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
z' ]: v/ F2 i; u2 t/ Jtemplate.htm ' //模板文件 <html>
! ?2 c% {9 `0 e1 N<head>
! R( P( k' d; x h4 @# F( l<title>$title$ by aspid.cn</title> 5 C% R8 t: z3 Q8 w" d$ `% i5 B6 U8 `$ b
</head> ; v- J- Q" q: ]
<body> 7 e+ j8 M% n. M; I
$body$
% \" z8 v$ G1 s8 q/ |</body> ; I! n& @0 y) q& w
</html> ? , P& @6 d* i) F4 }
/ j4 G5 n6 c: ]% D- [7 z6 a( T7 S5 h
TestTemplate.asp '// 生成Html <%
" t3 R D5 y# a0 Y: qDim fso,htmlwrite ; R0 {* j4 j; @
Dim strTitle,strContent,strOut
7 `' K' U0 D: ]2 Q8 O' S+ h'// 创建文件系统对象 9 _. | R, E. B' W5 ^
Set fso=Server.CreateObject("Scripting.FileSystemObject") % Z. D' C% ?# f! \+ ^! Y/ R
'// 打开网页模板文件,读取模板内容 3 M5 P8 r1 { b6 M2 ?2 a( p
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 5 I1 x( D6 D* M- ]* ~# B5 [
strOut=f.ReadAll + \8 }+ X) @( N0 D' N
htmlwrite.close ' V- _, b7 j6 c# i! ?; Y% f# O$ [
1 E+ \& P" n* i# c! x
strTitle="生成的网页标题" : l) M: C3 R% f7 F
strC " U3 K& Z9 y3 y- c' x
( I) J3 z; G) j4 G'// 用真实内容替换模板中的标记
6 d1 s' R3 N7 A* F3 X! \9 L ostrOut=Replace(strOut,"$title$",strTitle) ! {1 r( R. e) `" i
strOut=Replace(strOut,"$body$",strContent)
$ { A8 [! ` v
6 Y, y% j+ s# [2 `'// 创建要生成的静态页 % r( O+ L/ s. }; ^
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
" s4 {8 n8 L# J2 u/ i$ `- d8 u ^- r7 I' h" W* {# E( N
'// 写入网页内容 7 P+ {+ _" r0 h9 y( R! j
htmlwrite.WriteLine strOut
j! P- k3 w% L! k# qhtmlwrite.close , s) W/ ^2 v4 Y: h# b$ S3 h* K5 w h
* P, i) n) d$ g Q# a7 [+ Y
Response.Write "生成静态页成功!"
( L3 i0 s! `) C! K5 r- S
1 P) H& O( f* M x( V$ [& Z'// 释放文件系统对象 7 Y8 ?% W# M& P8 F
set htmlwrite=Nothing : A3 D" P/ u. \" c* K, I; a6 B
set fso=Nothing # i1 i1 v' v, R
%> ( T9 R; r+ Y. A( z# m: ?3 b _
9 B; u& X0 Z6 L9 P
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
o$ ]4 E$ _& N( S; n' }% x' W# F9 c/ V<% 8 @, D. s6 K( q0 W/ O8 x
8 n0 t+ \* X. z8 B
'常用函数
- X& t" W& U2 L+ p'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ) U, t: O' P F2 |2 @& {1 B& n* k, x
function getHTTPPage(url) 7 ]& \! z- `/ Q; z& W5 p4 r
dim Http
: a/ \. C- J( | m# `set Http=server.createobject("MSXML2.XMLHTTP")
7 v" r# ]! b& K5 `3 o2 z* ?4 A OHttp.open "GET",url,false
4 S3 B% G7 D( AHttp.send()
( E9 l0 A* M. ^- r: M% pif Http.readystate<>4 then
$ A+ Y% {7 d( \# Aexit function
6 M* j% r3 ~' [end if + V# u1 ^ K; W5 R: v: z: d
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 2 \ A* t: j! C! s4 _8 S
set http=nothing ; n/ E: l$ m* g; y
if err.number<>0 then err.Clear ; w7 F3 |4 {- @9 C% v1 K3 [
end function
1 g4 V: E3 c2 C" W, e: s/ f, H: E/ E( G$ Y9 v& z
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
" }- V0 N1 Q$ u' W4 ]7 ^1 b3 t- GFunction BytesToBstr(body,Cset)
" g( \; t! j9 u5 U' t$ s. vdim objstream 9 u) @' d: q1 i% H+ _/ O. x7 G7 E. e
set objstream = Server.CreateObject("adodb.stream")
' c2 Q. U, x" M. j# c: i; ~, qobjstream.Type = 1 $ O; S1 I3 o) p; d0 t: K
objstream.Mode =3 ; a8 K+ p, p8 z0 D& o6 P/ _0 q
objstream.Open
6 X* F- R4 O5 A/ Wobjstream.Write body 9 X/ v" J9 b2 A3 a
objstream.Position = 0
6 k- z, j( Y9 k) k5 j. z$ U1 u0 gobjstream.Type = 2 ! d+ S. ^" H- P+ V* h0 u4 M
objstream.Charset = Cset
# }" _+ L+ t+ FBytesToBstr = objstream.ReadText
- m6 O; `8 s) p) W2 E+ ~( Y) _9 m( Uobjstream.Close 2 g* X' E+ s0 `5 Q' [. M
set objstream = nothing
, i4 ? Z$ Y3 L0 ^; x+ [! S: GEnd Function
+ Y+ y0 {8 D W$ f& @3 G! D
: l0 S' d6 }) [: l2 ^! [8 f
/ Z2 W/ H% E0 d9 G1 M4 ?txtURL=server.MapPath("../index.asp") & Z4 l' c8 Y) L3 `. f7 m, F
9 ^4 ? E# e) `0 y: H, l: {4 k5 U
sText = getHTTPPage(txtURL)
8 G2 G, v8 z+ J; x3 \! a
6 t. N, P: E4 t9 ^% gSet FileObject=Server.CreateObject("Scripting.FileSystemObject") ! ? e+ W& Y% W. G& E9 n" C
filename="../index.htm"
# J V+ }7 F! MSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
; P. f$ Y( x, g$ Z9 H ?# e) @7 x( NopenFile.writeline(sText) ) c, w$ y! O; a" O/ x9 O5 q& }
Set OpenFile=nothing
" h0 |9 p6 L. f8 [: {- f% ^3 {9 Q `
%>
8 R- f( I9 m9 T# y* Y<script> Z: s8 ]5 S( ^! M% `
alert("静态网页生成完毕"); 7 Q& u9 K0 V0 f8 T2 R
history.back();
, h6 I* H6 u& f) `) l</script> |
|