返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. # O/ V% [) \- |1 g; @0 N& P, u
www.aspid.cn的主站就采用了TSYS生成html文件! ( Z/ |! E- l, a# i+ [( d: _
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 3 l3 ~- }5 x- t3 {

' C6 }3 I$ l! Z# j9 N* L6 m6 e: j1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
7 ^; ?4 X9 j6 p0 ~4 p0 V8 Vfilename="test.htm" ! r9 G: f3 w0 e$ u( r
if request("body")<>"" then 4 |9 U& Q$ I$ p
set fso = Server.CreateObject("Scripting.FileSystemObject")
* P) C# k3 |! P- d1 Gset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ; |/ \. z" ?! I/ G) `
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
  k3 ^; B  Z; zhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
& K' y( e: U& ?htmlwrite.close ; a/ q# q, N. D3 g3 b
set fout=nothing
( C- B6 i% Q# {/ `" H  sset fso=nothing
) ~+ v+ a; T  Q" _: b" `. f+ h% D1 Send if 9 V- n8 S4 r, ^
%>
) t( Z+ u/ z; N4 p) V% h<form name="form" method="post" action=""> : ^2 Z. \9 P0 p; Q( m9 v
<input name="title" value="Title" size=26>
1 R  v; d3 {" B- E7 H* F<br> - j0 @! E6 z5 t1 R5 R
<textarea name="body">Body</textarea> 9 b) x$ M5 u3 u, w6 s- I
<br> $ s2 j  P, e6 A6 i& j2 l7 i
<br>
- ]8 _0 h: k5 r( k<input type="submit" name="Submit" value="生成html">
$ K5 n6 h4 I+ P7 b; i$ a) S- k; p</form>
. O3 h, c0 t0 H2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
  h1 ]! l! }  [! N( A# x+ G& Qtemplate.htm ' //模板文件 <html>
4 x! H7 r0 h7 y6 N$ Z/ R+ `<head>
% X! \( w! M! o" ^1 R9 x<title>$title$ by aspid.cn</title>   S* \* {6 D1 O& x
</head> , Z. x1 T0 Y. C# h
<body> 6 D: L8 J5 F" @# g
$body$
. n6 W- v# U* V3 ^' N</body> ' k5 n1 I9 W# f  H, _: K( }
</html> ?   h+ \5 {0 r: k% F& ~* o

- K* G, y1 Z) r7 L4 }4 G4 F+ B; cTestTemplate.asp '// 生成Html <%
1 z0 S) ?1 F& g% R. GDim fso,htmlwrite % }7 X" D3 ?- n
Dim strTitle,strContent,strOut % L: A, t2 A) @
'// 创建文件系统对象
$ O  P+ {, d! u5 kSet fso=Server.CreateObject("Scripting.FileSystemObject")
  p7 q; A8 M' E4 ]( u6 ?. U" c( A6 e0 N'// 打开网页模板文件,读取模板内容 ( W: l9 H" q9 o3 a
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
# z  c* Q/ E! M" i7 f! estrOut=f.ReadAll . O% E1 _6 \+ F4 s. Q1 F
htmlwrite.close ! b9 I0 q: P' W% e2 H* f+ F2 I

: r$ f; A0 a0 u4 R; wstrTitle="生成的网页标题"
$ V, Z. B8 o" ~1 {! `0 {& {9 a  YstrC
* R1 @0 F( G! {' |+ n- P/ O4 O* d: ?0 o: |' ]
'// 用真实内容替换模板中的标记 , A  `0 _+ \( n9 ~5 v4 ~' B
strOut=Replace(strOut,"$title$",strTitle)
3 h$ o8 d- |5 y( i4 j4 M, IstrOut=Replace(strOut,"$body$",strContent)
; g! r! L- X9 Z3 e# j+ I0 Q" A$ j/ i3 O* \9 [7 O
'// 创建要生成的静态页 8 }$ s/ E" n7 }, o& O3 N
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 5 [6 s0 R9 O% I8 Z2 H7 p$ L+ s
/ g. {* G# p$ }9 o" b! R
'// 写入网页内容
7 g  Z" u* C; Z) O5 K4 Jhtmlwrite.WriteLine strOut 1 S3 F  ?  m7 Y+ M1 N8 v& n3 I2 U
htmlwrite.close
) J) o$ ~2 e6 w1 l' p* B
4 j/ ^3 R9 d5 [; U  a  rResponse.Write "生成静态页成功!"
) X% w: @& k5 e4 q; D
( n6 P# Z/ V4 ]. [8 z/ X8 k'// 释放文件系统对象 7 x  O( r3 i# {1 E/ f
set htmlwrite=Nothing 2 {0 b8 J4 V# f5 Z2 S
set fso=Nothing
/ y' p) _0 s& |/ j2 P4 C4 c- \%>
7 t, B& C/ j1 }6 ^' [9 T+ p$ h
' Q5 e" U* k; W8 J- ~3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
' R7 K/ `6 J' d* a# j: ^<% # Q) U% R* C- `) d; y8 s% o) g
& k' s2 A6 R( g9 o& F
'常用函数 & H5 W9 X) z7 Q; Q, J
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
; V2 H2 g. U3 n" U  zfunction getHTTPPage(url) * ^5 Z- Z  O9 w. _0 b3 E
dim Http 8 e0 @0 j. U$ b- n' s- O" _, v
set Http=server.createobject("MSXML2.XMLHTTP")
8 ?- r* {* x/ `* J; \Http.open "GET",url,false + p# ~# h  w/ X3 G' A- H
Http.send() ' i7 L. Y9 T# d" z; z  m
if Http.readystate<>4 then
3 ]# @% b' x! g; K: Q) h# v" r0 V  Fexit function
4 P3 _) V: r. f  e, E  jend if 9 e1 Q) ?' s) O3 j# x9 d$ o6 {; X
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") + q3 n* T) d) X+ T+ m
set http=nothing
. c; ]' F! U) }3 Oif err.number<>0 then err.Clear
) q" w! n9 ?' p7 eend function   {+ x3 z* H; I! a. v
0 N) W2 n5 p( G* |& [% ~
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
5 X( p9 G4 v4 H2 L7 t# sFunction BytesToBstr(body,Cset) , A  L% I6 k+ k7 Q6 J
dim objstream
( j4 d5 @' a+ x- L. y, H' |set objstream = Server.CreateObject("adodb.stream") 5 i0 R$ ~0 r9 X' A
objstream.Type = 1 , F! u2 [& u( R5 {: M% d
objstream.Mode =3
5 @/ ]+ Z% |+ o6 ~0 Sobjstream.Open 3 i; `. q) D4 W+ o0 C7 E3 A
objstream.Write body
8 z' \4 ?" d9 W7 o4 S3 H) Dobjstream.Position = 0
- h/ Q2 q; j3 J! R: e  y! Eobjstream.Type = 2 / C, I) x$ @% O4 l7 `% W
objstream.Charset = Cset , M4 L$ R, T8 v  R) V1 R
BytesToBstr = objstream.ReadText
1 d) A0 f, a8 Fobjstream.Close % S" o- Y& m( C- O& H1 U, w9 J( I, i
set objstream = nothing
7 [* s! g: H. J9 |- t3 \! j: IEnd Function 4 ~8 {: u* {( x* T" y
% H4 r8 c6 L8 a5 c

9 G  d* \3 w8 A1 G5 G; AtxtURL=server.MapPath("../index.asp")
- x% r  ~2 ?" _  W
( u/ ^' u! ]% x& @sText = getHTTPPage(txtURL) ' Z- y! x( K7 T" N, x3 |) ^

. b4 c9 U  ^4 Z# p& @Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
* s# i: W! S0 D: m+ w) h7 `: I, i- Wfilename="../index.htm" ( b2 X% ~/ M6 k+ \. M1 _
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 : S& N% J7 l! c/ M! P
openFile.writeline(sText) : y+ T& A4 w6 M. J- I
Set OpenFile=nothing
. v/ ~2 T# H; j; X+ ~+ H+ F
  S/ c1 k  v# f0 j" X4 f% z0 g%>
8 g& s. E! X6 ?! `6 B<script> 0 i: D4 N9 t; Y+ \! D( `) I. m; W6 {
alert("静态网页生成完毕"); 5 f0 u; U4 S9 M3 g2 ^* m
history.back(); . j  O- Q0 o. b9 A0 U3 n3 M6 d8 G( n
</script>

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