|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14387
- 金币
- 2479
- 威望
- 1647
- 贡献
- 1427
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
P& J$ k' k) y+ N像www.aspid.cn的主站就采用了TSYS生成html文件! % y) m1 o4 O ?, E
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. $ Z- B1 W3 N/ ?
1 G/ L7 f+ q9 F$ W
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
2 F; ?* l9 ^* {3 _% m' }filename="test.htm" 5 z) H" m9 P9 g5 a% n
if request("body")<>"" then
/ @; W; W; e2 l: u% T; Xset fso = Server.CreateObject("Scripting.FileSystemObject")
" E* [4 x4 ?# F4 [ N$ Uset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
: U& _' ?, s4 A' d+ ehtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 0 t+ R, M1 w7 y$ n! u5 l
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
3 F5 F6 ?% n9 n: R# X2 shtmlwrite.close
R+ [7 u$ a3 y1 |" M5 }5 {set fout=nothing
7 v' R5 ^% `6 q7 _, G {set fso=nothing - |5 Q- g3 @, M7 {, M! h
end if 5 c8 u6 z# E$ l$ Z- K( }1 {' _$ A, f
%> 9 I: r) V7 w6 d) A4 y: Y. n
<form name="form" method="post" action="">
( q6 o" F6 _9 ^' |0 K* s<input name="title" value="Title" size=26> & y& B& k# r/ y$ @
<br> 6 w/ [4 `& a* x7 A+ X, u% W
<textarea name="body">Body</textarea> ; |$ x/ E& l8 ^8 h' c8 l) w7 ~
<br> + Z$ u5 l( B. G, H0 W! I
<br>
2 q% r. @3 O* ^<input type="submit" name="Submit" value="生成html">
/ l" k" a# g1 n, s1 Z3 g" N</form> % E: J6 { P* K+ p9 N: y
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
: J) U9 i1 F) T( qtemplate.htm ' //模板文件 <html> ; O! |$ J0 a6 t7 J; Y! S3 o
<head>
% g1 p0 H2 D, w5 b9 d, j1 p A9 N<title>$title$ by aspid.cn</title>
( ~; t- L6 f$ ?2 B1 O/ H1 T</head>
0 q, D" z: H2 |* X- f<body>
. j6 p8 r' x( F r4 a S$body$
6 B q: Y7 ^! |8 `) n: ]1 U# G1 R</body>
* c$ J1 `' X+ ^7 X7 T8 X2 x</html> ? 7 r0 O. w8 h5 Q5 C$ \5 J6 h1 T! v
/ N$ h; h0 }9 R2 _3 X3 S3 u% v
TestTemplate.asp '// 生成Html <% ! [3 { m% t# ^4 v0 c5 H
Dim fso,htmlwrite
, D. F% |7 I* B2 W& ]* v( hDim strTitle,strContent,strOut . P( l) F$ i5 J) m' b8 D: K- i
'// 创建文件系统对象 8 V0 \4 k( L: B, u4 E) `
Set fso=Server.CreateObject("Scripting.FileSystemObject") " _, }% q" @ o+ ?" o4 |
'// 打开网页模板文件,读取模板内容
: g1 C$ b4 C3 ?: B1 I5 ASet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) , B; } K. ?( Z. w+ r7 W& x0 r
strOut=f.ReadAll % z8 g4 I3 Z$ b7 Z
htmlwrite.close
" a$ ^( A9 B- S( e3 F& |8 P, W5 V B4 V+ ?, z b
strTitle="生成的网页标题" # }6 O/ E% k, U9 {
strC # p7 R Z) O5 L- F9 ?) ?2 r( k
/ N- z( \1 [) _, D
'// 用真实内容替换模板中的标记
- f8 V9 }# F& _+ `3 e0 d! sstrOut=Replace(strOut,"$title$",strTitle)
3 q s+ c3 o) i" R, N) W& `% pstrOut=Replace(strOut,"$body$",strContent)
+ ?/ Y5 \* @/ J
& e6 `6 w0 a9 e' x; V ~4 N6 j'// 创建要生成的静态页 * g2 |8 ^8 \, |% p! r
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) . o1 n Z- v8 L4 N- w p
- Z6 z; p# D1 N6 k% u
'// 写入网页内容 ; [& o, b' w" h6 p7 q% V
htmlwrite.WriteLine strOut
; z1 W1 h$ U" z2 n! {; w) G) fhtmlwrite.close 7 e+ z5 G# s- G! ~* Y; ?
7 h5 c) {" S4 \Response.Write "生成静态页成功!"
( g3 c* n* k( p( v! [4 n0 i9 A
( X# Q1 ~) R/ o# B'// 释放文件系统对象
! n* |2 U+ ?' h6 tset htmlwrite=Nothing ) s9 i r, t- u* }! ~* o
set fso=Nothing
$ n" ?1 \3 f) K( v* L& Z%> ) {3 e9 ]. q s E' V0 x* B
4 Y7 j# P3 ?' b1 B. r
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
~$ A. c" K4 ^" H% n/ W<% 3 T1 t: w7 t% i! m& j) k$ l
- U- e! c6 P7 A2 e; i: U& @! C
'常用函数 9 u# F$ F* e" j& V0 P$ a' \
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
+ H* V+ Z8 z8 ?8 [& M" D; v+ b2 K, Ffunction getHTTPPage(url) ' z' Y+ E1 S4 w: J8 z9 X# R
dim Http ( _( y: b! B$ Z" {
set Http=server.createobject("MSXML2.XMLHTTP") : d& p. m! i3 g6 A$ C9 q. x
Http.open "GET",url,false
4 v2 X2 Z! {3 x) Z; c3 {Http.send() 8 O* M2 {' Q8 t
if Http.readystate<>4 then ! u4 l! Y& v4 I* K
exit function 4 `/ ]8 t6 R5 J( b: B% B, {" D3 U S
end if
- R0 G* ~& R" O5 |getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
, {- ?% K _/ W$ \set http=nothing ' s; K) }3 A. [
if err.number<>0 then err.Clear
% S1 v0 G* V4 ?" T. z- r8 Jend function ) {# Q! [0 z' B7 o: A
6 c) a' x: ~5 A$ K1 y t
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 5 k8 D* K" i+ ]4 {/ \! J' C
Function BytesToBstr(body,Cset) ! P! x! d& M+ b( B0 Y6 |( _: n' \
dim objstream 8 i# V l* p5 J4 c7 a4 w7 N
set objstream = Server.CreateObject("adodb.stream") # Y7 m, f3 r' L5 r- w: {4 S" @
objstream.Type = 1
) {+ }4 P: r; r- d) aobjstream.Mode =3
- x5 U4 Z. B% Robjstream.Open
( _# u$ y! {/ h* W9 c6 m: y& W+ Bobjstream.Write body 2 Y B' ^. G1 @- ^" M4 d0 m& V: s
objstream.Position = 0
7 y& E1 ?- q2 Q4 r, I% V. f6 Uobjstream.Type = 2
/ X* |& m+ {$ [objstream.Charset = Cset & C! Z2 j! O, u/ F3 q' j- C$ w( p
BytesToBstr = objstream.ReadText ; j) O7 O3 u% {
objstream.Close
w! C$ [6 ^& l2 Aset objstream = nothing
: q9 x3 P5 E1 [% g. t( F7 kEnd Function
3 d0 F! F6 c) C
& P7 M+ W: L( k, Z8 x* {/ e' A0 Q/ t. b5 o# m3 o
txtURL=server.MapPath("../index.asp")
! z; G7 R( d3 F9 g) c x6 Q
7 V, U: g# g, l* X5 osText = getHTTPPage(txtURL) , }" G* U l) z3 G
! z8 L% w! b7 B" G# y {# _3 m# D
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 8 x. p1 N; F5 }3 e' H
filename="../index.htm" + H1 ~0 B2 o& W& n* Y+ k
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 & c9 k4 x5 u2 y9 J
openFile.writeline(sText) 7 N: U& c5 }# M+ K; L9 C- ^7 L
Set OpenFile=nothing
u3 }% d }/ i/ j$ G
# a" e$ T8 A" b* e4 E& E%>
4 S( c, m9 P$ ^3 i1 z% _4 g! b<script> 8 X( C. x8 g9 O
alert("静态网页生成完毕");
( H! c* D/ ^2 C' P. F# d4 }history.back(); & `6 F1 Y$ ?* n* V+ ~
</script> |
|