返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 1 }# j$ ]: i% k9 I  z& h5 P- _
www.aspid.cn的主站就采用了TSYS生成html文件! 3 m1 o: h& n0 T6 J6 j5 i' G
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
: y) }8 j0 n& L/ V# q% N) F+ r' P, D% z9 |1 x) J( c' f3 L4 s, x
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
1 z6 k2 e* g& H! H  s1 Hfilename="test.htm"
+ }; R) f+ m  Z5 \0 ?$ G" j8 \if request("body")<>"" then : B% r* ?' Y4 n/ L+ @
set fso = Server.CreateObject("Scripting.FileSystemObject") . c: K# Y$ s+ R9 p1 ~# D
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
0 w+ t- P, n" W4 I2 }htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
' w3 g3 f* Q" \  q& Bhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
9 t! S# f* s' h/ p4 o% \+ n! e5 Vhtmlwrite.close / x3 ^( `' [+ w! `
set fout=nothing , r9 _' }) l" v1 r# J$ F0 M
set fso=nothing 0 f! |& J# b. q* h* u; e, f) m
end if
# z6 I& _& O1 J, W- Y0 L* R%> 8 K* y( L: B# c! f
<form name="form" method="post" action=""> % M$ ~/ y0 T9 I, p3 g  j
<input name="title" value="Title" size=26> ! Z0 F6 Z+ }5 \0 Q6 W& V+ v8 a" }
<br>
" u/ }9 f8 O: `% ?* m5 m<textarea name="body">Body</textarea> 7 Y8 ^( B+ L. M$ w5 h8 F* ?/ c3 ]
<br> % n0 F. s& n1 Y) i4 Q: y
<br>
8 J: t' {: G8 |! f<input type="submit" name="Submit" value="生成html"> ( Z  J6 c; {4 j  \
</form>
- M; L/ T6 E& I: _2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ' S8 a) j' E0 t5 l+ n
template.htm ' //模板文件 <html>   i5 s" Y* A- y) o# b# v" U. f  Y
<head> : O) f$ i; r1 U3 y9 L
<title>$title$ by aspid.cn</title> 8 L9 @+ S5 o% t# K- o8 z$ f
</head>
4 d7 a' d) x& m( X<body> 0 L) K% q5 ~* M( W
$body$
2 W9 L9 C( O: w+ k</body> 3 s( \& A1 A; Z- W6 |! B; L
</html> ?
% l/ o8 l" h) h% c
/ j: \# R$ E! J- o. vTestTemplate.asp '// 生成Html <%
* b+ p! j' I% I) K8 L/ M: ?Dim fso,htmlwrite
# R  {* {# N5 ~" f$ I: XDim strTitle,strContent,strOut 2 ]- J: N3 H# a. d2 P
'// 创建文件系统对象 8 z& P3 X, n" G
Set fso=Server.CreateObject("Scripting.FileSystemObject") + ^0 G4 d6 f5 a
'// 打开网页模板文件,读取模板内容 6 |, L8 j0 [3 h5 i' d/ C
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) . k7 P( }# j  r5 O" W2 R
strOut=f.ReadAll % J9 c- [2 Y: T0 D1 W  U# z
htmlwrite.close
9 x: c5 z( f, J
6 F: L2 v* o: u# N& KstrTitle="生成的网页标题"
1 s$ I9 ~2 D# ]* B8 p$ o( h4 f) |! PstrC : q. R! R6 i. P! O

: y2 R6 H+ \  K) ~( G1 {) m9 z'// 用真实内容替换模板中的标记 7 r+ q- x! K& h2 G/ v2 B! v4 u* U
strOut=Replace(strOut,"$title$",strTitle) $ W4 t6 |- f3 @! ^1 n& X# }
strOut=Replace(strOut,"$body$",strContent) + O- [3 |2 D0 A
% s6 k- F4 v+ w5 J- Q8 S
'// 创建要生成的静态页 ' [- y3 s6 d& G# s8 k$ [
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) ' X+ X7 `0 l( c: }0 X. n9 u+ A
7 F2 J' B1 N6 R! s9 r% e# {
'// 写入网页内容
' A1 P1 q' P, ^htmlwrite.WriteLine strOut
9 ]9 j. G9 z7 C4 q% _htmlwrite.close
' e& {( R3 g6 G7 D7 x* c5 N& ^1 T2 x+ x, H; {
Response.Write "生成静态页成功!"
4 [* Y  F) A" j
! ^6 J4 D2 _2 i! p1 S6 z, q1 p'// 释放文件系统对象 % ]4 N' k+ o5 l, F5 e# Q6 i
set htmlwrite=Nothing % ?5 |7 J1 ]' C% ^
set fso=Nothing 6 V- Z: U8 m6 a  M1 x& S9 R3 V
%>
2 g! g9 j' C# I7 b6 h" ^. W. L* r% F0 X: d9 K) @& _% k& d" Q4 u; ?
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 1 O( _2 B4 ]' v
<% 8 ]: Y& t7 c& v* |- a

) }- [+ e! N& t'常用函数 " q9 J1 ]: n6 o/ g. R
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ; v9 ?% }1 }% o: u% ~! w) p1 h
function getHTTPPage(url) ; Z5 D5 N! S& }
dim Http 1 d* ^  W* L) s8 V( [, D' x
set Http=server.createobject("MSXML2.XMLHTTP")
  ?# a# q7 Y* l. iHttp.open "GET",url,false
3 V! C* E, E, Y6 S$ R- O: A& B5 P6 sHttp.send() 2 s6 `2 D8 W2 ~7 k$ K0 V. m
if Http.readystate<>4 then - a: |0 Y! I% e5 n  |, z+ ~
exit function ' A6 y; X  `: [; D* U" A; X0 r  f
end if : m8 ^( ?. j9 l: P  v" X1 n$ i
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
0 P# v" x1 E2 v$ [) n4 A  f: rset http=nothing ! ]+ o  s- z6 N) T! i
if err.number<>0 then err.Clear + \. G  L2 s, Z7 s" @9 D
end function
! x, I. C+ r. m- \0 ~, C) F+ [# }$ X0 m6 m( a8 L9 F
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
- i3 k, g- D2 RFunction BytesToBstr(body,Cset) 9 S1 T/ G  c- E3 B& ~
dim objstream ' w8 I! v& X; n+ n* J
set objstream = Server.CreateObject("adodb.stream")
3 C' U" H8 R, Y9 v- l" H! E0 {objstream.Type = 1 ( V" d9 D. H9 \
objstream.Mode =3 / o& n! e3 _7 P
objstream.Open
4 @( j9 a+ e' O# T8 R! gobjstream.Write body   j* }0 t9 |# a$ G, R2 K
objstream.Position = 0 : d4 [! T" |# Q# t) [9 }3 K
objstream.Type = 2 . F8 {* V9 l: R, E1 y; k
objstream.Charset = Cset - z) S6 {2 s/ U3 P
BytesToBstr = objstream.ReadText
3 w( @/ W. `4 m4 A3 Hobjstream.Close
; S" l) Q7 \1 G* @  j9 a+ o; }set objstream = nothing
% F& ?6 v( G  ZEnd Function ( X$ u6 z- K( K2 F6 o7 B" x

1 G% g4 D7 m/ a8 L3 c; @; @# f  M6 a; T) t6 J
txtURL=server.MapPath("../index.asp")
! Z% S' y$ d) }; a# Z$ z, t- T: O9 ]" S( L8 E3 R! F, P
sText = getHTTPPage(txtURL) : [- e8 n5 r, d) d

; q( }1 a4 [- K1 R! Q% LSet FileObject=Server.CreateObject("Scripting.FileSystemObject")
* o% D7 z3 t. k" _7 G3 m3 F- Cfilename="../index.htm"
: q% [! Z* E5 J0 V! ISet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 & T# Y7 ^% g* `4 K( A$ U4 X
openFile.writeline(sText) & V' ?0 l( X7 W# R! y
Set OpenFile=nothing % M, m9 f9 b0 v" L" ]; N4 _! t  H. i

" p4 C  d) K! \* `: }%>
: Q+ e4 {$ {9 _! F* Z0 r8 c<script>
; K" t# t0 S, b1 n; `alert("静态网页生成完毕");
# \5 F) Z- {' Z5 ^( U5 |' Z- b4 l: Ghistory.back();
$ g! E" V0 E6 M$ ]/ T</script>

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