|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14365
- 金币
- 2468
- 威望
- 1647
- 贡献
- 1416
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
1 ~6 j' e; w% _+ c$ |8 Z像www.aspid.cn的主站就采用了TSYS生成html文件! % M6 j3 l( y' Q
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 5 D2 |( s9 @; n: ^
% b) O8 G1 a+ ~7 ^0 a1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
9 B; ?2 g# A1 A; G* @0 o' r: g9 Wfilename="test.htm"
" f6 P: W# y( U) S) v0 qif request("body")<>"" then
0 o: F3 p5 _& l: Z# K% vset fso = Server.CreateObject("Scripting.FileSystemObject")
& W, B/ m0 f! t* Z: ~5 Sset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
4 f. C0 X7 d+ G8 t$ P8 t; Ohtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
- x# t5 B/ S3 S) M0 ghtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 8 Q/ {. K: H' |( P
htmlwrite.close ! i$ N9 }1 u. X/ p! w
set fout=nothing
, ~$ ?( L3 W$ @4 ~8 o5 P: G$ C, Zset fso=nothing
& o; [' J$ E+ t1 N& k% n7 O% T6 f& ]6 jend if
% Z/ @8 Y5 m4 |# |* B5 L3 y%> 2 D, Z7 x& C1 D6 O
<form name="form" method="post" action=""> $ q( t2 B& N- O
<input name="title" value="Title" size=26>
: \* U: \! ^) n2 Z$ c<br>
7 L# W- s! m8 ^, b" W/ L" |, Q<textarea name="body">Body</textarea>
7 p/ i/ j i, L; D<br>
) q- q( s$ Y# G3 ]! f0 ^<br> ; x" q! x8 J" S2 F: z6 j2 ^; c
<input type="submit" name="Submit" value="生成html"> # U% U6 }( K% S6 c& c
</form>
- `, O* Z6 N' X# y2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. / d- P5 p& ~% ?; ~. {! a
template.htm ' //模板文件 <html>
/ c7 x8 F4 q: H$ y* f<head>
+ c5 }! U* a% M4 x<title>$title$ by aspid.cn</title> + l4 O& |: q0 O1 W! o9 ]& _# v
</head>
" ^7 `* I/ K( g* b- H1 L! g. f3 X" V<body>
: P+ x" d1 w* e7 O# v! G, c; K1 j. X$body$ " r$ o& S% c% i
</body>
+ P& n' w9 M5 _6 |& E' I</html> ?
% N/ Q" d8 g4 ?! j/ L/ H: E0 z1 l$ j- V; I
TestTemplate.asp '// 生成Html <%
- o ?2 n" Z, ?4 K6 X, n/ ^! K2 pDim fso,htmlwrite 9 @4 H" ?) e' S# ~
Dim strTitle,strContent,strOut % J3 i, V3 p* w
'// 创建文件系统对象
5 P+ T m# E1 U+ u JSet fso=Server.CreateObject("Scripting.FileSystemObject")
. ?) T0 A+ {7 I2 h'// 打开网页模板文件,读取模板内容
$ ?0 r& z# {# Y, `' mSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
/ U" r+ s; V+ |2 c# \; e3 ]/ r hstrOut=f.ReadAll " s- ]2 G5 \& \5 L
htmlwrite.close ' o U, {7 I0 B' s3 J
7 ]1 t; {" s+ _9 C! r+ G# HstrTitle="生成的网页标题" " n& X6 a1 b- Q" G+ P9 G2 X1 C
strC 5 M& F) k9 d5 r; e3 z! g( W
) m. d0 Q/ o( E5 p: y) ]
'// 用真实内容替换模板中的标记 6 o2 X& T5 Z+ C& i$ X; _
strOut=Replace(strOut,"$title$",strTitle)
{# l5 m1 w& s5 j( I$ TstrOut=Replace(strOut,"$body$",strContent)
9 S9 c0 p% T: T/ J2 Y( x8 i
4 a$ O$ n* T* d6 i6 Z'// 创建要生成的静态页
1 [- n: ^) \4 ^7 L7 G5 {Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
6 z! _% ^# q3 L! v
. Q, \, Z, H( U& l$ j9 f'// 写入网页内容 3 ^9 a0 t" k* V' ^. x# z
htmlwrite.WriteLine strOut
9 O9 @" g( E/ G4 m& Jhtmlwrite.close 1 b) S% j; b3 z' ]
4 O/ _* V3 w# ^Response.Write "生成静态页成功!" 3 x* i$ n2 W/ e% u
9 O l% B- E4 ['// 释放文件系统对象
0 }5 W1 j9 h X, R' H; c7 wset htmlwrite=Nothing 7 c' a3 r. S' n
set fso=Nothing
0 G* `; l: q( F: M! E%>
: Q* y3 V+ y! e( L6 l( O) K# @
! b( ~. q* s9 o9 w" X- _8 }& U3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. + v- A2 Q' b. ?5 q
<%
* m y$ U1 W! ?5 N2 v7 x. q5 F5 ]( {, O) k
'常用函数
/ i; b" _; @/ S$ ]( r$ C1 @'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
0 I9 O Z# R0 v, T# n/ }6 zfunction getHTTPPage(url)
6 P2 c& S5 ?" u- bdim Http
1 v7 c& R7 o( N7 R' p: rset Http=server.createobject("MSXML2.XMLHTTP") " h6 R5 y r+ t: l( B n7 X
Http.open "GET",url,false
7 f _. d2 E7 |! ~% LHttp.send()
& V9 C9 l4 P% ^" U! Vif Http.readystate<>4 then ! @" m" B4 u0 v* F3 a
exit function `4 B9 b6 R, a! W( q: C. c# Q
end if 8 S: H) Z2 |9 d7 F, S0 i7 _
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
( Z' H# `# Y) H- ]6 Lset http=nothing 6 T7 r$ j2 X; K. b
if err.number<>0 then err.Clear
7 U& d+ ^7 F! Y, t- Vend function
+ ^6 R/ C. l$ n, X' Q7 a$ Q5 S q; z1 Y
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
* {4 U- h& s& ]/ VFunction BytesToBstr(body,Cset) ' \0 Y( m# Q1 L5 d8 V5 k3 T" B
dim objstream / ~* L! q! E( ^; N$ d9 A
set objstream = Server.CreateObject("adodb.stream") ( i( s+ o% b: {+ R. t8 U5 j7 r
objstream.Type = 1
' B& n7 L, r% E' j1 S ~objstream.Mode =3 0 d) v" w7 y1 U9 m* ^. r7 u
objstream.Open
( ^& e3 I8 P. i$ L/ robjstream.Write body 8 O- D, n9 a+ [% }- C7 W" E9 u- q
objstream.Position = 0
% J2 O+ }4 C& c) Dobjstream.Type = 2 , V0 I" C1 X! g/ l( G6 w( A4 }
objstream.Charset = Cset ; X+ _& x( \1 x3 e
BytesToBstr = objstream.ReadText
# K6 r; \' ]& D! Wobjstream.Close
' A: Z2 h: H1 W7 Wset objstream = nothing
. e$ x. I7 ~1 y# w) N4 v) vEnd Function
1 @; a* Z: U4 T# }& |. ]7 J, ]. u: {/ N
* ]' N9 h8 T9 _0 q. g, D+ q
txtURL=server.MapPath("../index.asp")
. g+ ^ J/ Y0 k6 B z$ d
h1 i" n* H, PsText = getHTTPPage(txtURL)
' `% a# ~3 s% r+ ^* {& K2 A: |% Q0 E) I
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
+ o6 f L/ ?4 e9 Ffilename="../index.htm"
6 x4 n6 \ v; {# Z rSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 3 B5 |( R+ D$ R3 c, \
openFile.writeline(sText)
5 g( k3 @! a1 E3 P9 \: i" Y8 f9 `$ XSet OpenFile=nothing
5 z2 s0 v/ n' j6 V2 u+ ?6 p: N- b) w
%> " G1 T( @+ L2 _' c# `6 g
<script>
+ O3 ~8 t# k' W. Q, _alert("静态网页生成完毕"); 2 D+ H! R* m' O4 _
history.back();
9 M. @9 N& [+ D a</script> |
|