|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14443
- 金币
- 2507
- 威望
- 1647
- 贡献
- 1455
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. " O/ ^8 j ], e' C% k* z# u$ i
像www.aspid.cn的主站就采用了TSYS生成html文件!
: h5 Q- {' B8 O/ D1 p0 x所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
. z1 g' Y) q% [* A) ], B! F) I( o: f
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% - f8 p/ T9 `* U3 X1 ~! @/ [5 d
filename="test.htm" 4 j: c% d5 s6 C' j2 i- C
if request("body")<>"" then
5 B0 F5 l9 \) e. Q- I- a* Vset fso = Server.CreateObject("Scripting.FileSystemObject")
# G0 L# A+ S4 [set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) & w7 \ O A% J& O* ?& ?
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" d# y- t5 }7 j `/ V8 a! @ d
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
% d( d5 R, f. Z* @htmlwrite.close g* R) T- R4 c, X' a
set fout=nothing
/ T3 ` s* h- _$ Z k9 J' Y! Vset fso=nothing
/ V3 U! a! j' D& J! m$ b( _$ Yend if
' a% A. Z3 f* N* p7 E%>
" C3 |. V0 c+ N% h- N5 d/ y; w<form name="form" method="post" action="">
; @0 T/ n0 t- b/ E7 A6 @<input name="title" value="Title" size=26>
h5 T; @7 k% O4 ^/ e<br> 3 b3 A' G/ T, h% D
<textarea name="body">Body</textarea>
' V7 P8 Y" \ l3 A2 m$ p' N5 ]<br>
/ x0 H! x6 i. c' t5 E' \, G% Z<br>
3 Y: \2 s3 `6 E, a3 A9 t<input type="submit" name="Submit" value="生成html">
: u1 q' N2 ]2 C1 `' @</form> 3 w, L7 _) Z! v# Y5 u
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
: `7 V9 p1 S) Rtemplate.htm ' //模板文件 <html> 3 V, l& ^ |8 E7 c( s: n1 w
<head> 3 M9 ~, r- o" ?1 D0 ^4 h! L; h( n% S
<title>$title$ by aspid.cn</title>
a' v: U/ C/ m- J# z</head>
# Y0 E8 [3 @/ s4 K7 I W3 j. N$ {<body>
7 h! z/ ?0 }, l: V' k; b$body$
! {! F8 O- s6 H) V- a" h$ g4 z. W0 p ^</body>
8 J+ C9 G9 z8 j% |& }, `</html> ? + z7 p$ d" c' k d) S
7 G7 A+ U5 ]' jTestTemplate.asp '// 生成Html <%
8 v7 S6 |0 g* ^& p4 e& T) LDim fso,htmlwrite ! q# U0 w5 ]' j
Dim strTitle,strContent,strOut " J8 k/ d: O8 a- {: b1 q E
'// 创建文件系统对象 2 d' P9 o$ P. w% m
Set fso=Server.CreateObject("Scripting.FileSystemObject")
5 C0 N2 L! M, Q! R! N9 ]1 m'// 打开网页模板文件,读取模板内容 + i) l; u: F* t! B
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
+ t- ^* p+ ~7 o1 rstrOut=f.ReadAll 5 N5 E# |0 }9 M6 y
htmlwrite.close
6 a+ R: C1 q: C3 I2 h
$ r! D( ~7 b( ]) tstrTitle="生成的网页标题" 4 H7 `6 J$ h8 f% B
strC 0 ?6 n& e# Q: w% X% _& j
n/ p* v5 b- @$ @, t
'// 用真实内容替换模板中的标记 * z8 P" h! [3 u8 y) V5 C
strOut=Replace(strOut,"$title$",strTitle)
9 m, t9 w7 z2 q! E' R5 O( VstrOut=Replace(strOut,"$body$",strContent)
' b+ X( I2 }7 }# W
' X$ H! r& A' _- ^3 w'// 创建要生成的静态页
* U( w1 P' n- o2 T iSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
' W: b& \. v, N" s6 v2 y/ s) o, P x C- l8 }
'// 写入网页内容 : |- h* o& K( Z8 R
htmlwrite.WriteLine strOut
( Y6 h3 z1 a1 Z$ N" r" xhtmlwrite.close ' v4 t) h4 w, r) w$ _: ]# l& V
* p. c% }. N5 AResponse.Write "生成静态页成功!" " b( L: m- `# z6 J# Z! S
. ~7 t8 K$ A+ j3 }- A9 y'// 释放文件系统对象 ( y4 s5 D! ?& J" W2 _. T' l
set htmlwrite=Nothing 8 |4 y- w" s# z4 T5 Z
set fso=Nothing
: Z& f# K2 {7 R) E6 m%>
' ^$ e: y! M: f; N+ v5 B" e- |' P+ b/ h/ O3 |2 c) ]
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
, Y; L6 B" ]' y: X" Z9 d |: v<% ( |" N; ^; N2 q7 E% R% C6 r
4 d8 c( e( E+ l4 G2 i, n; }
'常用函数 ! e/ |; ~7 u1 K8 R6 q# i
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
6 ]+ s; C7 B/ z, v9 `1 P3 h7 M7 ^function getHTTPPage(url)
: r8 j- S' M0 N- W5 kdim Http : L8 a/ I$ G' A6 Q- W
set Http=server.createobject("MSXML2.XMLHTTP") # Y. g: `. ]" A D% ^, v
Http.open "GET",url,false & [1 k. O% p: w# N; [0 F4 p
Http.send() 7 M3 M4 n3 f! W* k+ N
if Http.readystate<>4 then
2 J2 O" t9 j7 p3 `& H% o& mexit function
/ E8 c( g3 K2 N/ V% b2 [9 K- ~end if " T. S5 ?8 q* _+ a2 m Z& i( q" O
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 9 w" M c F0 p# f W& T
set http=nothing - J( P8 X1 B% P
if err.number<>0 then err.Clear 5 i+ o$ }6 {3 d/ [, q
end function
$ O& Q$ v* y/ E* F' c; X* Q$ b
. R8 O6 u6 R0 P. x6 e/ ~'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 / E/ a: u% Q5 b2 a
Function BytesToBstr(body,Cset)
$ z4 `2 e. n# ~9 C- N% h' Adim objstream
3 u6 U' e' D" C& u0 oset objstream = Server.CreateObject("adodb.stream")
3 P- E8 ]* v6 [% K5 ?) Q; r6 iobjstream.Type = 1 + o* e: E- ]) b; f" W
objstream.Mode =3
8 M# ^7 B y+ R3 {( i" oobjstream.Open
5 B7 E) i8 v+ w P! Kobjstream.Write body
9 p$ |+ C( A# C$ Gobjstream.Position = 0 : W- m4 X7 G4 X, C3 E2 G) ~( G9 g" K
objstream.Type = 2 . z Z' x9 X% |9 b- `/ c! T
objstream.Charset = Cset
7 K- x1 }% ~+ H' X' }2 RBytesToBstr = objstream.ReadText + w7 g M* n; W: w
objstream.Close 9 A W! e% y W8 c( \3 k! \" j* G
set objstream = nothing
6 \4 U5 b( N7 Z9 `End Function 5 [& }: D: A" t1 }
4 n* E: P* t% q: F' k5 ]3 v
8 N+ `1 R1 |0 @6 N8 g% A: utxtURL=server.MapPath("../index.asp")
1 ]2 w5 C' ?; O q' B8 J
9 G/ B& |3 Q; k2 e7 WsText = getHTTPPage(txtURL) 8 i h$ M' t; k4 c
, c" \; r4 O N. O. K4 [ e; ~
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") : y9 u7 X$ _7 V* T* z4 R
filename="../index.htm" ) x7 v' t1 g" R4 S8 m; W
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 6 q. `2 }) I, t2 |. [, O3 V
openFile.writeline(sText) 9 P7 Q' S6 Q1 x9 ^, ]
Set OpenFile=nothing
. i& j7 E- ]- N9 { `0 h$ H
+ F7 W) s4 h' ^( q1 x%>
1 x' y! H8 |) j# P! e* ~<script>
9 p4 c* J* I2 L: D. ?& K5 E, salert("静态网页生成完毕");
, z3 j, Q! S- K4 {1 G5 T1 d$ Khistory.back(); 4 d* t! W" B+ Q# h/ a% _6 S' R5 q
</script> |
|