返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
; Y3 H% F3 H" }www.aspid.cn的主站就采用了TSYS生成html文件!
" b& L- a- L! W# I所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 2 \- n+ i6 H1 l- H" ?% ]
5 A/ q8 G% ?3 _" _1 b
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% ( I5 A: O6 @$ Y* Y( i3 a8 o9 Z
filename="test.htm"
0 m  E7 N9 @. dif request("body")<>"" then
9 H# e' f+ N, ^$ y  r* Kset fso = Server.CreateObject("Scripting.FileSystemObject")
3 ?  O+ |  A1 xset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
( ?3 |4 r5 R8 U- Lhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
' L, m6 {8 O; D# Y2 S# A* Khtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
% s$ j5 w# r! Y0 G/ r9 ?: m" R5 \4 }htmlwrite.close ! V/ w" y$ L( q8 }$ B3 r, `
set fout=nothing
" _4 q) T% t, Y; |2 B6 R0 cset fso=nothing : U0 F, }8 `: B: u; x0 o& L0 s
end if
( N/ n; T' d; l; [: C" @9 B3 J* Y%>
: R( x0 n9 I3 S8 w9 U+ C( S! n<form name="form" method="post" action="">
3 ~' E# o8 o# v0 l<input name="title" value="Title" size=26>
5 m& J: v  {* s, L! Q) J! h<br> 4 v" Z, D! S+ v& n2 z* i
<textarea name="body">Body</textarea>
: b! q* c. K4 X* N& s* `- d<br>
8 I$ |4 b$ l. o- c<br>
1 E4 s- u+ A/ y* C* q<input type="submit" name="Submit" value="生成html">
+ A+ z( }' u* c% p/ W( f4 @</form>
( j6 i: J7 x9 @% o/ e* S2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ) O7 O* E" l4 d( i% ]$ L" K
template.htm ' //模板文件 <html>
8 L2 p' ]" h2 F0 e( U+ |- d<head>
! j* T( c  p" A  [3 l3 ?" v' |<title>$title$ by aspid.cn</title>
" W! M1 q" h  l0 K</head>
% l. \1 h+ F7 K<body> 0 w9 `, Y* {: E- q* n2 u8 r
$body$ - R; I9 J; M6 |* w& Y: [
</body> 1 \. X1 [5 Q) z
</html> ?
4 Z! n, ^+ ]1 k2 w4 P7 v6 Q" c& Y3 D' C7 q: }, G2 ~( x; V0 i
TestTemplate.asp '// 生成Html <% . y* Q3 r% `, \0 ]. Y6 ^1 K7 ~4 i# ^
Dim fso,htmlwrite " O7 S7 J& g- z8 `' k1 Q+ @
Dim strTitle,strContent,strOut
* i: t! M4 n+ m# W3 z1 T'// 创建文件系统对象
5 Y( d7 s* e5 TSet fso=Server.CreateObject("Scripting.FileSystemObject")
; o) m0 v/ |2 B. T& s: N. w& |'// 打开网页模板文件,读取模板内容
7 `9 ?9 i* G! Y4 aSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
. b, f6 o6 z8 PstrOut=f.ReadAll
$ ~9 F5 t. h! k$ }! x1 r! M; K1 R7 Rhtmlwrite.close ! k: u, h% J; W, d

5 G7 \4 j: l7 Q1 V* g; M7 CstrTitle="生成的网页标题"
! j! b9 _1 M" G# D5 lstrC
0 h" ^. l* s5 W. N) c
3 Y' j/ I4 \6 d: U# ?'// 用真实内容替换模板中的标记
4 k- G; t7 n; t2 OstrOut=Replace(strOut,"$title$",strTitle)
* A, P3 D! P7 B1 v  ^/ U9 tstrOut=Replace(strOut,"$body$",strContent)
" U) ~1 x$ B6 J$ U
) d- c% E4 d) J1 h'// 创建要生成的静态页 0 O9 }, I0 V; Q* k5 o
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
( V0 R) T* K. ~2 x( F1 Q
" t3 e. F& p: i. {* b& a/ V7 w+ B$ }'// 写入网页内容 0 D3 v2 Y9 _7 C7 b2 z: @
htmlwrite.WriteLine strOut
8 N" W6 M" a3 Y: Q. Phtmlwrite.close / O2 ]5 J& D& ]2 t% }% ^4 m0 e
" d3 s$ V+ H1 _6 ]% Y6 b
Response.Write "生成静态页成功!"
; P3 a% h/ K( I' M& ?& e$ v/ O1 e0 ~
'// 释放文件系统对象 : ^  p" }5 \- G/ a
set htmlwrite=Nothing
2 H& L3 y/ O3 }( vset fso=Nothing 2 b* p7 Y" ?6 h5 i" L
%>
2 W/ G1 Y- B; j& v7 L9 j( u" y( c4 D% h) L& r
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ' D# H0 x( p: ]' E
<% 1 d0 c8 X1 Y+ w/ S  v/ w, X
* L' M% S' \% B7 S+ H
'常用函数 % t7 O& ]1 Q( \/ P
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
1 b; z3 M1 N/ ~0 R6 C- Q# D2 vfunction getHTTPPage(url) 1 K+ n- M1 f5 [) n& O$ ~
dim Http ; E$ u: E0 V; H/ g6 a/ k* @5 M
set Http=server.createobject("MSXML2.XMLHTTP")
/ u! L" y0 ?+ l! ~+ q# s' `Http.open "GET",url,false
/ B9 F- u9 C9 M0 _0 jHttp.send()
6 d9 q$ x: P+ a$ \, N' J' Xif Http.readystate<>4 then & ^) `2 Z# U0 N) V% u( ^2 L8 l
exit function ; G/ E6 y' V$ v
end if : D* Y/ m. P" u$ A: q6 o
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
$ t) t- M& J; J# `" M7 `3 {set http=nothing
0 T: L: P( B5 [' _  L& Tif err.number<>0 then err.Clear
! p7 ?+ p1 y" M$ Y" ~7 w# |" b/ Fend function
* f$ Z# m; U; G7 W) Y6 w) t6 t
" A7 x; G. T& q' E3 @% d! @'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 4 F' W% o- F* @6 A5 Q8 J& l* R
Function BytesToBstr(body,Cset) * {* t) O0 ?% g3 @* K# k8 Y
dim objstream , O% z: f  H$ J! |+ p6 m1 _- Q7 f
set objstream = Server.CreateObject("adodb.stream") 6 X3 C2 Y: m3 C$ \, ?
objstream.Type = 1 4 b7 t# ^3 J: L: L+ a1 }3 h
objstream.Mode =3 6 I' J; G7 s  I. N5 J
objstream.Open
1 g& H& r  \0 x4 E7 r! A9 cobjstream.Write body ) U- o0 p& h+ x  b; M$ ^
objstream.Position = 0
& d. i# Y4 i& Q/ v! X# O( fobjstream.Type = 2
9 i3 g; `; i) _2 ?) `. w( l  tobjstream.Charset = Cset
( R% h3 z' H' {% U% T) q) ?7 vBytesToBstr = objstream.ReadText
0 ]5 w( ]' ?  z9 c5 B- jobjstream.Close 4 U( i  I% t7 \3 {
set objstream = nothing
9 z7 m) w4 T2 L# {End Function
: _; M8 i3 L* z2 W  i4 K0 r
% j7 M. q0 o$ ?( u5 `  H" \/ H5 R  ]1 B6 j. w
txtURL=server.MapPath("../index.asp")
# g( p! {$ N  h
0 F# F& j' z9 X" _# S& \+ c2 g5 gsText = getHTTPPage(txtURL)
9 n  F7 ~1 \5 `$ I# b4 p& g/ E! ~" t% X6 [9 a* |
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
( Y% u* j- e" g0 j8 efilename="../index.htm"
& A' ]4 Y& ]+ T9 ~* m6 VSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
) n. r* g  O0 f9 h* \openFile.writeline(sText)
3 k0 j- ]1 Y: }' w9 L1 i( kSet OpenFile=nothing
! g1 a, z) p& s; z/ C% S! U$ j6 p& b4 s
%> : `2 _6 ]- E5 Q4 l) `$ x! m4 p
<script>
& _( O5 ^$ ]6 x: F, j: Zalert("静态网页生成完毕");
# a) D$ u, H  L2 I) zhistory.back();
  [9 A' U7 F. p. b9 O  d1 B</script>

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