返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
% c. W  V7 ~9 l% g$ {6 x4 a6 Gwww.aspid.cn的主站就采用了TSYS生成html文件! - P/ R3 f1 q/ o' z
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
: [% \8 P! Q. I% k/ k6 i$ Q+ c" U
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
3 w$ R# C- K$ n2 t3 O" r# yfilename="test.htm"
8 ]! ]* K2 r; V+ Dif request("body")<>"" then
# r5 [) k" f! b' zset fso = Server.CreateObject("Scripting.FileSystemObject") 0 e4 D* g" Y) Z
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
2 r2 |* B* h$ q6 x9 Hhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" : u- f% e. Y/ y$ |% W7 v5 l
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 9 {: [7 H4 x0 ?5 N6 D' g  h, Z! R
htmlwrite.close
4 g( n% Q- F" v  l8 Lset fout=nothing
( ?7 q1 D  R/ H/ Y% K1 H0 t, @3 Zset fso=nothing ( a# o* h1 t3 K7 S9 F* i8 n
end if
  D, D0 _9 ~' C6 ]2 i%> $ H: V% q/ i0 ?' N. z
<form name="form" method="post" action="">
, [1 V) ?. }' i, m, Q" b, |<input name="title" value="Title" size=26>
5 B7 B' Z  F0 U$ c! E- y, ~0 G<br>
- M6 M8 R9 d7 S+ `8 N<textarea name="body">Body</textarea> ! _: D% Z% N" ^) V
<br>
+ Q' u( x5 m! s' v* S9 @/ q% u, C6 V<br>
4 e2 a$ P& V4 j5 [<input type="submit" name="Submit" value="生成html"> / h& x. }9 u' Y
</form>
- Z- X) I, T( j% w, s1 G2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. * ?( x$ r# x8 f: y) ^
template.htm ' //模板文件 <html> " u0 e8 J, S6 q3 O/ i
<head>
0 j/ o- b9 P% ^& d. y( r# M! @<title>$title$ by aspid.cn</title>
: n- W* g5 @* {</head>
- x" v; O+ n  I5 t<body>
9 p5 Q4 N) _7 c$body$ 8 F) R5 |) b) A2 I4 Q
</body> ' W8 t% Q" n. I4 E  r
</html> ? 3 _) n# Q, s" t8 K, ^( U
' v& V  Q6 L+ B, Z8 ?9 s, O( m8 S
TestTemplate.asp '// 生成Html <%   P+ p6 [& C3 y) n" J( m. U
Dim fso,htmlwrite . Z1 t( P  W+ ]1 n, [0 d
Dim strTitle,strContent,strOut / ?( B% l9 U, ^3 c* u8 S5 H8 ?
'// 创建文件系统对象 . U$ ?. d2 u1 @! ^# s" y& ]6 K
Set fso=Server.CreateObject("Scripting.FileSystemObject") . {, j2 n0 q2 C% c2 o+ z6 x* {
'// 打开网页模板文件,读取模板内容
" Q" `9 f) B1 D6 GSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
% r7 v6 [5 X- \strOut=f.ReadAll $ N6 b  @2 |2 }; b: h& [2 A! Y. d
htmlwrite.close & W2 c6 Y' v5 E7 c: W5 d, H

! e, r  d+ g8 }2 WstrTitle="生成的网页标题" 3 i" A, U3 T3 R$ P
strC
) {- v. @. m- R( W; z6 ]' N8 `: E% ^4 r- r9 {% s
'// 用真实内容替换模板中的标记 6 L2 T( F$ @1 y
strOut=Replace(strOut,"$title$",strTitle) , f7 i5 D, J% r$ c3 h
strOut=Replace(strOut,"$body$",strContent) - \, V& F. F" K* C, {5 l! y# t

3 s9 s7 z  h$ L7 w9 K/ S'// 创建要生成的静态页
0 c  W( `" s# G( D3 S7 Y( m5 WSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) # P5 t" }- J1 Q( b7 I6 C

" B4 s3 @$ o' ~+ x% x& {8 C0 ^'// 写入网页内容
( I- H7 y3 I9 n% Thtmlwrite.WriteLine strOut " Z2 D# V3 Z1 v% g9 u" I
htmlwrite.close
& ^& h& n$ q- n; O3 D" J9 f% X. w, I7 O4 U9 O; I& X9 _
Response.Write "生成静态页成功!"
4 Z+ f* F$ P4 M" S5 j4 [/ a
& d3 m  A$ @  _) l'// 释放文件系统对象 ; ~$ ~' E: `6 v  ^
set htmlwrite=Nothing
4 V( p3 \. g8 Rset fso=Nothing
$ f) e9 t- E) H' O( ]2 ]%>
( a" Y- Z7 |- }# B7 i
/ A. T, g. Q9 Y3 E2 }4 Y3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
3 X6 {; m1 K& q* U<%
# @) ]' L5 q! o
6 O- f8 H4 u. b0 U, x3 G'常用函数
( M% H6 @5 F0 `'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
. T4 g3 F# ?$ x& ^8 q/ Tfunction getHTTPPage(url)
8 J( I3 R* p7 [/ V4 ydim Http 3 |1 v) \, R( d: R3 m% g4 Q/ u4 l
set Http=server.createobject("MSXML2.XMLHTTP")
* n0 Y! r, S7 BHttp.open "GET",url,false
8 z; T2 w  ?8 Z$ P; D8 u& GHttp.send()
! K; M# L. T  I' c" P* L, y1 mif Http.readystate<>4 then * q! i4 q$ c2 N8 M4 I
exit function
% g) i# r) ~1 p, rend if . S+ M/ y( D4 D) R" _
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") . E# D, B9 @5 H) A9 n/ j$ D
set http=nothing
  C) R! j: A# S2 Q6 U8 U- f8 v6 }if err.number<>0 then err.Clear % V9 S6 S/ M5 P
end function
$ P, b) h# m- \" t
9 a$ h6 K% h$ P% t2 Q4 h'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
4 ~3 J: U3 k  v4 w2 Z( ?Function BytesToBstr(body,Cset)
7 h" K' T* i9 j  z; v" L5 |" k6 Tdim objstream
) K" L3 l. _' }, }# B. F" `set objstream = Server.CreateObject("adodb.stream")
# W; G6 @9 f% |% Wobjstream.Type = 1 4 c) R% b9 _7 Z9 h  F
objstream.Mode =3
1 r. ^8 o& d) ^" g2 _3 }5 gobjstream.Open
# D" e$ C, c  O# Nobjstream.Write body
9 U+ h. y& ?1 x5 C& B( iobjstream.Position = 0 $ ]9 K( a  P$ n- Z3 a* j, A
objstream.Type = 2
1 u0 u6 J" I( C/ n/ jobjstream.Charset = Cset ) X/ |' W& x6 w5 I) z8 n2 X6 b' m
BytesToBstr = objstream.ReadText
( [) t2 Z; w: t0 H3 b4 w5 |5 Zobjstream.Close " z) P4 x! I; D% Q
set objstream = nothing ' ~) \, M& E9 h- q3 l
End Function " j, j7 E+ b- K/ P: P* b
, t& x: D/ T6 V
! E- ^( Z! R! C& f: x+ X
txtURL=server.MapPath("../index.asp")
; ^; ^8 d$ y, o' Q# x) Z0 P/ S! l6 H& y8 @
sText = getHTTPPage(txtURL)
8 \; ~# C0 Y- h4 @! ^+ J+ o# T' z6 }$ p# i6 p& H' F# y
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") . I/ z* s* Y) K
filename="../index.htm" - i% J( o5 g/ c7 V5 x
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
, m, s& ^; M5 Z3 l, P) t9 C- hopenFile.writeline(sText)
3 b0 i6 i' L6 x9 j' TSet OpenFile=nothing
& W$ q/ b1 ]. p" s$ B" Q  R1 i2 R  j0 W* H
%> ' L8 o: p, P$ X4 L
<script>
; k# g/ o9 q* Z5 n& Zalert("静态网页生成完毕");
; U+ K# v4 l! R1 M, s' ~history.back();
0 x: N& x. y5 S( E3 h& o8 b& K</script>

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