|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14341
- 金币
- 2456
- 威望
- 1647
- 贡献
- 1404
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
' |3 Y: i7 g& \9 t像www.aspid.cn的主站就采用了TSYS生成html文件! ' W7 O' X8 s4 J6 H0 x) d8 Q
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 2 s* x3 v$ [0 U* ^+ Y2 d( N
; c( @$ O2 e4 X7 Y1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 5 ?# J- X9 f- g
filename="test.htm"
) H" ~8 Q( b' i! G& V( Bif request("body")<>"" then
" ? t& |; C, u; f( g% g, Yset fso = Server.CreateObject("Scripting.FileSystemObject") 6 W! Q1 {* V! p* Y9 g
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 6 o- e+ W, F. x* z" ^! D7 V
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
) a4 z' m+ M1 i- o2 a/ \htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" - ?5 x; F* d) \% {3 A
htmlwrite.close
$ D( I: m' P+ b- Wset fout=nothing " n) N. Z! k% j0 X/ P6 h, i- d: d d$ q
set fso=nothing
" R+ C6 L6 ]) H( f" M9 f7 \end if $ G# d* j5 [, L& C6 V
%>
' n& m6 ?* k9 r! Q+ j5 y" c) I<form name="form" method="post" action=""> - e5 Y- |, a5 t8 w
<input name="title" value="Title" size=26> 7 A, T1 F6 T2 X5 H4 Y
<br> + h/ a0 k9 H$ j8 Q9 d
<textarea name="body">Body</textarea>
* _: ?4 m# o- {9 R<br>
. C2 ]* ]0 V( U& X- C/ i4 @<br>
) h& T6 y! _5 N; t% E, {<input type="submit" name="Submit" value="生成html"> 6 j& i# H: A' C* w- @; S' s
</form> ) t/ A- V1 I; V7 f ?8 s
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 3 C7 |: a" D; F4 v3 m
template.htm ' //模板文件 <html> & S7 d8 O9 y3 m4 b4 s+ _& a
<head> 9 Z, M2 E0 i" k: H9 y" Z7 d
<title>$title$ by aspid.cn</title>
* u8 l: z5 W4 w @</head> 4 ^3 c2 j; {+ C! a+ j7 t/ r* |! c
<body> . Y" e9 J7 Z$ N5 u$ {
$body$
$ u2 d# S# s9 V0 z, u</body>
0 ?: m! b$ N- C% r& N/ t</html> ?
5 S H# K0 I* O1 u9 w2 K/ c
: v5 e L" n* z/ [TestTemplate.asp '// 生成Html <%
. U, v' c* d1 b/ x. Y9 CDim fso,htmlwrite ' s# H$ I1 o1 B, G$ B: k. b1 H+ O
Dim strTitle,strContent,strOut % U2 B5 e6 o% y5 V' L
'// 创建文件系统对象 5 L! F n5 H4 `
Set fso=Server.CreateObject("Scripting.FileSystemObject")
( A3 D9 c( s# F2 z! f'// 打开网页模板文件,读取模板内容 8 Y6 T l g- L
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
2 [5 a4 ~9 J# M1 f0 Y( ostrOut=f.ReadAll
2 \$ g5 _, Y2 ?& i4 `8 Q {" zhtmlwrite.close
W) ]" Z* K/ v5 d. L4 M, V
# ^4 `6 W# ?: UstrTitle="生成的网页标题" 6 X& G7 O7 L; v$ A) k4 q
strC 3 R* ?* F i& Y9 u0 ]2 G1 B& f% I
$ g3 h8 L: X6 s: g
'// 用真实内容替换模板中的标记 ! t& g7 y5 _( N8 I' \: B6 X
strOut=Replace(strOut,"$title$",strTitle) ; A9 p! s. W S+ x
strOut=Replace(strOut,"$body$",strContent) ; F' Z; J" a( {! U! i
) w4 Q: p2 L0 F( ^$ K- m& d. ?
'// 创建要生成的静态页 4 r7 B: M% Y$ A% [- H
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) $ Y' r% x: `1 F
0 g6 U; X( _: a+ m( N8 {
'// 写入网页内容 ) F2 Z4 { t% Z4 I) H
htmlwrite.WriteLine strOut $ l+ }4 V- L( l0 W
htmlwrite.close
2 {) h$ Z. k" R8 q% S' g! W! f. Q+ k Y; g/ h( w
Response.Write "生成静态页成功!"
; s( x( Y& K- q! p3 B) S: b9 ]$ O1 C; A) L
'// 释放文件系统对象 & h9 |$ }! ~$ }- [# V/ i
set htmlwrite=Nothing 9 T, f2 x- a% J; }1 |* Z
set fso=Nothing
5 C9 M2 U* ]* _( K! W" ]%>
, n* `* y; h, y- }0 F/ s
! v! w7 z, c7 s8 i4 S* X3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ( }- u; o& f; _
<%
1 d, }; R5 O, v l7 O0 s$ _# h" y4 K% L7 x% I8 U9 y9 N- z4 m; d* c7 x4 }
'常用函数
5 k6 a9 g) F' q% ~- n) A8 X'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
" r- c/ E$ V5 p5 [function getHTTPPage(url) " a! h3 ~8 R6 z* \3 U
dim Http % a2 w$ C, f# j
set Http=server.createobject("MSXML2.XMLHTTP")
& L; U; v( x# O* iHttp.open "GET",url,false & n8 z) y6 D( ]6 q3 e4 a
Http.send() # I$ U2 t3 P$ _. p0 V7 U# `. S
if Http.readystate<>4 then 6 A5 q& X. g. b8 A! \
exit function
% j$ h. h* }! W. Pend if 0 P: g" a5 R/ T/ Q$ `
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
7 T a' y3 w f5 i( W$ oset http=nothing ( I' c/ N$ U# o" K8 ~$ V0 Z$ n
if err.number<>0 then err.Clear 1 P! G( p9 o. r; R+ B" P' }6 M
end function ; N: B9 \5 I5 C( `! ~) T( J2 Q
" ~' j3 _( u% e7 T8 t'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
2 g6 C' F5 r1 B8 q3 VFunction BytesToBstr(body,Cset)
5 v! W- i4 h9 x" K# h1 }dim objstream , n8 ]; | F i5 l; \) J
set objstream = Server.CreateObject("adodb.stream")
; ?& I% E9 X8 p8 Vobjstream.Type = 1 " W7 @; |4 l# g5 s
objstream.Mode =3 ) ]+ l% P! f; p% ~, {
objstream.Open 1 {4 O' k, @& |+ }- o% P% c
objstream.Write body
+ B+ d& | s. ~) c: Z7 g. hobjstream.Position = 0
; m, Z& O+ @! o0 S5 E" oobjstream.Type = 2
0 d2 p- N: X& ?. R( I# u* Iobjstream.Charset = Cset
9 M' c* O8 S7 o. s/ F) sBytesToBstr = objstream.ReadText
6 q% L+ u. m" v Mobjstream.Close 8 a1 `) H4 {3 ]( F4 b5 u# C% d3 ^# ^
set objstream = nothing
; E9 C3 R! d/ P/ n: ~' Q, YEnd Function
4 n% O: k2 k9 ?) T/ Z3 L3 ?, v6 y
" i }) b' E ?" y
: Q# j2 l6 U V( ptxtURL=server.MapPath("../index.asp") 8 v. W- i M) _9 ~* c( [! I
$ I8 c' i2 X# s# S0 H3 `& ?
sText = getHTTPPage(txtURL) $ l5 h6 K1 N4 G% ]: _4 Z
7 R. L0 c+ N6 U4 Y3 O4 CSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
2 x. U# K4 Z! G. Rfilename="../index.htm"
) t6 j; y2 {* BSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
" W5 [; s6 p! _9 ^& \openFile.writeline(sText) 3 r7 E1 v* x4 |7 c
Set OpenFile=nothing
- ]! Y' J: P0 x" \+ G1 F4 n) M/ {2 y6 S$ o& E- R. M' j
%> & Z+ S8 `8 J, l7 N9 K- J& p$ l, A
<script> % R4 W5 O& Z; ?0 @2 B
alert("静态网页生成完毕"); , \$ ?+ B% K8 p& i2 Z
history.back();
3 n7 y7 h7 k; K$ n! R2 g# v</script> |
|