返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. / x  {( t, n8 t& _2 O) I" G1 l
www.aspid.cn的主站就采用了TSYS生成html文件! 6 k  M0 u3 s( S7 Q
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
; z3 S% z! {9 H; B8 w& Z
! l9 R1 W* s4 t1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% + w) R( }# v7 c! k( w4 L3 T! n
filename="test.htm"
6 }1 S4 D# I* f- ?# y* f1 z2 Wif request("body")<>"" then
) w/ z9 o1 w( w, ?' i- N( bset fso = Server.CreateObject("Scripting.FileSystemObject")
6 g5 o  o# x% L( lset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) % y$ V: B- B$ ?/ D7 O8 B
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
: K" e- S+ S2 T2 _+ ihtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
) g* S& W: O2 e+ {4 w, ]3 [htmlwrite.close
+ n' b& O+ O7 t; z2 G8 bset fout=nothing 8 o) y! O+ n5 r
set fso=nothing
- \: ]) o2 e9 s$ n" J: I6 oend if # b0 y5 [7 g1 Y+ D
%> " ~( }( l$ M1 N! V5 T- H
<form name="form" method="post" action=""> / q! R3 P; m9 k& D2 S* ^- Q8 C
<input name="title" value="Title" size=26>
9 Q8 M: ]8 P; }1 _6 P  d<br> 2 g3 w' g/ ]0 s* M- l6 p
<textarea name="body">Body</textarea>
. `2 C9 }  a+ L# A<br> ' K5 \8 A) n7 Z; G4 i
<br> . p' @0 O* x% r$ |8 w4 x
<input type="submit" name="Submit" value="生成html">
& B$ K1 H! N# W9 R& s& e, l+ P</form>
% W9 b& J" h8 v6 L2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
$ E% {1 {# R' Z7 p, v% S3 l  Stemplate.htm ' //模板文件 <html>
7 W7 W- ^+ Z2 J7 _4 x<head> % ^6 c. J7 X. u9 X  ]2 G
<title>$title$ by aspid.cn</title>
3 d; }- {$ e6 z4 v8 E/ \</head>
' V9 `8 C% X& p  S8 c<body> 7 G; C6 w) _) V0 ]) G. u
$body$ 1 a: Q6 z# Z% \$ h5 s# p9 |7 Q! |$ n
</body>
* T4 {# ?( i6 J2 b  V</html> ? ( o4 e% L- b" H. c/ X8 E0 e
  e4 D) i! g% |% _4 \: H& l0 A) ?
TestTemplate.asp '// 生成Html <% 7 k$ C5 F0 y* {
Dim fso,htmlwrite
" |& Z7 x" r" R9 cDim strTitle,strContent,strOut
- @3 w$ j$ m7 r7 T- ^3 [' k2 c'// 创建文件系统对象
7 v7 {1 I& @( }Set fso=Server.CreateObject("Scripting.FileSystemObject") 1 _5 C* g5 x$ h
'// 打开网页模板文件,读取模板内容
; b3 V2 H. ~/ G, [0 TSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 2 k9 \( z  A% g6 Z/ F0 e! q: x
strOut=f.ReadAll
% c7 _2 ?9 j, v0 p3 n, Vhtmlwrite.close
  v8 R: V8 S" E
4 t% o0 _  j/ t, r/ [strTitle="生成的网页标题" ) V) [7 `: j- h1 v
strC
4 {; n+ X  e$ F8 l% O
0 L3 s  N+ J$ a. d+ c) K1 s- j. g'// 用真实内容替换模板中的标记   L9 t/ y7 [) F8 o1 J$ j
strOut=Replace(strOut,"$title$",strTitle)
5 r+ M& E. C* R1 @% E$ nstrOut=Replace(strOut,"$body$",strContent)
6 h8 f7 P! d) k9 f6 w% u5 y
% |8 y7 l& `# o+ z$ B'// 创建要生成的静态页 2 L$ f$ p$ {/ `  I
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) . n& i% F4 E  T- ?6 x

& g# t. [9 u' [* Q/ b; O'// 写入网页内容   [8 O! @6 K8 L3 B- ^9 n* W
htmlwrite.WriteLine strOut 3 j) |. q* i: \( d6 M
htmlwrite.close * m/ N% s, }  n+ S
  |3 u1 C3 \1 S. X; l
Response.Write "生成静态页成功!" 0 \1 S, _5 j: q: d( j

( H- H; ]6 Z/ Y8 q'// 释放文件系统对象
; d* |; L1 d0 J8 kset htmlwrite=Nothing
1 ]7 O# x6 E2 ?' F+ ^2 pset fso=Nothing
. A9 X* u4 Y1 s3 q$ |1 Y%>
5 K. M4 e: r; s; i/ X7 S  C
- ~+ s  }  ]* @7 `' d& n: L3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
" Z- K3 O& Q2 i<% 7 R  @% s' T# S7 @3 ?+ P
5 V5 i' y7 Q7 u% h
'常用函数
. i0 v- ?( z$ {( K! |- W  c'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
2 l1 m# y/ [5 w  Q4 X  j+ V/ _function getHTTPPage(url) , G" L; O: @8 O1 ^
dim Http
. T$ f, E( O) ~5 J. @& \9 Wset Http=server.createobject("MSXML2.XMLHTTP") ( J* ^: ?: [3 G% l% [) H$ S
Http.open "GET",url,false & X' \4 t9 S. q& d
Http.send() - \* W" P  p. q  J3 N1 L" m, F
if Http.readystate<>4 then 0 Y4 h& z/ ^. v: m7 A3 A+ F
exit function
( I3 k- D1 e* I  w1 c+ E0 I7 V6 kend if 0 n/ F( o! j% l; X: q0 S
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 3 t1 O) E% Q$ b* Y6 g: n
set http=nothing 3 w0 x! o# g' y; V* I. Z
if err.number<>0 then err.Clear
; g1 H: i# D: Hend function   B4 P7 O' q: j: A
5 }! F7 G$ |" B& P: G; ]3 `- y
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
" n% z) }  Q: r" i. r5 GFunction BytesToBstr(body,Cset)
/ G# W6 [3 V% [dim objstream
8 D1 G4 }5 }7 C& ?* y. t" U) @( Oset objstream = Server.CreateObject("adodb.stream")
9 V& v2 X( d' k0 }/ d7 ]objstream.Type = 1 8 U0 `$ i8 M: L, X
objstream.Mode =3
9 V) }9 \' H# R6 K/ Cobjstream.Open
9 C1 q+ G3 m( b1 U" ]objstream.Write body 8 }* O' E5 x  ?5 ?5 z' v  [4 v& b; f
objstream.Position = 0 ) w5 _/ U* Z: M
objstream.Type = 2 " `; C3 l: x" N0 N+ Y: [
objstream.Charset = Cset 8 I0 a# U3 F! }2 Z
BytesToBstr = objstream.ReadText 9 ]6 q! n: T: Q, ]7 n: F
objstream.Close . w# P& T8 i0 ?+ u+ C2 a; w
set objstream = nothing + P/ X# S2 a/ e5 I1 @2 Z+ I
End Function
6 b& l- L. j, s" b$ F( ?- o# _- `; H8 x' Y/ a7 L

3 N: O/ g4 b# M* }+ D: otxtURL=server.MapPath("../index.asp")
4 ~4 J& {6 f4 t: k( Z8 y; C; ~4 c; C) Z! p& f6 @" G
sText = getHTTPPage(txtURL) 1 q. D( r8 e7 [/ e+ _( \; o

4 {# t: `  t; n8 uSet FileObject=Server.CreateObject("Scripting.FileSystemObject") 3 q) ~  t: c0 q+ f" Q) \3 N
filename="../index.htm" 3 ]' D( }# A3 w0 o
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 % t0 W6 i+ h1 b# u- v& j+ }0 e
openFile.writeline(sText) 5 K9 c7 G/ V9 y1 k& J# @  r
Set OpenFile=nothing * d/ |7 E1 {4 A
: w" }5 b4 [4 i& _
%>
6 d8 u& n4 _5 V: j% K0 Z& V0 k<script>
2 o8 T! o8 R5 }$ H; ~& {1 G) Ealert("静态网页生成完毕");
, r2 h# ]; y5 m: R0 l" U: _history.back();   @8 S; a, Z5 I3 t
</script>

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