|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14397
- 金币
- 2484
- 威望
- 1647
- 贡献
- 1432
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
) o8 L% u8 T7 W \) m+ X4 T: _像www.aspid.cn的主站就采用了TSYS生成html文件!
2 _! t2 a: X4 C8 u所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. - _$ n2 d# Y0 [. I
$ G! c% R' c3 U0 b2 a1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 7 u. T: [# T( d4 H6 h( i( `, X
filename="test.htm" 9 B" p; H: P& F8 B
if request("body")<>"" then 2 H1 h6 k1 u/ f5 d) o/ u
set fso = Server.CreateObject("Scripting.FileSystemObject") 9 m6 a" h+ n# g$ j6 m, S
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) * P% p9 F( V& }7 O6 j% c% U
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ( T8 c8 x. H0 X3 v) @- v6 J1 ^
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 0 m" V' \; T( t% F
htmlwrite.close - t4 c M. e# u. @0 P
set fout=nothing 1 B% u% `$ Q5 j5 c; c p% B
set fso=nothing
1 s; ~/ f, i% d9 R) D+ xend if 8 d% c* t/ p! _ R' M* u# C1 z
%> r3 \3 U& V+ j. a+ M9 |
<form name="form" method="post" action="">
+ d& M7 [8 f1 I# P+ M<input name="title" value="Title" size=26>
: C Q t$ `& t# W- {! P<br>
( S( }" U; ]9 |, ^3 D4 V* r6 I<textarea name="body">Body</textarea> : _& @" X! l% a
<br> 6 q2 F3 F! `6 e5 b# L* v% \5 k
<br>
- l% e; O* e% ~, _+ V( p<input type="submit" name="Submit" value="生成html"> 3 C# ^3 j# o X: |) w) v1 y
</form>
9 ^, Z8 Z1 [" c$ z/ n! T3 A2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
3 G2 C8 ?: f& E/ ?& ^, B3 Etemplate.htm ' //模板文件 <html> # `7 t1 {% p$ k& |/ Q( j
<head> 1 o& p; s3 U& L4 Q$ I) b" M
<title>$title$ by aspid.cn</title> ; a3 d( ]6 F1 K' B% r [
</head> 3 \9 X" U: x- x, l; Y% ?, r* d
<body>
. D- U/ ~' N. N6 i8 v$body$ 6 v" h0 t- s) H& o1 g6 O+ C5 d2 I" ]
</body>
4 ?! F4 m% J4 ~1 H1 A7 M+ u</html> ?
8 J* i. A; j6 s* v& {; r
, A/ U- \( [+ ^; E% @" gTestTemplate.asp '// 生成Html <%
2 D! f! Q, G: j+ g, ^Dim fso,htmlwrite : p5 |) @7 A* a
Dim strTitle,strContent,strOut 9 _; O6 Z% F5 z" x0 g( K& x
'// 创建文件系统对象
Q5 [* B# x8 J! c6 t7 BSet fso=Server.CreateObject("Scripting.FileSystemObject") , D' T' b& B: Q) p. q; f8 F: \
'// 打开网页模板文件,读取模板内容 , t9 C: c7 h( i% e `+ @, W
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) ) V' S2 R' }/ [
strOut=f.ReadAll ' V6 B7 c' z* T6 k8 n8 |- n. V& L
htmlwrite.close
1 N* r2 O2 m: {% o& a5 D8 x% \4 r: m( h; T$ O, s
strTitle="生成的网页标题"
" I) h$ l: v) J3 T0 j' xstrC # v% [) Y: u4 g; D
9 @" `1 i- u A4 |8 B'// 用真实内容替换模板中的标记
" g p$ I. Y) @4 ostrOut=Replace(strOut,"$title$",strTitle) * U! H# R4 x$ {( w" S
strOut=Replace(strOut,"$body$",strContent)
1 w4 N9 {* p8 `
7 D# M* _1 c5 T7 U) \'// 创建要生成的静态页
! F( R1 p' q: \$ _. b. q4 A$ P( D) NSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
2 O1 z& v! E# C8 i) h
0 R; ~$ A7 d6 f'// 写入网页内容
I M# Q/ m, Y5 Z. ihtmlwrite.WriteLine strOut ; c& Z5 C+ I2 {2 {3 V& J
htmlwrite.close
! l' e# n+ w9 d I. b- B! h3 o6 u/ A0 Z5 q0 [
Response.Write "生成静态页成功!"
& Q8 k! D, l) I, A) j) V
5 c$ ~* o0 o' w+ @: e! N'// 释放文件系统对象
, v# c( F8 d0 Z, gset htmlwrite=Nothing
* N4 L$ J: e+ r+ ~+ d& @1 kset fso=Nothing 6 ^( A0 @2 a& R8 J6 P* G; ^) O* c
%> ; e G; x4 G' A4 W# ~5 K4 I7 S
. l! @3 S7 R* }' t F4 l
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
+ b( W4 t- C+ O9 P) |3 i7 d<% 5 _8 t) S" O' s9 @0 I# Q
8 w5 @ @6 W; I _3 C& q3 G" T0 B
'常用函数
$ ]$ E1 {/ h) h2 |( z, b'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 8 ]) T/ b5 u4 c. l9 `/ E& @" G* |
function getHTTPPage(url)
3 C- ]" R$ k) ~/ Ndim Http & y5 D# ?! A4 E+ `( a
set Http=server.createobject("MSXML2.XMLHTTP")
1 r$ v+ z Z# D) MHttp.open "GET",url,false 2 q" z! P) D0 ?" x3 L: V
Http.send() ; {& z( x* X; e" l
if Http.readystate<>4 then
8 n1 K; y4 Z x9 ~- bexit function
0 W" D. }! g9 f1 hend if * X0 k- d O8 b
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
# ?1 ]/ \" g, O; E' a6 n" Wset http=nothing
- `, C/ }/ X5 Wif err.number<>0 then err.Clear
( j$ T- i9 C+ r5 @5 hend function ; _8 b: N4 l1 t; s# i8 v
" {, [0 ]& W- f. ^* r) \
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 6 ?$ i# u% S3 S8 t. d* ?4 e
Function BytesToBstr(body,Cset)
9 d- k5 k6 _, T. _" \dim objstream
. q4 n" `, _$ d' [' Y! }set objstream = Server.CreateObject("adodb.stream")
. m5 N: J, C) Y! G5 `9 i! xobjstream.Type = 1 1 I H& ~8 Q; P) I/ Z7 i
objstream.Mode =3
+ T; `- _/ J* A0 @$ dobjstream.Open ( L$ t4 t: C; J# E1 O" {
objstream.Write body
/ W2 N; ~, R0 F$ bobjstream.Position = 0 4 V# w% K \! z- t6 Y3 v% q( W
objstream.Type = 2 ! |, h4 `# ~! X
objstream.Charset = Cset 5 _2 L! {# @6 P0 \
BytesToBstr = objstream.ReadText
( d! B' x A: Hobjstream.Close
5 _" O% {0 T. a) Aset objstream = nothing
/ p7 x+ k$ i' Q" o" i8 ~/ |, bEnd Function + T! ?4 {, \2 D! }* l, Y0 Q
" N6 Z- f6 C" ^, a! H$ E& w) ~2 O* k" g9 i2 B, Z+ o
txtURL=server.MapPath("../index.asp") - p" }/ L4 O( P0 c, J9 j- O
. L F8 e/ {* E% ~- h' B% CsText = getHTTPPage(txtURL)
! h0 s3 a6 |7 ]2 E" t1 Q1 X b/ s8 [ E+ }: F; D8 b2 s
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") - I( \% ^- q) t
filename="../index.htm" ! p. w; a# c# k+ M o" ]1 H
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
- U! T* N( l+ IopenFile.writeline(sText) + X5 }- _. q5 O9 p% h" h: U
Set OpenFile=nothing
3 |& H: q' A7 d$ ?2 C" V) @! \, ^& Q. d9 g! N4 J! C
%> / a, }+ `' e, p! ^3 \+ H4 F
<script> 8 T. I+ X! B e h
alert("静态网页生成完毕");
$ x) ?: L1 |+ G8 ~+ M" ~9 k- ehistory.back(); $ r# G) ^7 x" X; w+ ] a+ Q. H
</script> |
|