返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
/ z2 H6 a+ C& n  Nwww.aspid.cn的主站就采用了TSYS生成html文件!
  l- R9 n! ]" f+ ~6 B- }所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. ( [- T- O8 j& ?, a" Z) H

( ^) ?/ |  L; S% ?# B1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 7 {4 ]$ q4 C9 `0 L  U
filename="test.htm" ( \  q" y1 Q* S. E4 j
if request("body")<>"" then
- K0 [  I- @* _" B" `# K) G4 F7 hset fso = Server.CreateObject("Scripting.FileSystemObject")
" q9 U$ l" V4 Q8 P) X; Aset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) - W$ r3 i0 p9 H
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 7 o/ H3 n, N- W, j& x' a
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ; K/ H: X2 }% [
htmlwrite.close
; u5 K# P* o( U* l0 f( V  |/ |3 iset fout=nothing
/ F- L! Y6 J, Y* Qset fso=nothing
0 f- L, U, E! v& [0 F& ?; Pend if
' _" J5 L. Z* c/ c" Z3 l/ V%>
4 V5 s4 r( M1 O8 Y<form name="form" method="post" action="">
( j, L0 ?* p- O& e6 ^) e; Z5 y, z% p<input name="title" value="Title" size=26>   }; P0 h0 s$ d5 ]
<br>
+ p) g. F9 l$ e5 [' R; Z. c<textarea name="body">Body</textarea> 8 d2 I' d0 L2 M2 v, B) b6 [
<br> / Z% f4 `8 h) J6 ?
<br> # v" Z" l- D* z! U0 U5 x
<input type="submit" name="Submit" value="生成html">
; d, s9 W0 W2 ~1 X- R</form> & P+ w/ Q* b7 ]6 g
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ' ?* Z. ~) I6 B! {& v! H
template.htm ' //模板文件 <html>
2 ^2 k' S+ l+ Z; u<head>
9 y% t3 L! o9 i( `2 Z+ j6 h7 C<title>$title$ by aspid.cn</title>
; Z; ?! F! ^" v8 y9 t/ D& M$ z</head>
- U6 k& J7 n! l  F2 v<body> ( s" Y6 B. H# j/ _
$body$ + t, x4 l: S$ L) [; y
</body>
6 m9 Q$ N+ u4 i8 t& i; i</html> ? ; c4 n# I! |3 w$ s6 p
& d/ U1 S2 ~6 `$ V
TestTemplate.asp '// 生成Html <%
7 z3 @: T( T5 E  JDim fso,htmlwrite + p3 A8 F4 t3 O* t- X/ k+ L
Dim strTitle,strContent,strOut
, g4 o% G) v' N. a, D'// 创建文件系统对象 " X) C6 u% ^( u8 w  [2 x
Set fso=Server.CreateObject("Scripting.FileSystemObject")
5 u2 v9 w: h9 [4 Q'// 打开网页模板文件,读取模板内容 * |- ^7 k$ ^/ q; x6 Q( L8 T4 T; K
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 4 g8 s- F) H; Y2 w- N
strOut=f.ReadAll
9 G( n" F: J: X0 ghtmlwrite.close 7 ^7 j- j% y5 a8 A3 @

: u# }1 H0 q6 `* {strTitle="生成的网页标题"
, x' w4 x. d. Z1 P# G- O& U! ystrC # g( s1 B% d) ^$ c

# S8 I+ s+ f) C+ g'// 用真实内容替换模板中的标记
& y: d3 }3 h9 ~& x( T  bstrOut=Replace(strOut,"$title$",strTitle)
' p6 f% R- V/ n2 cstrOut=Replace(strOut,"$body$",strContent)
! v8 i8 s2 n1 q1 j% J( @2 A5 `
'// 创建要生成的静态页
! C9 ~) I0 ~- o# O) f# BSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
) t8 _1 q3 p6 X' G+ R  j0 t) ~, n# S- h" j2 Z6 C9 u
'// 写入网页内容 2 [1 U* i+ v0 q. @  g
htmlwrite.WriteLine strOut
( y. Q9 E+ h' rhtmlwrite.close
2 D! ^2 @3 U, s  [1 c* d+ [$ H
  ]3 i% j0 p7 Q4 dResponse.Write "生成静态页成功!" 9 f: X; S- m- m- l0 a9 \1 \
8 p# i/ Y; J! f# U
'// 释放文件系统对象 6 z$ E' [$ C2 h
set htmlwrite=Nothing ) v5 r+ V1 U, D
set fso=Nothing ' d- g9 S/ L- a& b: ~( j7 h
%> 6 O- O# P3 W% e% A0 z, u

) `" g9 Y" a/ i3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
  z% w/ f7 P* {2 d5 }  w* C3 R7 L<% * D# m4 Y6 R  e; q# Y* u

* ]) Q4 M/ e, o2 g9 F4 v; h'常用函数 ' Q5 S( q' L' X
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 2 `$ E( d/ i7 C3 Q
function getHTTPPage(url) + v' r3 `" o+ U  y: N6 w' N
dim Http
" h2 R: {9 J% \" N0 rset Http=server.createobject("MSXML2.XMLHTTP")
& ~! ^0 f3 Z$ A- J; M. [8 oHttp.open "GET",url,false
9 }$ S5 p/ j8 cHttp.send() 0 u& s: D) p4 f% U6 B
if Http.readystate<>4 then 4 l% b0 j' k( y' a1 o
exit function
) e, w: `& w) dend if 4 {8 i5 w; ?+ ?  m; H- L$ ^- N
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 0 h1 n- }, f1 l% U% @
set http=nothing
2 ^! I% s' `  l% d8 x4 O, x5 D/ Zif err.number<>0 then err.Clear 8 N" b4 @. ]; Y+ s; o* Q
end function
$ V5 y- X0 Q1 o1 A9 ~. U3 r- }  H5 `! U- G6 C
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 5 X) ?6 V) a& X+ ]  U0 \' k
Function BytesToBstr(body,Cset) 1 C# H5 k9 k: G( ^" n6 P9 |8 k) D
dim objstream 6 N  A0 D: C/ M7 o( K
set objstream = Server.CreateObject("adodb.stream")
; g* |- l% C7 e5 `. Cobjstream.Type = 1
% O: k! F' }4 c" ^) Z' ?7 kobjstream.Mode =3 ' R: D6 a# [0 P1 b7 K
objstream.Open - U% @  O% t5 \6 ]8 l8 P
objstream.Write body ) v; g' m! k" Y: f, x: v5 q
objstream.Position = 0
- a" X0 f! [! ]4 A" R, n! Hobjstream.Type = 2
! ~' A( @+ W/ Y6 }objstream.Charset = Cset
3 A7 c, o; f. d& t$ }/ J& KBytesToBstr = objstream.ReadText 0 |" K, U  {" u' G6 W& K! K
objstream.Close
8 i+ q8 H0 s/ v6 ~set objstream = nothing 1 }8 [3 u+ F7 L  u
End Function 8 Q; y" w* ]+ U, W# U3 {

8 F9 D6 Z+ V( ^/ z* K$ |# W- F4 I. ~+ E. q- Z& _, S1 E
txtURL=server.MapPath("../index.asp")
& k4 j9 D$ T* ?! q1 v
+ G: ^( j$ V1 IsText = getHTTPPage(txtURL)
9 z( `' Y2 ^; b! }( l, p
9 y5 z2 {6 f4 O3 k( TSet FileObject=Server.CreateObject("Scripting.FileSystemObject") " f* e8 F( S: X  p" H
filename="../index.htm"
9 O* Q+ t, a+ o  y# e* _Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
6 S3 S# S+ C" J" x7 [openFile.writeline(sText)
# o. h+ `0 g6 A( [9 Y2 u/ cSet OpenFile=nothing
1 S2 k7 f) u; E6 K# n! X& X& Z- y' M$ @: b$ d0 {# J
%>
* [9 b; ?! [' T% a: v5 P% t<script> ' O( c3 o, j3 e% O9 x/ }8 U* g
alert("静态网页生成完毕");
& }! {! q: W3 Q( h' {5 Ahistory.back(); 8 U9 ]& K; e% ]# Y% K$ D8 P
</script>

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