返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
, r# I8 ^* h$ s3 O% R" Q$ T; {$ uwww.aspid.cn的主站就采用了TSYS生成html文件!
* U- H( K2 I  ?5 a7 P9 X2 b% z7 q所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
/ O7 r7 Z* Y( p4 W; Y* O+ q6 C4 J  |  m5 o
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
% y# X6 S, I+ h/ A& p  \* E5 M3 L" pfilename="test.htm"
" Q. q" O$ \. ?" Y/ zif request("body")<>"" then ' l% V: {6 j9 y1 n& u
set fso = Server.CreateObject("Scripting.FileSystemObject")
2 }* O' W6 _+ x# nset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) & T: z; q: Q- h
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
( n5 n0 j! x* J' A: nhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
5 F* y- l0 _. q+ w9 s$ L3 g; J0 fhtmlwrite.close
. |( B" `; D9 ?' O: s  uset fout=nothing
3 h6 h3 d2 R1 m1 tset fso=nothing
  c; X" ~  _& b( T5 B4 Lend if
! g% G# G3 j9 @0 `3 b& f3 g& u" ]0 o%> ( q- S$ o8 I6 T0 A
<form name="form" method="post" action=""> 9 u" I/ ], x: _- A& t- u7 }% d
<input name="title" value="Title" size=26> . _) I8 ^  @0 K- E
<br> # g7 V# ?6 J  _# r. `
<textarea name="body">Body</textarea>
/ [( d4 J7 ]& W3 f1 i. }<br> % |  i5 x: ^( G8 Y$ e, K
<br>
9 S% A: T1 |8 w7 P$ w8 N( N<input type="submit" name="Submit" value="生成html">
) H9 Q$ x0 e: ?9 S5 R3 b</form>
! s! d/ l9 l8 Q+ O) p2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. " [; h- |( V2 E5 j% `
template.htm ' //模板文件 <html>
- c1 W# c6 L% W8 u7 @<head>
- `  p- I) A  H<title>$title$ by aspid.cn</title>
. R, Z  }! K; ~5 ]: V</head>
) M& k& v( G9 n: m- Z- {<body>
" H: C+ j+ }' s+ Q$body$ 5 X4 m5 a: E4 f; D5 }! I+ b' M( R
</body> 6 O* Z- R+ \3 z+ d
</html> ?   l5 z$ R6 A7 w4 R9 U  v

3 R' Z/ ]& V8 w" }TestTemplate.asp '// 生成Html <%
: P* }' _% n: c# A9 {( TDim fso,htmlwrite 5 S8 i# U( N7 v! T& R
Dim strTitle,strContent,strOut
4 v$ j% M3 r$ S' k2 f'// 创建文件系统对象
: Q+ P! K$ Y: D/ o; XSet fso=Server.CreateObject("Scripting.FileSystemObject")
5 J, ?+ E# a$ Q! j! ?  A2 I8 C) {/ t'// 打开网页模板文件,读取模板内容
; S2 a" \. f" ^9 E$ [% N4 JSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
# g+ z* U9 f/ v) K& vstrOut=f.ReadAll , [% O# W, F- ~) K
htmlwrite.close 2 n) k3 W3 Y3 C" Z* ~

* Z; h6 Q. o8 l4 `* y  DstrTitle="生成的网页标题" & r. _8 D( {5 Q2 i' V
strC + F' o: E0 `: W# o  b

* {2 l; v' L! ^9 l2 ['// 用真实内容替换模板中的标记
" g( c8 b6 ^3 bstrOut=Replace(strOut,"$title$",strTitle)
) n1 j. P' T; V' qstrOut=Replace(strOut,"$body$",strContent)
) @8 D4 {' a4 Y, Y0 N1 S; p0 W% u0 }4 M- i0 I' P
'// 创建要生成的静态页 " {/ p! A# c2 _7 K( T
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 3 e' L  {9 O" w7 R6 ^

# b& F# r; n, F0 c7 A'// 写入网页内容 # j4 ^+ ?$ ]3 H
htmlwrite.WriteLine strOut * q/ j+ `0 t5 o7 D* W. w
htmlwrite.close 9 b) t+ \  n/ w5 K( f9 F- M3 N) R
) c: s+ e" q* Y# i
Response.Write "生成静态页成功!"
( y: L7 f2 y. R6 E3 U) ]. Z0 q1 G8 P/ J6 q: w1 U; ]* Q
'// 释放文件系统对象 3 t  G9 h# b* R3 o3 e/ D, S
set htmlwrite=Nothing 7 ?* G% y7 |+ K0 m& k
set fso=Nothing , Q3 x% l( e/ ]+ I+ L7 Q) X
%> * f; |0 E" j& Z7 t8 T& d/ `% k

1 g; ]8 t, F8 w% _8 W7 u3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ! v  _6 b. ?+ d( N
<%
$ T4 y$ E$ C. E4 B( \" H7 m+ T8 X0 {2 g, y% U
'常用函数
$ J$ h# _! f+ ^& ?, w0 V'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 * v% {0 D4 p5 S4 |
function getHTTPPage(url) " S5 A( V, S  `
dim Http # H3 }; i, B/ `
set Http=server.createobject("MSXML2.XMLHTTP")
- W2 h7 k6 I9 s) j: ?2 SHttp.open "GET",url,false
6 a, b- x1 n6 K/ I# s4 g0 j9 }Http.send()
1 U- n% D! {: R  x4 I7 d+ Uif Http.readystate<>4 then , n2 o0 _) O2 _' H0 s) X% F
exit function / T( {3 ^4 w$ k8 E6 j; W0 l
end if
) }5 j# q5 t, a- j- E1 _& XgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
! Z6 l- _! b, J8 yset http=nothing
/ b" l. l: _0 H( H& p) t. Aif err.number<>0 then err.Clear
5 M3 ]5 {+ W! j5 {+ a2 n; M- V8 qend function
# j1 j% L1 C2 K3 n, e9 U) }) D3 X; E/ e3 t) V$ N
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 2 q9 Z8 X2 w  `& B( u% \- k
Function BytesToBstr(body,Cset)
. d$ Z+ {9 a- @" Q( Z! \* Ddim objstream
- p9 ]' G- k  |7 j5 vset objstream = Server.CreateObject("adodb.stream")
- w  D9 K* L  A% A+ J+ vobjstream.Type = 1 ! j: Z% w* L8 J  @" ~4 b, F1 b
objstream.Mode =3
. [$ V& K/ W3 r6 ]7 e  W1 o& `objstream.Open
& [& C: |% K3 P# T& hobjstream.Write body 1 N% r0 A2 @+ g* l
objstream.Position = 0 & m) R" F+ h4 v
objstream.Type = 2
  r: F' W# A& R$ S- hobjstream.Charset = Cset ; c3 d& ~7 m& N! M  q
BytesToBstr = objstream.ReadText . u/ w3 w( D4 C" z
objstream.Close
- k# V& [+ G2 K8 Oset objstream = nothing
7 w+ F: e4 ^# TEnd Function . @6 J! Z2 d# b; @  V6 y: }
' i6 f- s: I% V) p" Z, M

- |/ G; i( }# H4 K) g" X8 V' ], \4 ztxtURL=server.MapPath("../index.asp") % w  [% e4 r( R6 C& f
4 G2 m" g; F7 g( L7 a5 q8 P- I; y
sText = getHTTPPage(txtURL) 2 Q- l5 T0 N1 m. T) D+ ^

6 o7 r+ Y* d% o/ C: z, BSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
) g. F1 e+ k5 M" W  ?  Dfilename="../index.htm" 9 x( a9 h9 g( @  R' B" @
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 1 q0 |4 g( \* B( x
openFile.writeline(sText) 0 T) [7 `# j% L
Set OpenFile=nothing ! L/ b/ y. u1 |; r7 E7 Q
! U" \' q( v5 T  {- G* H2 A
%>
7 j& u3 V, G% u, l<script> ! y5 v8 P( A; `3 `0 z
alert("静态网页生成完毕");
1 P, `  h+ D2 g0 W- Z/ X3 ]. [+ Whistory.back(); ! x2 i- S/ S3 j
</script>

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