返回列表 发帖

ASP生成静态Html文件技术汇总

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
! v0 Z; Z9 \+ W% I6 s, i1 ^6 ?www.aspid.cn的主站就采用了TSYS生成html文件! , w! v* j6 S; Q0 H8 c
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 0 F/ U9 U1 m4 q7 t9 v
' M* D: [7 J* c' x
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
1 `! J% I, ]8 S, i  J/ J0 Jfilename="test.htm" 7 K0 Z2 o! J+ U% X  f
if request("body")<>"" then $ A3 R% c! H/ j7 I6 C
set fso = Server.CreateObject("Scripting.FileSystemObject")   G7 {; ~6 Z! G* c. T
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
3 L5 O4 a9 [& a  l: mhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 4 i$ R+ k( A( K2 r+ J! b' Y
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
9 i/ V0 `$ S" Chtmlwrite.close
1 H! u+ A' k; iset fout=nothing
) c2 f; _( g# I" U9 tset fso=nothing ( x/ `/ t5 C* {: w7 v5 H
end if ' P) L5 g, r( f* e% ?7 ~! X
%>
) v2 _+ [+ k+ P0 i0 B<form name="form" method="post" action="">
  e/ h# ], x- U% m- P<input name="title" value="Title" size=26>
, V* y6 F5 v- n- e( H* ^<br> ; g) O6 Y: A" W% T/ p# G
<textarea name="body">Body</textarea> $ J  g$ Z0 F- L% l+ E  N6 J. h
<br> ( F8 W$ Y# g( ^% W7 O
<br>
0 q: {4 T% s) u, @- L6 V<input type="submit" name="Submit" value="生成html">   s/ p8 P$ A( Y% R6 k+ W
</form>
5 K: w2 W6 M9 H3 {2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
0 B; C" g3 v' S& p/ l( ptemplate.htm ' //模板文件 <html> 0 l* H9 D. V- Q6 n! [: \
<head> * }: e4 [& U' }/ H
<title>$title$ by aspid.cn</title> ( c% R. w3 m* j5 N0 Y) U- L) f
</head>
+ e. z, \; \0 x2 |0 T1 ~8 e% F. v<body>
7 n2 |4 q0 M' p5 L* d9 b$body$
9 D% i1 @" r9 q9 k</body> 2 m: s$ y- x$ z
</html> ? / h7 F: a8 j# o* ~8 k+ S
4 \7 @1 y/ M1 _$ |9 q) O
TestTemplate.asp '// 生成Html <%
5 y8 m, p9 u5 Z3 \- j/ u' wDim fso,htmlwrite . ]. S, w2 N6 V5 X" ?; ]0 }
Dim strTitle,strContent,strOut
5 [  L9 y2 x$ o'// 创建文件系统对象 $ N0 S/ h% F9 f- @: x
Set fso=Server.CreateObject("Scripting.FileSystemObject") % G- ^& f9 j3 k& }: F
'// 打开网页模板文件,读取模板内容 1 i1 V# [4 k* g  _7 [0 q) g5 u
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
$ Y8 s2 B* E: j% {# v# gstrOut=f.ReadAll ( x0 J( c# i& T
htmlwrite.close 4 y& u. y. h: R- d- E6 |8 q
$ Y* b$ B) _0 \
strTitle="生成的网页标题"   D- A8 {# |5 ]- G/ a' R( x
strC
" B( J/ g# |3 L' A
& ^% v, \' ?5 p/ q- g) Z1 O'// 用真实内容替换模板中的标记 . Z9 n# C$ K7 B* W5 h- `& [0 ?' p
strOut=Replace(strOut,"$title$",strTitle)
- O( u9 K$ I) cstrOut=Replace(strOut,"$body$",strContent)
; c( R( F5 P, [+ L. @# ?5 j
1 y0 X1 ~' f; F. P/ i'// 创建要生成的静态页   X8 D# M  x# I7 ~8 K
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 2 G% b+ A; _  x( W/ ~" g
" Y. D" R1 w. ~! K9 r
'// 写入网页内容 ' k, k$ `1 n* G* V. l
htmlwrite.WriteLine strOut   H) j% W! U/ Y( F* \/ N$ i
htmlwrite.close # U0 G" f9 w% _" b2 n
: Q% p  M* c4 I, _. w' V& ~
Response.Write "生成静态页成功!"
1 S, H; p" m- [; u6 L- B4 L
1 T/ M* ^! s+ {$ A" X  e( R( I! B'// 释放文件系统对象
) y) ?0 d" ?7 y2 b) F" Jset htmlwrite=Nothing
6 Z/ ~8 U" b: h3 }: Bset fso=Nothing
9 M0 r1 _7 O' c0 K7 Z%>
- ?' y: t/ ~6 X2 d1 B8 \  J6 m8 u6 s. C. X1 t8 |
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ( p4 d) x* b3 ?/ E% i# Y
<%
1 B( S4 a; q5 n+ C( ^3 t2 A$ O7 S5 v' F$ _; |
'常用函数
: c. o! _! N! Q- @% C'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
( }$ L0 j; u7 ~" x( a5 M/ X- jfunction getHTTPPage(url) 2 }7 N4 b0 A! M# ~( N2 K( ^
dim Http + a7 \3 S0 I/ N2 _- R4 j
set Http=server.createobject("MSXML2.XMLHTTP")
1 F  o0 H0 o. k# _" V5 S  |Http.open "GET",url,false   _' s$ z  J4 {8 J7 j, b
Http.send() ' b7 h' v9 l/ X
if Http.readystate<>4 then
8 F3 h7 \8 n; v7 J4 Gexit function 9 [+ N; g8 \$ n6 n
end if % ^2 t, r4 M" ]9 I2 T
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
; t9 j- v- E& d+ Y/ `4 G; D7 H8 L/ ^set http=nothing
! ~0 A6 p0 h$ R) ^if err.number<>0 then err.Clear # A, E7 u9 T  o/ E! S# Q
end function
6 Y- A' O2 }* S
6 Z3 H; l% l- {; x# A# @. i'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
; d0 s; {) {3 r9 nFunction BytesToBstr(body,Cset) " V  w. T- z! ?: W
dim objstream ! E" H3 D  c1 |( ~* A0 {# ^8 Q$ X
set objstream = Server.CreateObject("adodb.stream")
1 W# r# |* \$ I* C6 }0 Uobjstream.Type = 1
9 p& b. @; b$ B8 [objstream.Mode =3
% B2 V) p( D5 |0 i2 _/ B! Zobjstream.Open , G$ ]3 ~$ w( Z# w* H" W/ |
objstream.Write body
3 P2 j( G1 B% x- w( t9 cobjstream.Position = 0
/ m$ b0 c8 G7 [" V" r' D" uobjstream.Type = 2
5 b7 h6 U+ i- B8 n- Uobjstream.Charset = Cset
8 @7 R! r# J! `. U1 p& qBytesToBstr = objstream.ReadText & |9 J( b$ Z1 c: X' A, F
objstream.Close
/ w& |- z( _' L! c* lset objstream = nothing
8 k+ @* r1 C5 ]" Y# W7 lEnd Function 1 D0 E) h) U3 }& K( Z( W
1 ~+ P$ j* ^' q; ~& R. V& K) Q

) G$ \  H) A* i! ?  x+ atxtURL=server.MapPath("../index.asp")   I( b6 [$ S) v' D

+ G+ B* O9 Q( e* LsText = getHTTPPage(txtURL) % Z6 G4 g7 v1 T  c) P9 l0 c; l* X
9 F1 m) |# l$ d6 I, n
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
& X1 x! l. Z, ^) _5 W  W9 c0 q( Jfilename="../index.htm" # N9 r% J/ b8 R# U
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 , O+ s2 B1 L3 d' X1 l; e
openFile.writeline(sText)
4 l* a0 S' G3 n9 n8 R9 A6 |; lSet OpenFile=nothing # B2 g7 x8 M/ }$ k2 C

% q5 _0 W0 t) a/ w% X%>
. l, I9 }$ T0 q3 U<script>
; y1 t% |/ ?7 q; F; B4 k( Jalert("静态网页生成完毕"); 1 R" h7 ~9 M9 C# m
history.back(); 5 }1 M) O- _% o7 U0 D" V3 @& q
</script>

返回列表
【捌玖网络】已经运行: