返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
" T0 y' d- E+ c' L! Iwww.aspid.cn的主站就采用了TSYS生成html文件! ( u  v. a. S4 z, a  K. K, `1 e0 ~0 u
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
- Y8 J3 K! m$ e9 c" E
! H, G) S$ A6 C2 c4 f! o2 p1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
, ^) n3 J- }6 s9 V" H0 A' O8 |filename="test.htm"
2 f0 D% M& f# C, o& S+ w' Nif request("body")<>"" then 2 }- e( z* m& g& q5 l
set fso = Server.CreateObject("Scripting.FileSystemObject")
  O- A8 }: f; W! Q6 E) s( X5 jset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
- T; L' L( s; {; Z: ^$ @4 Lhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
3 V( X5 U7 D2 B8 a& S, ahtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
+ x" o- [1 B. K$ S3 T" W( xhtmlwrite.close   F/ B+ x5 t, I, q1 ]) i' s
set fout=nothing 5 X) X, `2 }1 S. P! k# S5 q( o! U
set fso=nothing
& z$ N, }8 m' a" v6 f0 lend if
7 g. C7 D: W% T- @* \1 _%>
. b* A; D1 \8 F" O) f<form name="form" method="post" action=""> 6 E0 h  O4 }# V; |- _  F
<input name="title" value="Title" size=26>   ^- i/ x& k6 _" O
<br>
# M5 `9 N* w3 Q* R4 @# C<textarea name="body">Body</textarea>
% Q0 ~) W, _  _( q7 L0 m<br>
: J+ I; s  R# `! A) N# s8 }<br>
$ m5 ]" y  f& ?<input type="submit" name="Submit" value="生成html"> 4 E! Q% q- ]2 O1 o9 x* B" o
</form>
% t1 g0 v) I8 x4 g6 V/ E6 r1 Y% |2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
# M* H7 ^) e( O& b9 G; ?template.htm ' //模板文件 <html> , B! B4 M9 D6 i  q
<head> # h/ m9 C- e, K& o" o: g: Q: u
<title>$title$ by aspid.cn</title>
# D- B. [3 F: q' T" x  G7 B' A% ^' y</head> 7 k2 ]- K+ \+ Q5 H/ V
<body> & L8 L, ?" H/ a9 |0 l
$body$
% s% n8 x* G6 e/ ~* i+ u</body> " q% v( G  X5 H; {/ g
</html> ?
- h4 ~" H% b1 q  P$ v- L. |! F
' H8 ^8 r/ A. j4 ]TestTemplate.asp '// 生成Html <% & d* r: R: y0 o/ j. N4 E, K6 S
Dim fso,htmlwrite * }  T, o8 a! S' Y
Dim strTitle,strContent,strOut
) j% B% {' ]# n; Z3 B/ A1 ['// 创建文件系统对象
# h$ v7 n! \9 eSet fso=Server.CreateObject("Scripting.FileSystemObject") & p/ R6 ^* r7 R* n5 P( F
'// 打开网页模板文件,读取模板内容
" @- {/ Y1 m; {Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 7 j) D' ?( J0 Q7 |; C
strOut=f.ReadAll
. a" N* t0 v3 T( h- ahtmlwrite.close , n+ d' ~) {/ R$ C% s" M9 p
2 n& v8 H* n' r# V- W% D
strTitle="生成的网页标题" ) n& ^; k5 W$ [
strC
" g+ ^# _% R  H7 Y
. Q7 c8 c1 c# R; R, {! Z) N% c'// 用真实内容替换模板中的标记
2 u+ v5 A, z7 u9 P$ e# zstrOut=Replace(strOut,"$title$",strTitle)
- ?% V0 T# l0 s3 V) V* estrOut=Replace(strOut,"$body$",strContent) & S. ^; \9 G) T# W1 y
1 O, }: L" h: n, R4 m6 S/ u
'// 创建要生成的静态页
0 E+ K; ^, |. e  }" D' c7 USet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
3 H1 {  W8 I+ i* l1 N/ j2 D# L) x' T6 l
'// 写入网页内容
7 o% z# Z& j. f0 ahtmlwrite.WriteLine strOut
9 j1 L3 p' L# w: R- _htmlwrite.close
7 Y7 Z8 a, g- i" [* ?
  d  B# c2 w% ]Response.Write "生成静态页成功!" 3 s- F0 e( H: J$ l9 ]" t- S
3 {- P7 d  a4 N5 j6 k+ T
'// 释放文件系统对象 / ]4 T1 y# c, ?" C, n0 q# q
set htmlwrite=Nothing
+ p: W1 n" {; j7 n% I9 {2 }set fso=Nothing ) \1 n9 U  r$ H& X" f- B, e8 [; ]
%>
/ g9 B! c, k" B. r/ E% F1 n- I* O: f2 z
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
( m; I# W5 `8 L, z* W<%
, S9 @" }$ t3 ~1 B2 N7 O! a
3 p5 k% R; ]0 Z0 j2 f2 \'常用函数 / i1 ]& k+ ^+ A( f6 v- t5 c
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 7 K5 ~7 L% M# Y8 |6 q# s
function getHTTPPage(url)
+ I7 f2 f  x8 m2 P3 s' Jdim Http $ x" G6 ~# F: D" R
set Http=server.createobject("MSXML2.XMLHTTP")
6 @2 ^8 T" O* Q' V6 K2 [Http.open "GET",url,false
, D, L5 n7 ^: F7 o  Y# P3 e( ^$ LHttp.send() * P. W& m! d+ C1 I  D
if Http.readystate<>4 then ! y9 g2 Z3 L7 Z1 K
exit function ' C) p+ @5 Y2 a; w
end if
- o6 T! i' m. j4 PgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 3 p( r4 D# X2 i8 }$ ^1 I
set http=nothing
. U% ~. E8 b& xif err.number<>0 then err.Clear 6 i. T: x4 |; ~  t# S8 D& W
end function 9 k8 x9 h0 t: y9 n
, b1 H5 z, V. F* U5 q
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
* L& _, B" P; j; z  DFunction BytesToBstr(body,Cset)   R6 j' g7 p( p! l/ K
dim objstream ! }. T8 j. z4 p9 D* f
set objstream = Server.CreateObject("adodb.stream") : Y5 p% q: v) H* n; `
objstream.Type = 1
4 t) L* C2 q# _+ E( B+ J6 Jobjstream.Mode =3   l0 f5 m& g7 O- m  A9 n
objstream.Open * T' d6 a+ I: J' a; k1 V
objstream.Write body + c6 j8 D4 Z8 T: _
objstream.Position = 0
1 x# X6 `% p- L8 `) ?; h( Robjstream.Type = 2 ; z; c& G1 I5 Y3 m8 P9 ]
objstream.Charset = Cset
( v# x. T& s, a! p! XBytesToBstr = objstream.ReadText
; X9 s+ i# }: q7 D% ?- oobjstream.Close 4 r& N1 E7 X' c  o: Y7 ^1 c. D
set objstream = nothing # a: q: H0 s6 M& z: ~2 A
End Function
6 n" k) R$ z" i. q0 F0 e1 x, |& [9 m" Y1 W; `- U3 K( ^

" B* b$ ]! {3 q3 ^2 g. [txtURL=server.MapPath("../index.asp")
' V- r- ~6 T5 i3 T/ x3 o0 j3 L/ U  T) [  u
sText = getHTTPPage(txtURL)
+ N7 W# B2 t* K. ^9 c" e5 ~, s
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
; Q, ^* K+ N% Z- c1 u7 Wfilename="../index.htm" 6 j# T, r% V3 u" c
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
/ }: o, K" @0 n  s" ?openFile.writeline(sText)
# T3 z( u% j! D# wSet OpenFile=nothing
' _* _1 F% W+ }7 _6 ^- k( h$ E
1 j" Y: e! h: o1 ~, J5 n%>
6 x! Q2 N$ v  r; H2 y, B7 x<script>
: J9 _( H& a  Kalert("静态网页生成完毕");
1 u; N( j; ?1 ?history.back();
" I$ R. x1 ?( m* n0 y. ^</script>

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