标题:
ASP生成静态Html文件技术汇总
[打印本页]
作者:
admin
时间:
2008-1-19 00:10
标题:
ASP生成静态Html文件技术汇总
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
. i8 |2 H. h: n3 f( k9 j
像
www.aspid.cn
的主站就采用了TSYS生成html文件!
/ @) T5 O4 e7 r9 g& @. o
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
/ Q) v8 ^4 W; n# |
; s) ]8 x! _, B: n* K6 s: `5 q$ w
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
" h( J% Z8 r5 j+ f) z9 A3 V
filename="test.htm"
& Y, H9 h' ~0 J* w: o
if request("body")<>"" then
* k5 n. o/ i5 R- j: z1 k9 |. G
set fso = Server.CreateObject("Scripting.FileSystemObject")
- o/ I1 M% b. p0 @6 |" |, u2 _" q* H
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
[2 r2 [/ z7 e2 F" l4 M$ C9 r. U
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
9 \" C; e1 i* a: F( y
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
3 t/ V; S2 d& G* X+ ]
htmlwrite.close
* N8 C0 V( b; p2 {1 k, O
set fout=nothing
1 k* @4 [, U3 _( V3 x6 R2 W- |
set fso=nothing
, s5 z4 T. b4 @
end if
6 w* K: D g @9 d* \: |, X& y
%>
: ~; O I3 Z" [7 u8 f
<form name="form" method="post" action="">
2 Y/ F/ g8 C2 V$ w' B% k+ Z
<input name="title" value="Title" size=26>
: \0 L4 f9 S, R/ s! x
<br>
" J- e0 x/ A3 Z: O1 c& l# H
<textarea name="body">Body</textarea>
* u" J7 F, k0 Q9 j }5 ^
<br>
9 `- C+ W- k8 ^3 X8 r& m0 h
<br>
: [# G( S& u# l7 P6 h
<input type="submit" name="Submit" value="生成html">
: [! I N. e- T% S) y1 e2 X
</form>
6 X% c' s+ h; k+ }
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
1 X9 W0 p$ U+ u% A( V3 r) c
template.htm ' //模板文件 <html>
/ s" } A \* I* f
<head>
( _4 _% M, [2 l! p. D( s0 `* h
<title>$title$ by aspid.cn</title>
4 [. v7 w2 h. o+ Q
</head>
) O4 j% x1 V) V! I' u! k( ]
<body>
* B% `$ v3 L. X
$body$
" `/ E# j& Q6 k5 }
</body>
4 F3 I F4 S- {2 B p8 N
</html> ?
4 k% w* V- T+ T% X1 a: X" w2 d) Q
1 u' e) k# E, l
TestTemplate.asp '// 生成Html <%
9 F6 g6 r: H# Q* ^1 b. S4 ?0 P: h
Dim fso,htmlwrite
7 M2 {1 b! {4 x$ L* N1 q% Q
Dim strTitle,strContent,strOut
( L5 m. @5 K" D g- k8 n
'// 创建文件系统对象
9 r6 w+ b/ A+ M2 a( F
Set fso=Server.CreateObject("Scripting.FileSystemObject")
% y, V. J# s' u1 t
'// 打开网页模板文件,读取模板内容
; `/ { z1 X% U+ s
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
, h- l- H. o1 I6 @% B
strOut=f.ReadAll
: \! S4 X8 J7 ]7 F
htmlwrite.close
: ^7 F) u2 ~/ K. |! z
k z) M5 G; }8 Y
strTitle="生成的网页标题"
3 B5 R, Q$ u( q# H0 s
strC
) X$ \" t. A2 z/ X0 v4 P, w! i
% M0 @0 a( ] x; V9 |2 W8 L# S
'// 用真实内容替换模板中的标记
( v& I8 y2 @; |7 |
strOut=Replace(strOut,"$title$",strTitle)
! \: @5 B9 d7 o4 i( P
strOut=Replace(strOut,"$body$",strContent)
6 }0 y& p @4 p3 t" \) D
& u/ v( ~& }/ G k. X; }5 v
'// 创建要生成的静态页
4 C z+ l8 G9 U$ l! K" t+ y0 M
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
! y. J2 s H, P
, R) q& @5 s/ k7 D3 f3 Z7 V
'// 写入网页内容
: O8 ]( V2 b4 q/ D& c
htmlwrite.WriteLine strOut
: d. c; o4 A2 i9 N# q3 ~% _
htmlwrite.close
/ s1 V% Q! K3 q( u0 e! k
) y5 J8 Q3 Z4 b
Response.Write "生成静态页成功!"
( @, J. `' j0 g8 L C. M3 ?* K9 ^
8 [/ i `* c4 m1 p3 q
'// 释放文件系统对象
" R D+ P! g1 {; R3 G8 F( c$ n2 F x
set htmlwrite=Nothing
6 }) ~9 U: n- _
set fso=Nothing
3 ]2 |0 B p% S. m" }# n7 r/ F
%>
+ k) @. [% J, W8 }
: P( v, {! I5 }* \4 t( I$ N5 J
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
( ^0 N0 r) p7 T4 \
<%
: J: j4 f( f% W; u, y. I
' }% v% C c0 N# i, f5 T
'常用函数
' t9 F) x8 F) O0 ?! L2 a! M% k `7 _
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
) x0 O! O% b" \. C% R$ ]0 _$ n
function getHTTPPage(url)
- h2 q4 Z) f) Y- w7 g4 A
dim Http
$ v' p( B8 h, V/ o
set Http=server.createobject("MSXML2.XMLHTTP")
# u! n) K; m. \$ X+ O
Http.open "GET",url,false
0 C) G' o4 ~3 L+ J
Http.send()
7 _! b" e$ ^$ c) |
if Http.readystate<>4 then
7 t, g, |2 D ~ \! V1 r2 d
exit function
& l- b# B8 D- K) |% M/ ]
end if
6 I( G& U3 n* T% [& `0 p+ W' Z: i
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
! H( Z1 a& }6 K, v$ J/ r
set http=nothing
1 G- h8 x8 x' |- |& H4 I6 E
if err.number<>0 then err.Clear
0 |+ s. D4 F3 q# U6 `4 `$ P) h
end function
/ I/ W f6 ?- ]2 }) p* U9 Z
& O% a; ^. }3 P
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
; T; r1 }6 b: u5 P& [: Q
Function BytesToBstr(body,Cset)
7 h4 X. y5 i K* I
dim objstream
/ A3 I" r; x+ }
set objstream = Server.CreateObject("adodb.stream")
% |. z2 h5 d1 I Q' ?
objstream.Type = 1
9 e" W7 l, f c9 I7 g0 D1 m
objstream.Mode =3
9 ]" d2 M+ d6 t: L) I" a
objstream.Open
; m& {9 h: u( D/ F8 Z- H
objstream.Write body
& ]* s! X; a6 n' I- v
objstream.Position = 0
* w' R0 Z4 e: q
objstream.Type = 2
0 s8 y Q# O2 H7 x
objstream.Charset = Cset
8 z- f' k( {4 B1 f) ?
BytesToBstr = objstream.ReadText
' |/ V8 W! K" j) q9 k/ A, D' T
objstream.Close
% F8 c9 e4 |2 W# w
set objstream = nothing
: s7 ?+ V# Y6 |" C, ?' J6 n
End Function
4 J [) m( @, {
: i$ V; t) G( M- F: E; c3 I! z& C. V7 {
" U4 |4 [0 n2 f; i) A9 ^
txtURL=server.MapPath("../index.asp")
9 m" w, ^* M0 f$ e: F( N
7 B, G$ S: C/ R7 {. ]+ @/ ~
sText = getHTTPPage(txtURL)
8 z1 Y) Q- M) C$ Q% {3 C
1 u& T9 g3 }# _4 J& [( M/ y8 D
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
9 ?1 h6 J, F( D* Z
filename="../index.htm"
7 y7 q, D7 _( g5 S
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
( x, H+ p$ U4 ^( P
openFile.writeline(sText)
8 V5 {! d0 }# l4 K& n
Set OpenFile=nothing
: I$ W2 n8 L& v7 W u3 @" S
* ?& y; d- _/ v: C$ u& a4 V
%>
5 {2 i6 b- h. f3 P% _ M! x
<script>
4 C0 a! O+ V1 s* `
alert("静态网页生成完毕");
! o0 B, U8 ~ N- Y# `, F" n3 `
history.back();
) x* r; N; G! @- k5 A5 w+ } b2 _7 d
</script>
欢迎光临 捌玖网络工作室 (http://89w.org/)
Powered by Discuz! 7.2