获得本站免费赞助空间请点这里
返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
" M: A- u4 H& \( Z- b$ x8 }( awww.aspid.cn的主站就采用了TSYS生成html文件! . d$ p/ V+ g* U, q/ S5 O
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 1 U4 e( e1 K4 e4 }1 |5 Q8 |% d
. \$ C& K  H1 v. p% S; u3 i. |% e) g
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
/ ^8 }& ]7 f( ]3 wfilename="test.htm" * K" L$ p* \' _9 E8 J7 ]
if request("body")<>"" then
0 F: U+ c' S2 M7 Kset fso = Server.CreateObject("Scripting.FileSystemObject")
/ Z" O$ V/ Q* m8 X9 Bset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))   \* l! S2 I0 P
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
! J; l( Y. c' Q4 F8 Q0 C- ^htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
% c* G9 Z  t* H. t, whtmlwrite.close
5 ]1 G4 t1 l# t; @5 _set fout=nothing
% Z% H# [  `- z6 ~$ h; J: U5 aset fso=nothing   b3 M9 d/ ?  y5 \' B2 g& ^
end if
4 N: k) q1 G5 O%> 9 v5 D6 C0 X* S& z6 {
<form name="form" method="post" action="">
% z; j6 y; T6 m<input name="title" value="Title" size=26> ; o) C- u2 e4 k1 \4 C+ r/ w
<br>
0 M8 h/ ?6 z0 n" `& x# F<textarea name="body">Body</textarea>
! z0 V) _: c5 i, ]; [* a- P' q6 t<br>
$ p4 y2 q* u1 Z2 V. l; B) d- S8 l0 q7 l<br>
8 l5 }( b# t: j' K8 X. V* n<input type="submit" name="Submit" value="生成html"> % E( ~  P3 A* h) r! V8 H: n
</form> . j/ r+ ]: k" K' ?3 |
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ; o: |0 u! E$ j4 c( l
template.htm ' //模板文件 <html>
6 B! k8 o# f" {7 x+ M- ~. {3 K. V2 S9 X<head>
$ |) P; ]# l4 h: [2 d. p<title>$title$ by aspid.cn</title> 1 f2 ^" \" R: c7 e% _6 o2 t7 S
</head>
) ~+ u$ V  E4 S' Z# M& J<body>
! E- `* a3 O! C  |, h8 v' [$body$
4 @' g5 S* J* E7 b8 q</body> ; `. N8 }( M  h
</html> ? ; v0 s6 M  V" h8 M6 B5 |

- Q- F% e& M7 I6 e& B7 `TestTemplate.asp '// 生成Html <% 3 f% y0 c0 d4 M
Dim fso,htmlwrite
, s+ g8 l/ ~6 m0 iDim strTitle,strContent,strOut
8 J1 E1 k! U) I* T  ?'// 创建文件系统对象
( @, z2 A) K% o& qSet fso=Server.CreateObject("Scripting.FileSystemObject") . A) U0 U7 Y  B2 h8 J0 s
'// 打开网页模板文件,读取模板内容
$ s; t" y" T! K. A/ j* R3 kSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
; u# h* D$ d% _5 astrOut=f.ReadAll
, ]" h0 @" c+ _/ ihtmlwrite.close
) C- k- R  S) c) F5 {
" I8 H5 Z7 V" J* tstrTitle="生成的网页标题" 7 W8 z/ c+ K, m* h  k5 w" c) ?
strC . C  J' k: V# Y. `! r

# s4 H% U# e% |5 Q: q: Y: H8 v'// 用真实内容替换模板中的标记
# t7 M! ~2 B  N+ S9 P# OstrOut=Replace(strOut,"$title$",strTitle)
3 q- ?- B$ x2 K! Z- L0 ]1 r  @strOut=Replace(strOut,"$body$",strContent) . V3 f+ m* K" v! b
7 }  e. B( i+ _& |4 l. l
'// 创建要生成的静态页
6 v$ V; o$ Y6 T! d, ^$ gSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
* ^5 c5 b, \# [8 f6 v! j* l  R# W1 _6 d' W
'// 写入网页内容
0 u4 ?, t, n+ x  B1 G- H) zhtmlwrite.WriteLine strOut
# b) D: g1 O+ F3 A9 {& U4 U) b. D& e8 ~htmlwrite.close
. r' j' A# A) t: h5 `3 q. x4 l( w0 L/ S5 J! l4 j0 H; M
Response.Write "生成静态页成功!"
9 _0 _/ P* L, S9 Z8 l- P0 s
% ]+ I. V' g0 w  O; y7 |'// 释放文件系统对象 + A  r4 v- b) E) G* Z/ w& h8 ~* A
set htmlwrite=Nothing ' b4 V- f) v% [' D2 _
set fso=Nothing
, V; [2 t3 }% c" O% K/ s# B- {/ W%> 9 I# g; O6 n( {

. W* N4 A; l9 ?0 [" `9 w& M3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. - @, l9 |3 M/ L
<%
; r9 @9 q1 W6 A$ H! [2 ~! Y8 t; R
: g$ K4 w. [6 l7 d/ A'常用函数
! v* ]' S1 x; A, @+ C'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
! z6 b! `* E/ U9 J* ~, p+ X) ?function getHTTPPage(url)
7 @" \" p; b6 c1 F; \dim Http , W1 y' N8 y7 B4 S5 g& I3 |
set Http=server.createobject("MSXML2.XMLHTTP") + T# y) K* z7 l' I
Http.open "GET",url,false 5 h2 G8 e' m; O9 u3 A/ U# n
Http.send() 5 U# ?# T! m; V. E
if Http.readystate<>4 then
. Z. V0 t* x" B; b$ v/ Sexit function 6 r! k1 C7 q6 N9 W
end if * L6 U2 z/ O" X3 _% N- E
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 1 N) |* P" S  U' H0 `' N
set http=nothing - s9 I& P3 D% p% J7 ^! G0 r
if err.number<>0 then err.Clear
$ C9 J9 x$ c" `$ c7 P0 E; f' Nend function
5 h# U& U8 f( q8 v
  q: e% Q, K+ p( u' Y'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
3 ?! l9 i+ z$ V6 o5 y6 \1 m8 d7 KFunction BytesToBstr(body,Cset)
7 m. c4 P6 p3 h- P, b( pdim objstream
# _/ M1 R. b7 `: ~8 i" ]! oset objstream = Server.CreateObject("adodb.stream") 4 X+ ^' N0 [* L4 U" w7 U* A) t
objstream.Type = 1 2 o& u" l: a7 B5 k  k( f
objstream.Mode =3 ) K" h9 J4 _  p) D: y
objstream.Open + L) p& b: D& o- `9 j- m* I
objstream.Write body
$ n8 q" m; s6 Q% F0 x; ?objstream.Position = 0
: f. P% U; K% I: D/ F" T; F  j7 yobjstream.Type = 2 5 U: O& i3 z" L) H3 V" E2 d
objstream.Charset = Cset
# K2 ]3 c! _) J; W2 \! p% S, D% UBytesToBstr = objstream.ReadText   Y9 j0 J( k; B) r5 _7 v  @
objstream.Close ) ~6 u* p/ |( K5 ]  q
set objstream = nothing
$ U- b) M% I3 Q7 r9 yEnd Function
/ y5 l1 I$ `. T" n3 S+ ], x3 y8 W6 Z+ z4 D
4 S# c4 g- G6 g5 w
txtURL=server.MapPath("../index.asp")
4 I8 Y3 F& X  M/ S* T* _( X# M" z: L- c. E1 J( g
sText = getHTTPPage(txtURL)
1 {8 _* I+ w  F- F& d
9 j% R- u* G2 L9 f2 ~Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 8 S7 w  S" r/ X' b+ |3 }
filename="../index.htm" # z: x$ H. Y! u, u7 I  L4 N$ n
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 + ]  K+ f1 e% q( [
openFile.writeline(sText)
: Q+ F8 S- @1 U) r7 `Set OpenFile=nothing 4 Q+ @7 @! I! T& P- w
3 n' u4 C. Q! H: ]: l
%>
. g' a. W2 w; @4 T/ |% \<script> ) j* f1 s4 L# [
alert("静态网页生成完毕"); . a6 u3 S0 J2 P$ K% X. [
history.back(); $ K8 k& @3 Z. G9 ]$ R' n# N
</script>

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