|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14443
- 金币
- 2507
- 威望
- 1647
- 贡献
- 1455
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. / [$ e) s* f. x: L9 N
像www.aspid.cn的主站就采用了TSYS生成html文件!
9 _ h. c' W: f4 z; P( ~: I2 M所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
6 U8 K! F S) e; i
# |: {# Z3 `1 O1 u# r9 R0 L* O1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% + @8 r+ z/ i9 ]# u$ e0 m
filename="test.htm" 5 _' }/ Q& s) r/ J: X _) y
if request("body")<>"" then
) L4 I3 S7 G6 t6 rset fso = Server.CreateObject("Scripting.FileSystemObject") ) z8 L; `. c3 p/ Y0 Z
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
& }) @8 s8 E' Dhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
7 r6 P ?; r/ J# c; Y9 Vhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ' T0 L) E. |! s$ I$ O# h
htmlwrite.close
" X$ A9 o# c, n1 Aset fout=nothing
) e" k! S* J: gset fso=nothing
& A) g6 d% F1 u& q# f- vend if $ S4 h+ c: ^; R$ _8 L2 Y0 b
%>
" V" V. I; F9 K4 V$ W) f- O" Z$ H<form name="form" method="post" action="">
3 P- Z$ z8 ~& C, g<input name="title" value="Title" size=26>
) o( n6 I6 e4 ]: r<br> 3 Z8 S5 D6 x- i9 \- ^* j+ [; X" U, i
<textarea name="body">Body</textarea>
, c: N7 \ G8 Z9 i9 \<br>
7 t+ B4 Z# v1 _' O( d2 }0 b0 ?% c<br> * [' ^& Q/ m; R$ Y- S+ n
<input type="submit" name="Submit" value="生成html">
$ D V# E% z6 x</form> . ~! y8 m5 Z( A% ~% l4 h4 K7 n5 i
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
) }% X7 y: O( l. d2 }0 xtemplate.htm ' //模板文件 <html> , l5 u* M/ a( U: L. l4 l
<head> " A) d7 ?: S# P
<title>$title$ by aspid.cn</title>
: i+ s; T0 u6 O$ }; K" T( I% J</head>
, H, R H* r4 b5 J<body> 2 t3 ~5 {8 r1 m' \. t0 w0 I
$body$ * p8 ~: Q. V1 G5 {% J* J6 i5 f' c( E
</body> ! P. |$ x1 t, S" d8 r" ~0 q) s
</html> ?
) a, G7 U1 O t9 m6 T7 G+ P6 U6 z0 k9 `& n1 s
TestTemplate.asp '// 生成Html <%
2 o: C4 L/ H. c- N# u4 o+ Y8 _Dim fso,htmlwrite
/ {5 K/ O# n4 x t7 ] V* y1 zDim strTitle,strContent,strOut e' d* c1 j+ |
'// 创建文件系统对象
6 T5 q! W5 ~; W d: I: vSet fso=Server.CreateObject("Scripting.FileSystemObject") 2 r7 |8 @; _7 B3 N/ w7 y+ S
'// 打开网页模板文件,读取模板内容 6 z; @1 ~$ T' A3 }
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
# A1 O% D& j. c' Q6 {. c. ?strOut=f.ReadAll + H) I8 H9 u, V" k% k. _
htmlwrite.close 5 e* s% J3 e! n+ F4 V
6 _2 Z# V1 V) h, K. u) o
strTitle="生成的网页标题" 5 ^# S( n+ g! Y) z. S! x
strC 3 | [# L. c5 |* [
3 l g5 Z' g) o6 |) \
'// 用真实内容替换模板中的标记 \* m! T3 R6 g4 U, s; J
strOut=Replace(strOut,"$title$",strTitle)
% C! Z. {$ @* }9 ?7 BstrOut=Replace(strOut,"$body$",strContent) 4 C/ n+ g' ~2 A
. P2 O1 r2 t9 p% [2 T
'// 创建要生成的静态页
2 Z; [: F8 q5 Y% N0 pSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) - v: I6 P6 q! u5 L4 [/ O A) B3 M: U
4 w5 Y! }' b1 \, t6 j( r. U$ l6 R'// 写入网页内容 4 Y. L- E* Z- R
htmlwrite.WriteLine strOut
6 ^# [3 Z8 m: R. Ghtmlwrite.close 7 w0 X. U+ _3 O. r# m: z6 y8 U Z
. R0 X" X) b! e7 [
Response.Write "生成静态页成功!" ! d/ N, h- r+ u# i- n9 d
' L6 J* L) D2 y, s
'// 释放文件系统对象
8 q/ k$ j {9 ]1 L# xset htmlwrite=Nothing
1 D6 p4 j. w. I, H, v! p- G: t1 ]set fso=Nothing
: J4 S* t; s) |2 B6 I) S# k* l%> & j1 O3 W1 j8 r/ k
6 d) v+ U& B; s
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. , `" N" O8 o W9 s
<% ! y x. J# j( {. H( _! K
& ?' N2 @7 |# M; X0 D) Y& H# r'常用函数
2 O9 d$ h1 b8 J0 n) X. K6 l'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 2 r M$ ~) v1 E) N" z$ e# k
function getHTTPPage(url) ) w% d0 X5 S4 i
dim Http
$ J% h3 f8 l3 Q2 eset Http=server.createobject("MSXML2.XMLHTTP")
, A' v: f6 f1 R B+ M' t- PHttp.open "GET",url,false
0 d# t$ A8 E8 j1 V# AHttp.send()
" O/ |, |; [7 f% y- Qif Http.readystate<>4 then
$ b, ~5 c- i, P2 U2 Bexit function 7 \ m; r- R' j; b, ?% p' X
end if
- g. x$ q5 a; S9 |/ r ogetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") . L" b, h4 \5 V( F. g: F$ k
set http=nothing # ]5 p1 I H4 x: A$ M1 g" c
if err.number<>0 then err.Clear & k9 n% D, g# J
end function
( F/ a3 W' [6 ~, J/ y2 h1 b9 `4 `( [) n7 Z. t
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
, c: M f+ y9 `( x( }" sFunction BytesToBstr(body,Cset)
+ [ U6 x s5 a1 Ddim objstream
8 `9 ` F1 Q! ?' S/ eset objstream = Server.CreateObject("adodb.stream")
" s% U) g$ O+ u- d: oobjstream.Type = 1
2 X( n7 E- o/ {% m8 W: _' xobjstream.Mode =3 3 @, @- Z) j* `. W9 F$ c7 E8 h
objstream.Open 6 x. o+ E+ Y Q. q# P# m9 Q/ W+ ^ K
objstream.Write body * K5 t9 @# i0 f* ^$ u
objstream.Position = 0 6 c! E6 ?, ^8 T. e k
objstream.Type = 2
" |5 Z7 p! C# G2 N j8 J* Uobjstream.Charset = Cset
/ E( `4 v2 U: O* r v- K7 \& XBytesToBstr = objstream.ReadText
2 C5 j$ j7 g7 }objstream.Close 2 U0 q2 z; \. @6 X% D& f
set objstream = nothing
I" h& v7 t. u. Z( MEnd Function
0 |& ~7 V$ B9 E* r/ x- n ^, u6 d2 \: y: |
* a: r& B# j" A+ h# k5 |! g( @! ?txtURL=server.MapPath("../index.asp")
4 @6 J5 Y# W2 N- |+ Z0 M z
6 b4 p, {3 ]4 N0 e P9 @# d. ?sText = getHTTPPage(txtURL)
6 p3 _% j/ N4 w1 O' g: N0 I1 I$ o7 n! [( K$ _
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") $ j" |5 k7 y- e& y
filename="../index.htm"
. O! D6 F; p+ y. V' Y6 X; DSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 9 f, v) a6 {( }6 u7 q* |; x
openFile.writeline(sText) + F2 `7 v5 G( h
Set OpenFile=nothing - U! L& m2 v, k4 p7 r: ^7 O! H
0 }8 d P X% q- n D%> 0 h7 j& a u0 A8 K0 K% j; r
<script> 9 z- ^& N6 b4 x4 z. ?( h2 g
alert("静态网页生成完毕");
4 k) n' G7 M9 M. t" }history.back(); - E. c- N* A8 `; A# I
</script> |
|