返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. $ U# U# a+ q9 o: d: b" `
www.aspid.cn的主站就采用了TSYS生成html文件! ' x0 V* K. Y" T& o( Z6 j
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
' C) a/ N7 h+ B, ]+ K* O  I. e6 G, n3 j2 ~' A; c5 K
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
6 P/ w! K2 O; ?4 `filename="test.htm"
0 `: w  E6 }$ @( o1 n9 Xif request("body")<>"" then   Z7 `% b! {& U4 D( P; V
set fso = Server.CreateObject("Scripting.FileSystemObject")
2 R5 @+ J/ G8 sset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
: s4 ~( x# o# j: h! z# f8 m, ahtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ( v3 l( j4 Y- l7 _+ l
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
& A, F, G$ L. O" ?/ [. ehtmlwrite.close
5 K1 Z. S8 ~) B/ dset fout=nothing 5 r1 ?; D! U7 c- y
set fso=nothing * e' n8 p3 X4 d0 R
end if 7 n/ M# W' d) t$ M6 c8 T
%> / v5 q% G6 T: @' h1 w$ O- n7 o
<form name="form" method="post" action=""> 4 P4 e- s# V: ?* k1 G9 l7 U) g5 y
<input name="title" value="Title" size=26>
4 W/ j1 Q# \0 n1 |8 X5 p# D: k1 p<br>
( y* W6 i+ ]1 D8 h, l9 A<textarea name="body">Body</textarea> 3 N0 i+ y* d# Y
<br> " p- {$ `+ P% b# I! m
<br> $ m* {) A9 z" U- s, j
<input type="submit" name="Submit" value="生成html"> & @: s3 Y' B* B4 h; u5 z
</form> / I1 G, }( K7 c
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. / }  W! M# C# u9 j8 f  @; d
template.htm ' //模板文件 <html>
+ I# R" V1 {. ]0 w% U1 _. E0 y3 A' X<head> & g$ _  I3 n& d& I; A6 s
<title>$title$ by aspid.cn</title>
" J+ [8 g3 C: V9 M, l0 G6 o- C& Y" i</head> " \6 }, |, r! [! a
<body>
% k  |5 a& |7 i$body$
& O- @( W6 O- ^+ Y</body>
% f& Q$ y/ ~5 }4 d</html> ?
5 q! [+ @. a' ]) y7 Y, h5 s2 y) l. p6 j
TestTemplate.asp '// 生成Html <% 5 e8 i& q& K4 @; B! z
Dim fso,htmlwrite : O* ~8 [+ @( i# E$ }) \& }1 H
Dim strTitle,strContent,strOut
- R9 Q7 D: L: |4 Z1 s2 `: W2 d'// 创建文件系统对象
* Q8 D  Z2 _$ V+ u4 l1 y8 ISet fso=Server.CreateObject("Scripting.FileSystemObject") % P: j0 y- z0 z
'// 打开网页模板文件,读取模板内容 : z- n! w# A( ?) m1 M
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) . l# ?1 ]: f+ J8 x; [# f1 L
strOut=f.ReadAll
, e2 ~  A' c4 X6 O! Nhtmlwrite.close
5 k' C2 u  Z& E" N8 g0 ]  p3 z6 `; a7 D
strTitle="生成的网页标题"
0 u% z2 E) Y. Q! C% ?strC
2 A2 Z7 _) H0 B8 E, s- U1 ^2 Y" C/ `% D% M3 d6 p9 }8 Z! @
'// 用真实内容替换模板中的标记
4 S5 I( D5 [" a+ w4 ?* `strOut=Replace(strOut,"$title$",strTitle) 8 A# {( ]2 ?/ V6 C6 }; u
strOut=Replace(strOut,"$body$",strContent)
4 Y: S- r0 G% j2 r* ^! B# _6 G. L8 d9 U: }% J7 E, E" g
'// 创建要生成的静态页 . j- c3 d9 v* A4 f5 t6 Z- [
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 7 b: B8 \$ x5 [1 d$ K/ N$ r- t
" Z2 h- F- V1 J. y2 q1 s
'// 写入网页内容   l! I% G; B2 v. J  k& x
htmlwrite.WriteLine strOut 2 B# O8 {* O; f6 f
htmlwrite.close 3 v: m7 o" s- j' R6 B" W1 N

* l- y3 O3 i; f, c* qResponse.Write "生成静态页成功!"
) J( ~( ], I7 j2 y
0 `5 z# Z! t' d- O( ^- G+ T'// 释放文件系统对象 " L1 G- w4 ?- ^& `2 i7 R0 O
set htmlwrite=Nothing * q0 I4 W9 l1 L0 y& o. S! ?4 u4 J
set fso=Nothing
$ l2 S+ ~/ R7 ]( ]; B+ b%> " l6 S6 |7 C( i

( r, a& `6 c% F4 }9 S3 y9 @; c: W3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
: u8 c+ h# y6 T% @8 P<% * _) u0 l. t. d1 ?

8 R( M2 [( U; G0 i'常用函数
* V% \( N) P7 V" M7 _% f4 I'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ; \. c) _7 q2 o5 c
function getHTTPPage(url)
3 B) q; N0 [7 r: Hdim Http
! x$ l1 E" |% z3 Uset Http=server.createobject("MSXML2.XMLHTTP")
- P) }% j2 g0 t; d# aHttp.open "GET",url,false 7 m  v* f0 x7 o* P
Http.send()
7 X5 ]0 Y% Y+ I+ U; yif Http.readystate<>4 then
6 f/ G) H* E7 lexit function
' {  E, t( W7 q. G. oend if 1 j, a! q! v- r& W8 H2 _7 j# C
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
6 a9 N6 N3 _$ W) @& h3 i( ^set http=nothing 4 Z4 v+ J* C4 u, D! J
if err.number<>0 then err.Clear
% D  C; i5 j: U1 a: c( a" \end function
# C& e' J* e. e  X" J# v8 v7 X+ s: w' _
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 0 A7 {0 \8 v. e
Function BytesToBstr(body,Cset) ; x% V) ^$ J% F, e5 F& ^  k
dim objstream
' S7 j1 [6 g- ^, z! |* Aset objstream = Server.CreateObject("adodb.stream")
6 H( Z8 F# A: G! X5 ]objstream.Type = 1
+ y% d, N7 k7 \- q7 v- pobjstream.Mode =3 7 f5 f" C5 ?2 f* ~7 W/ v- K8 m+ T
objstream.Open
' u3 T0 V  Q% ~& d$ Qobjstream.Write body
+ D# X, U+ Q+ yobjstream.Position = 0 ' t9 p3 ~2 D: @# y7 R% m
objstream.Type = 2 8 t4 H% H/ L9 T
objstream.Charset = Cset , s# c( J! v( i2 Z! @
BytesToBstr = objstream.ReadText
& l/ N- }) d# n6 b) u5 Q4 G+ x9 k- }! cobjstream.Close 4 d. j2 w3 C" ?& u4 L
set objstream = nothing
8 u! Q. ?% [; o; v0 zEnd Function
! ]) u8 M" e  z4 Q0 o* r2 R4 o! ]2 b2 u' J5 }
$ }& w7 ^+ E2 @' N3 v5 E4 }
txtURL=server.MapPath("../index.asp") , x3 b5 r7 Q( Q  O: c1 V$ O

; w9 C$ b2 d& {7 [sText = getHTTPPage(txtURL)
0 l/ S* T8 N1 W) C4 Q0 n( `6 s/ m" _- N1 Z- A
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") : u7 Q3 u$ ^- q$ W7 L0 S
filename="../index.htm"
, A3 y3 X7 W# a: f1 NSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
8 F" {/ x9 z8 X5 ^3 U# popenFile.writeline(sText) ; S$ E2 I; l! C2 F! t
Set OpenFile=nothing
; X& X! ]7 `0 c! q
( X6 \9 j5 i& _" a7 U) R* p%>
0 z* i3 X7 e' B9 N" a% K' J8 E. M<script>
$ f9 H9 W5 v# D, v0 q: oalert("静态网页生成完毕");
. A/ o& l& a: A3 T- Khistory.back(); % A- a$ m; w  x. _6 Q. [! a
</script>

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