|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14433
- 金币
- 2502
- 威望
- 1647
- 贡献
- 1450
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
7 Y1 z9 W% E: ~像www.aspid.cn的主站就采用了TSYS生成html文件!
! {$ i) J, G: h+ z/ h6 J所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. & r6 C, b( `6 z- Z- v( i8 S! X+ S
5 y. C4 J* F' h( a3 t1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
5 M1 D; J0 f. ^1 O/ A6 ofilename="test.htm"
; z) U! J0 e+ x A& B" S$ t9 P5 pif request("body")<>"" then {# W I, U" B0 T6 O
set fso = Server.CreateObject("Scripting.FileSystemObject")
( j* }- R& W5 n9 F1 O, k$ Oset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
- [3 B" D9 B' G! A+ |htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" $ w |2 a; p, A
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
/ Z# ^& }4 e* X7 ^/ l! d5 ~* zhtmlwrite.close 9 W- T- J. _; S" o
set fout=nothing
6 j' g6 g3 J# T$ t9 Fset fso=nothing
: M2 ?) B d3 @) send if ( [3 L9 d& |8 n# r; g* ]
%> 0 L* Q5 _8 x" ? q/ f
<form name="form" method="post" action=""> . i y5 {/ o* v G
<input name="title" value="Title" size=26> 3 E( O8 A: D% N
<br>
) x6 s; \( O) \4 D4 A& f2 I+ s<textarea name="body">Body</textarea> 8 {4 m. m# y: I! A
<br> 0 x r/ P. F0 m* k4 U0 |. s
<br> . y$ k* Z7 [2 t: c/ f5 j" u+ ^) D) O
<input type="submit" name="Submit" value="生成html">
$ X' A5 v5 q, |/ I; e* |# W3 b</form>
: V8 {6 @# c- W. S s, s) M2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
' p7 N3 g6 I; g' u1 s/ itemplate.htm ' //模板文件 <html> ; P" J* c9 W& g. I5 c- V9 u+ q) G
<head> 8 i1 R1 a) y; I6 S0 ]3 t( Y) R* p
<title>$title$ by aspid.cn</title>
- b2 @! X' \7 g5 Q</head>
6 _3 C: c6 I* I" V" F<body> - k- C& a' L4 S. W( q6 `
$body$ $ T0 ?: A. {* n0 W
</body>
- ~( B* _7 A3 O2 U% l1 Q+ b</html> ? u0 @; O+ ]5 l9 I$ L5 ]
9 e5 ?: G0 R Y2 OTestTemplate.asp '// 生成Html <% + Z8 N! \: H1 F3 `
Dim fso,htmlwrite 1 ^; V' F) v, z* _# q. Q
Dim strTitle,strContent,strOut 7 t) O4 f/ M6 S
'// 创建文件系统对象
3 i: B; \1 T/ E0 d1 FSet fso=Server.CreateObject("Scripting.FileSystemObject") + k3 P) y% c% ]" S8 h$ |% n9 G
'// 打开网页模板文件,读取模板内容 $ T. q& Y# ?9 x
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
! t3 z0 g+ E( g& bstrOut=f.ReadAll
2 t( V4 _" ^, C$ ~& }; s- qhtmlwrite.close
+ a9 d% N# B, W9 {1 x9 u& A# H3 Z$ E+ L- ]
strTitle="生成的网页标题"
6 J7 x1 j3 a0 h5 i; BstrC 3 N) i. r2 {0 v: W0 l
$ K9 ~) D9 P" z9 O# W, H'// 用真实内容替换模板中的标记 , i6 [2 ^8 x- |: U
strOut=Replace(strOut,"$title$",strTitle) & m9 s0 b3 F2 O1 Q: B
strOut=Replace(strOut,"$body$",strContent)
; \" k* P7 G) M& D- F4 d$ t* ?% `8 H8 z* U8 i7 z, ?: M
'// 创建要生成的静态页
! |% S! L V! T; Y+ A3 tSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
. F5 N* x1 q) j4 Y, \
, u; D# i& K3 k6 r0 `4 Q'// 写入网页内容 , ~" g& |8 { X8 V
htmlwrite.WriteLine strOut
$ f6 ]4 U, c8 M4 I$ r, Ohtmlwrite.close * x9 L$ Q, L3 T+ F- w' w- _4 i
' P/ P, q0 K q: _1 _7 c5 m
Response.Write "生成静态页成功!"
& g+ H) }/ M- S; _* U- a
$ _' @4 k; _3 S'// 释放文件系统对象 . i, k7 }" V* l6 V. y
set htmlwrite=Nothing 6 q! F5 c' E) g& U% X' I
set fso=Nothing
2 W" |8 B1 x/ W3 i%> 8 { A+ [2 E9 l4 U) H# ^- a9 \- G
. A+ G3 s8 m# G7 s8 Y
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
" a$ R9 n0 a4 l* w) ]7 ]<% . Y9 [" R. Q0 h: ^ C3 {; P
) t- H2 a0 A) b+ V
'常用函数 ; W+ u0 @1 R( U9 O% B
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 9 G* Q: M9 r0 Z* y
function getHTTPPage(url)
6 e! n: O. l2 Zdim Http 0 F0 }/ M( Q5 a2 C
set Http=server.createobject("MSXML2.XMLHTTP")
- F7 c4 J* ~: L6 l% kHttp.open "GET",url,false 0 O% z0 C1 |* e T' b* m
Http.send()
8 m% s6 `( y$ `6 W7 O+ e! Eif Http.readystate<>4 then
- T8 w' G$ ^0 g/ l% B2 T! wexit function
8 t2 D8 y0 V0 U; \end if 9 O" H) I- R- i1 o, B+ @, [0 Q
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
, @) ~: w" j a5 Z, J% _8 Iset http=nothing 2 k. i2 b J7 r/ K, G
if err.number<>0 then err.Clear ( u+ ^( a8 u8 c- d5 h
end function
$ s. a, [6 a5 b6 I8 q" u9 @
7 ]8 s* l' Y1 m/ `+ s* i# K'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
3 f' @. @+ u% c3 j* tFunction BytesToBstr(body,Cset)
' T. O" T# _) pdim objstream
- c6 x$ K% J0 |! ?3 @set objstream = Server.CreateObject("adodb.stream")
7 Y5 \ P! ?5 S5 y" o! o2 D/ Robjstream.Type = 1 $ s; J" u& [% q s9 K& }
objstream.Mode =3
i7 V! a8 T$ D4 M$ H* ]objstream.Open
# u: A& V4 w4 ], G E: B& _/ d6 q4 Fobjstream.Write body
9 m- x! O0 G7 Z. m) Hobjstream.Position = 0
# |2 L5 m& i2 V( Z( Jobjstream.Type = 2
$ G9 o: [7 I5 |& ?/ g& H- ^: Z3 Z8 Sobjstream.Charset = Cset , a! ~ T4 b* J; y @$ U. N, x5 c* o+ n0 Z
BytesToBstr = objstream.ReadText % n) o0 Q* f. q% {$ W
objstream.Close 1 v" ^5 @0 o# r" l5 h
set objstream = nothing & ^& }1 x' P5 r: O0 {
End Function
0 d/ x P2 g# O/ F
" ^3 a- j5 U' J2 r% J' u% ~ o
) f, |0 A8 Y. w5 ~- ^# f* g: btxtURL=server.MapPath("../index.asp")
( `% K0 v u: m$ m4 D; z- G" W, `. {& e3 w: {6 g
sText = getHTTPPage(txtURL)
1 q3 |$ p& V: U- J( X h
* _# R5 d y, b) }# \2 ?% RSet FileObject=Server.CreateObject("Scripting.FileSystemObject") # C# h' B4 A! Z; [4 y% e- i
filename="../index.htm"
1 ]$ C3 i& [6 y- i* h* q2 ^$ I, eSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 0 E1 D' O) O% \+ z$ q1 y
openFile.writeline(sText)
; x$ }- n( G! KSet OpenFile=nothing 0 Z; L" r# O4 ~ }: L) E9 G) ?& o8 ?
3 v- {% _5 o" M( Q ]
%> 6 T( H5 z$ G, W3 z+ V
<script> , S. h$ d$ d [' K
alert("静态网页生成完毕");
+ F4 P2 q- G2 i% Thistory.back(); 7 u3 h- f0 N& J$ y8 R2 n" a: T. ~
</script> |
|