返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 5 @/ g9 j# S& I9 ?$ a! l1 e2 e
www.aspid.cn的主站就采用了TSYS生成html文件! 1 M% ]  k% K. [- p$ w$ ?* j& |. q
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
7 w# Z+ t7 Y. c  c8 G. J( P+ p6 \5 t4 ~, [1 U- h) h
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% ) s  c# Y6 `# @8 ~
filename="test.htm"
0 n- _) K( |( P7 s7 Cif request("body")<>"" then
: \, j4 o  x1 p8 O2 Nset fso = Server.CreateObject("Scripting.FileSystemObject")
9 ^- Y# L, h5 f; _" N5 j$ Iset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ' S" x6 ?6 O. }% L) _" L( p) }
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
9 e. e: J# }0 M2 L1 t. L/ ]htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" 2 H( j: l) N: G4 h% V, o6 I2 B
htmlwrite.close
( i0 K. Q) G2 X/ p7 A- U3 aset fout=nothing
! t8 {9 r: O4 J9 b4 sset fso=nothing $ e& P, \  L4 }4 T. C# q% d$ F
end if + t4 c8 U: D% f$ G9 l& ]
%> ( i* J: x" A& ?" p
<form name="form" method="post" action="">
7 o$ M8 n. V+ p' _9 q<input name="title" value="Title" size=26> 5 Y" x( ^7 G2 ^' n8 u' f
<br>
; x) t, J0 }$ r" M% s<textarea name="body">Body</textarea> / [5 y! b5 U: c, E; a
<br>
* G; D3 \2 t, R<br> , t. l9 [' j7 S- B. u4 L
<input type="submit" name="Submit" value="生成html">
) |% `; H+ Q" }( R9 A</form>
$ R3 }  X' M- A. a; A, X' w2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. 0 s9 I$ W) ~9 U6 S2 c6 A
template.htm ' //模板文件 <html>
( i0 c& E% {+ F' `<head>
( P$ x5 |6 n5 G; I" o  A<title>$title$ by aspid.cn</title> 8 N* n1 [. W4 X" x8 a4 E: }
</head> 5 f1 I5 @3 Q0 F# {  m
<body>
* N+ R- |) B8 r/ h$ X: O+ w/ l4 n7 b$body$ * A: {4 }4 S! u$ C$ k) h1 M
</body> - s; z8 D0 T  h+ T
</html> ? 4 c5 V# [8 W' J' P4 [

1 P3 ?5 b' ~: L& oTestTemplate.asp '// 生成Html <%
$ H" e: _! c. v% WDim fso,htmlwrite
% Q) j+ g6 E9 ]" K' }9 j% pDim strTitle,strContent,strOut
. ~/ c/ A0 X+ D6 Y# K. o  [- ]'// 创建文件系统对象 ) f& `& S. i) c9 |" T. u6 r
Set fso=Server.CreateObject("Scripting.FileSystemObject") 7 S$ i  }; k# G2 _( `2 F& j
'// 打开网页模板文件,读取模板内容 $ V) H6 z( D9 ~2 {5 R
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) " a% {0 K) ?3 I9 M1 A: ]
strOut=f.ReadAll
" c/ O$ S; P9 i3 D* L5 @htmlwrite.close
2 G  A  g0 p3 Q8 x" v, M. C
9 N: j5 t7 `/ Y" O9 h8 HstrTitle="生成的网页标题" 5 u. p4 Y/ ?7 Y8 t8 H+ M* I
strC 5 }( x& Q+ u, g/ j# n% t) V
1 L; @! ]% b4 K2 s/ x
'// 用真实内容替换模板中的标记
: L5 @. p! h+ G0 I& UstrOut=Replace(strOut,"$title$",strTitle) " ]: j! H% d, K; U: h, h! f2 k
strOut=Replace(strOut,"$body$",strContent) 5 A! _+ r5 m+ Q

* \% j+ H  j! x* T/ n'// 创建要生成的静态页
7 E6 ?2 q* W1 e3 X! _& _% E, oSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 4 ^$ e: D. J0 H! A9 n7 K  U
! w7 N/ ^( [7 l
'// 写入网页内容 ! d  P! L( S5 b0 ]3 y# u
htmlwrite.WriteLine strOut 5 {6 r$ ?# z0 h/ n$ u* w
htmlwrite.close + Y& Z, @  y$ e$ `  t. }! o
+ P8 q- ~5 e0 i7 ?
Response.Write "生成静态页成功!" " v6 v: a- H- d& i$ y6 G
6 h6 S6 H% Z8 L9 u; O7 ?6 O/ f
'// 释放文件系统对象 ) C6 o0 A9 ^0 u2 J- ~" A5 E
set htmlwrite=Nothing * H4 }  Z/ o& h0 @
set fso=Nothing 7 p/ o( }. A$ y1 W$ H/ x3 z# E1 h) i
%> 6 y9 P: b3 j/ W5 J  s
) S3 Y$ k% a5 X, e/ J5 e8 S, s
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 6 i5 }/ i9 d( K" b! W3 F8 d
<%
2 H, |) R) Q  h+ k
+ i& w* a5 M. c: h  B: e- n'常用函数 * E3 y3 K7 s' B* P, P" y! z) L
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ( p! `9 r5 r4 R8 @
function getHTTPPage(url) % O7 ]! s6 m4 S5 w  l& S* ^
dim Http
3 m7 S0 Y0 ~# c4 D* oset Http=server.createobject("MSXML2.XMLHTTP") 1 |8 c3 O% @" M) ^" T! s  ?
Http.open "GET",url,false
5 P) a" v6 t# ^! a" mHttp.send()
% D, v' M  s. g2 \6 i1 jif Http.readystate<>4 then # l3 J! {3 k7 F. h- M7 d5 ^
exit function * g: o" S  u; ?0 I$ t2 e; @- d- i
end if
: J9 H# P  |; {% S# }- M; j- K. e. VgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") ( k( ^3 e. S. j' a, ^) J
set http=nothing 3 l: F2 g+ B0 j
if err.number<>0 then err.Clear
" Q; q! Y: X! W, @end function $ T3 j4 K* R6 d& t7 N+ O
- z; {) m" Y5 `8 i. R2 M
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
- \% z; F, \7 t: `; aFunction BytesToBstr(body,Cset) & o8 j9 M  g5 Z6 v& j( K* Z
dim objstream . E' D9 I3 e2 ?  \+ K
set objstream = Server.CreateObject("adodb.stream")
/ }! L/ s5 S7 ~: U8 b. |objstream.Type = 1 9 |, }7 G( C+ v% @( c5 F
objstream.Mode =3 3 h% }4 J) S+ q5 P7 X6 T0 t
objstream.Open
6 H: C/ u5 @; U) jobjstream.Write body ( T! h* r# R' _+ o$ S4 o
objstream.Position = 0 6 s2 w  E& c! O7 Z7 d3 t( d
objstream.Type = 2
$ ^9 z9 u! P) z' Xobjstream.Charset = Cset
, k- }2 \  n2 z5 eBytesToBstr = objstream.ReadText
: @+ j9 ]; `9 H; c) x; E7 Vobjstream.Close + m( @7 o. h1 h, B8 q8 R
set objstream = nothing
3 x& M. O/ }! _% H  UEnd Function 4 |6 U$ p: m# ?; k8 w2 q7 k

- s* a$ N8 l  J& j! R& W; [- M# x' o! }6 r
txtURL=server.MapPath("../index.asp")
. Q+ A, @  v. i9 J3 {" G% O7 z5 l- a. O( w  u
sText = getHTTPPage(txtURL)
% _- s0 {) U$ V& r. A3 ?6 _+ k0 M. f- D% ~" R2 Y
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
: l* o1 s8 `7 [' ]$ hfilename="../index.htm"
! W1 G: Z/ ^) dSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 % U0 [# G2 X5 }  h0 b' K, H
openFile.writeline(sText)
4 }( t5 y0 T1 QSet OpenFile=nothing $ Z/ c) T2 ^- l/ T' k- T: e( f

0 k; Z. d2 N" ?2 K: E& @* L%> , v3 [1 }) n2 |  b& F( W
<script>
% c5 x0 K9 Y2 F) l/ balert("静态网页生成完毕");
) J7 r2 n, Z( b$ {$ |8 f; Ghistory.back(); - l, Z, ]4 w% Q* z9 L+ c; ]
</script>

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