返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. * i+ N, ?" D0 h
www.aspid.cn的主站就采用了TSYS生成html文件!   z" I5 `* D& s4 {* r1 J
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
) l/ ^$ e8 t( M0 e4 A1 G0 n
. o+ O: x: j4 b5 W+ C& x3 h1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
) V, Q5 _$ e7 |; ?$ F4 c, kfilename="test.htm" : W; j4 ^5 Y) f9 _
if request("body")<>"" then
9 x$ z( y  B& ^2 i6 \: o3 b; bset fso = Server.CreateObject("Scripting.FileSystemObject")
7 z! P% `) y2 c$ ?, m0 E4 L  l6 I" G. ~set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) + ~* g- n& ?& w5 f1 _+ M% j( X+ b3 m
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
% b1 k9 i& Y+ ]$ Ehtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
0 v5 ^: m. g  b# khtmlwrite.close * g3 k9 ~) ^; _
set fout=nothing
+ j1 O6 Q$ Q2 ?% U, j; R' J6 [! K* `set fso=nothing
8 ]; h4 M7 V* v; G" ~, xend if , Z* _+ T' ^9 s- U4 S! A1 D; E
%>
$ U2 l" Y. E: @* t/ V: A0 [2 [<form name="form" method="post" action=""> % d* q1 L( t8 v- b6 S: t* Z
<input name="title" value="Title" size=26>
. \9 b: L( j) O. l<br> * D6 g. i0 _2 }" T- o( n( G1 |, O# P
<textarea name="body">Body</textarea> - l! k& A$ z) n
<br> # ]- g& R. E0 G, p" {
<br> - g( A  G, x% C5 q( D( l) Q8 [
<input type="submit" name="Submit" value="生成html">
& F6 f* A  c' W. w" b</form>
5 ^+ E: z7 }6 V" e9 {2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
- v5 ^: s0 T; j4 |template.htm ' //模板文件 <html> 6 V1 V7 Q/ o7 l+ q% {  \
<head>
' S* o4 m. F1 @% ~8 O7 |) @0 w<title>$title$ by aspid.cn</title>
. z' s7 {$ T1 K/ p% w( H</head> - P4 k# L6 r' M) b1 Q% |% g
<body>
; G# h& ]( R9 _, i1 |$body$
: d/ f/ U4 C- N& R9 |</body>
# [$ z7 U' T; w# d% f0 G</html> ?
5 y3 Y% B& x4 }3 _2 y9 P
- C& J9 _9 t5 ATestTemplate.asp '// 生成Html <%
/ h5 ?& Z$ T7 F8 g; A7 R* SDim fso,htmlwrite
7 q9 \' z+ a: R( W% xDim strTitle,strContent,strOut 7 l% X" `9 z1 f9 {4 |' E
'// 创建文件系统对象 4 O3 U9 ^  @5 ^6 e' _, n  C/ K8 r9 T
Set fso=Server.CreateObject("Scripting.FileSystemObject") ( [7 n2 x) I8 [% a; ?8 l* L
'// 打开网页模板文件,读取模板内容 + t7 e6 R" M3 i" Q
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) - `; P/ \6 P4 P2 U, U# n) o
strOut=f.ReadAll
8 S/ ?/ c- V- y8 y$ I9 P. Qhtmlwrite.close
+ Q: `8 K* y' N+ }+ Y) I+ v
5 d# N: p, h0 |strTitle="生成的网页标题"
4 k' u  x0 }6 lstrC
' z' d8 Q  q. k
4 E" ^7 L) I8 t4 P  L) t* q: ?0 Y'// 用真实内容替换模板中的标记
4 G7 X6 S8 x( t: {+ T" ostrOut=Replace(strOut,"$title$",strTitle) # C7 v1 |! s: D6 `  E; u" D
strOut=Replace(strOut,"$body$",strContent)
5 d: \& Q" ?( b
$ V6 |6 |: v3 @'// 创建要生成的静态页 . ]) |; f8 L5 z6 |& k% R% w
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) & g7 Q. o# d5 g" g$ F9 l

9 U; G& w8 B& Y'// 写入网页内容 ' p6 P" C- w" o$ t+ w# i4 K5 Q
htmlwrite.WriteLine strOut
$ Z7 h( W" \- h. chtmlwrite.close
- y+ L; R: x/ c+ X3 {8 D2 A0 g0 w8 J: s8 m
Response.Write "生成静态页成功!"   n& I( @& ?( m1 Z$ z
9 n$ v3 O1 ?% ~. W, \. {- i/ n
'// 释放文件系统对象 9 A/ A  U( t% e  L9 l4 G
set htmlwrite=Nothing
& G/ w) j' j" wset fso=Nothing ( m8 i0 H( l" k3 m
%> & x4 A. a0 m* F+ C0 m: T
+ U4 T" `8 E1 ~) P6 v
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
( ?& |4 y; u8 w1 Z# B% Z- ^<%
0 q9 Y' p. K8 h/ k" i9 Q& {
9 O& s, F0 C8 a% l7 U1 e2 M' g" b) P'常用函数
8 i/ m7 o1 n. v2 I6 e'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
: W! p( ?# b$ _' I. \" Ofunction getHTTPPage(url)
( R0 m4 g( r! y5 l* Pdim Http
3 j& E9 h+ N" Yset Http=server.createobject("MSXML2.XMLHTTP")
5 z5 `$ ^6 R% A: RHttp.open "GET",url,false
& d( B% q6 E! ZHttp.send()
  _# D- e! ]! p6 f, b- S' Yif Http.readystate<>4 then 4 p# }: J2 ?; V9 [
exit function
: O( k' P8 F1 eend if + Q/ M4 ]/ o/ ?
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 5 e4 A( J) ]! M6 {8 v/ Q1 n  Q
set http=nothing / k2 s0 i- `& P7 x* X. O
if err.number<>0 then err.Clear
- _& t& E+ W/ v. O! f/ v/ @end function " ^' H" L$ ?/ p7 Y
$ V" {6 [( r9 w: `1 h* Z8 [9 E
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 5 y, q0 V3 _) B9 j! ^
Function BytesToBstr(body,Cset) 1 ~. s2 i8 O- H6 B; _; v
dim objstream
# r  ?$ K& _) \" g" T. p8 nset objstream = Server.CreateObject("adodb.stream")
2 N' p' g, A" c  I* Nobjstream.Type = 1
- f7 `, t- |( h" F3 ]  Dobjstream.Mode =3 # r0 |' d7 ?2 }+ }
objstream.Open
' p9 f/ _& }& u  w7 q( ?. l% xobjstream.Write body 1 _# @1 z) c- w! b
objstream.Position = 0 3 b- u/ w1 J- \2 S
objstream.Type = 2
  [9 b6 ~+ J2 v+ ^objstream.Charset = Cset
& b  s. \0 D1 A3 {BytesToBstr = objstream.ReadText
1 ^, A- q1 ~5 e; |objstream.Close
3 k/ I( `" J. p; Sset objstream = nothing 6 o7 J4 Y9 O& n, c5 r4 u. d$ ~4 B" ~
End Function $ i; d1 V  b  y, h# L) h, I
8 f  c, c! J2 ?( P  f/ a: k
) a( Q' ~0 r8 |. i
txtURL=server.MapPath("../index.asp")
3 @2 i* B' g- o% B+ P& t5 A9 z$ `2 S: B! f) b, F9 w* ]1 q+ f
sText = getHTTPPage(txtURL)
& x6 Z1 a/ h" S9 V/ [' {7 E- Z& h5 H
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
* a7 O) s" |! ?filename="../index.htm" + I6 ~3 W4 w  o+ A9 X  ^* @% B- }; k
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 . k* a( ]5 f2 F% j* a
openFile.writeline(sText) 0 {8 h) R% i* M7 D$ \& L; @3 V
Set OpenFile=nothing
2 M0 f1 T+ O& O# S  h/ C
& d& ~# F- ?% z! d%>
& k$ S3 V& U4 {# a3 [<script>
+ |: R' P1 [3 }3 halert("静态网页生成完毕");
& T9 S* r& \0 h6 m& w8 \. i' Shistory.back(); 3 A" S( A8 k& n, \! A7 L
</script>

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