返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
; ]5 c5 M5 `# j9 vwww.aspid.cn的主站就采用了TSYS生成html文件! - s/ N( G3 U! V& l
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. : C4 B# L' d( D% V0 b, I
! H; s5 x# W6 d1 c9 l9 F8 O
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% & f$ g+ V& U; R9 U
filename="test.htm"
8 Z; S; r' M7 J7 x- N; mif request("body")<>"" then
% [7 k8 }/ U  |/ \! H+ iset fso = Server.CreateObject("Scripting.FileSystemObject") ' k2 ^- z# D; l5 ?$ R, ^2 w  V& n- Z
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ) B8 y2 E2 B; l4 z% R
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
4 v4 c# e5 W, w" T7 h9 \htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" : d# X3 P" b, n7 Q. |
htmlwrite.close
! N2 y6 \/ w7 |set fout=nothing 4 p+ p# W( _+ _
set fso=nothing % n. u" H( U( ~; R% d
end if
5 Y' j( X, I% y7 R& _! v" Q- A%>
5 Q( F5 p! V2 d$ b2 w3 b5 L<form name="form" method="post" action=""> 2 I: y$ ^9 ^; L8 a  ^
<input name="title" value="Title" size=26> 1 E6 Q1 h: a/ f: v- ~
<br> ( k' O( c* d- S8 y
<textarea name="body">Body</textarea>
9 V" v$ \9 ~$ t) Y9 A<br> 5 m) q% t% H9 k% _
<br> . g2 i5 `- M7 N8 d" c' V
<input type="submit" name="Submit" value="生成html">
" T8 ?5 u$ d) F* ~3 J</form>
3 t9 j; [" O( t0 Y2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. / d; j7 q  N+ c, F. G
template.htm ' //模板文件 <html> ) u2 M- V: V9 z( [8 o
<head> * ]5 z1 X( q8 b* Q5 {1 c
<title>$title$ by aspid.cn</title> $ W/ q% \8 C) y/ ]2 `- D7 u
</head> 3 k0 o) @$ P' n" [8 d+ b/ `
<body>
0 ^5 y9 Q; d4 c" y$body$
& J: V" [  N* t</body>
/ }+ b% e6 L) ~7 [, p9 ]- ^</html> ?
- K/ a2 P* {1 B: i9 q% y' l) v5 a
% ^& p% @$ B2 z2 xTestTemplate.asp '// 生成Html <%
6 \# D; R" ?( F4 f" [Dim fso,htmlwrite * Y7 `8 r8 H2 d1 o* u3 t) Q( y
Dim strTitle,strContent,strOut
# w  E3 n: z) x/ f, ~( x1 {'// 创建文件系统对象 4 [- ]! y* Q" s* x8 Z3 l& @
Set fso=Server.CreateObject("Scripting.FileSystemObject")
: {9 I4 ^( t7 {, `7 Z& A'// 打开网页模板文件,读取模板内容
3 f. s( d8 h, m0 ]+ O0 K/ N# PSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
( w$ R8 F% m5 J8 ?strOut=f.ReadAll - b- ~$ Z9 b; b5 m# @! c
htmlwrite.close 5 j0 f. h* L! l4 a# L" c( ^

" F6 c+ O  a2 l) z4 NstrTitle="生成的网页标题"
) _' P8 e8 _" s  f( JstrC
! d. I1 P1 [: y& D# r  M1 R% i3 S/ C: o: x7 ?/ \$ M! {1 p7 N
'// 用真实内容替换模板中的标记 ) p( z- N5 w" V9 a: u# a- ^
strOut=Replace(strOut,"$title$",strTitle)
1 v3 ?+ X" \' _- a6 ?0 sstrOut=Replace(strOut,"$body$",strContent) 5 Z2 k$ U7 z, e  P1 h

- d: K0 b- C& s8 I'// 创建要生成的静态页 9 R; C' a! W8 L$ F. u% g0 Y$ ^
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 4 G- a- j% D9 {

7 N3 m1 K, ]# w0 M6 C'// 写入网页内容 " m! A# a$ F* C/ `- h! @
htmlwrite.WriteLine strOut . y, ~2 D9 X0 F  |
htmlwrite.close " B- \/ F! m" d. l- S: I
; a# v4 I( |  S6 G
Response.Write "生成静态页成功!" , e( d1 t, `; a6 T: C

* a  \! t9 I; T3 F/ V1 `'// 释放文件系统对象
7 [5 j! L! T: u, T; A; Pset htmlwrite=Nothing
' y2 [$ X# }# v  |: @0 |8 eset fso=Nothing 8 k; B' w, i$ g4 c
%>
! R5 ]" W* r0 v* a) ~  P9 b
- r( C: L6 d6 K& n* H3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. # ?' h! `- |( L/ f+ x' T
<% # T7 I& ?. u) N  R( e$ [
8 |' S+ I( o. K2 ^: T; L* N. ~
'常用函数 " X0 }0 ~5 e) ]$ r" v
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
" I+ r& h/ I! `8 Z! ofunction getHTTPPage(url)
, j6 r/ G( d/ _% n3 ?) ]5 J! t" b9 f' zdim Http " g( Y2 s1 a* V
set Http=server.createobject("MSXML2.XMLHTTP") ' m; `/ r0 y: a+ F. s! E! l$ n
Http.open "GET",url,false 6 A  B! j/ w; L5 L  w; W$ ]2 j" g
Http.send()
0 S$ G5 Z9 k6 o& F9 h# x$ \if Http.readystate<>4 then 5 P$ W9 I9 [- b9 n! c- S
exit function 5 c0 r6 U, A6 N: N3 P
end if # @0 j/ F  {" B) e
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
3 N$ G+ ]+ Q+ i/ e, \+ [: {8 @  M3 Nset http=nothing . _5 O& B% ^0 P
if err.number<>0 then err.Clear 9 Z: }' e# p, {+ Y
end function
* D. D5 v# H( `+ q6 r; N0 V% e% w$ @  B# j* G8 O' W) z
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 / l1 t% e! \# S
Function BytesToBstr(body,Cset)
" `# H* W( j( U! h5 \5 n# v% Rdim objstream
  Z8 f' d- l  q0 p( \% J% @set objstream = Server.CreateObject("adodb.stream")   [+ v+ q9 m: J
objstream.Type = 1 + t1 M/ z, ~2 ]; F
objstream.Mode =3
* V; }0 O# `0 u& a4 Y8 ?& g$ qobjstream.Open 0 {  }% z) v: Z) G2 M
objstream.Write body ) {& ], k8 }+ y7 T$ X2 |
objstream.Position = 0 ' G( q/ r$ d3 v# [6 x2 r
objstream.Type = 2 5 G& O# X0 y1 H
objstream.Charset = Cset # Y' B7 ?" D- J0 T2 s
BytesToBstr = objstream.ReadText
$ v: K8 r- v- q$ R; iobjstream.Close 0 b( j1 \% t- F' B
set objstream = nothing
7 G" e& `  M; Z) g# eEnd Function
1 v8 p$ S7 P  X: r) h( ]2 I8 k7 ~: b' q
7 R, l6 ?8 y; E* s# Q/ y5 Z6 |/ Y6 Z6 S/ [
txtURL=server.MapPath("../index.asp")
( Z) U1 y! o: k* F7 a! M/ K) H
; C+ X2 T8 [% EsText = getHTTPPage(txtURL) 0 z7 U& l  M8 }, N
. Z1 H! p$ j1 b( u# u0 `
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
. K; h$ M5 b# y9 Vfilename="../index.htm"
6 j2 {& |8 h( n: v) SSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
$ O8 H. G, F1 i# EopenFile.writeline(sText)
6 i; y9 |. J3 i$ u/ x$ H$ QSet OpenFile=nothing % q7 w" A1 P9 l. ~  E

' }  o9 j" {9 {5 i  P%>
1 ^; x5 x) D% V  F3 [5 J" F<script> 3 I9 ~3 A" x. ^$ H( ^6 W
alert("静态网页生成完毕"); # u+ K1 A# Z+ c/ s( V/ l; q
history.back();
- b  v% A  {. M6 B& q</script>

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