|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14397
- 金币
- 2484
- 威望
- 1647
- 贡献
- 1432
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. % V! ?8 C2 N0 n% ^8 Z; ]9 R
像www.aspid.cn的主站就采用了TSYS生成html文件! . M% M+ n4 O8 ^. j- I3 L
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
4 V' O# h- e+ c6 b0 R7 s6 N
, ?+ M! z. A2 ^$ j1 S6 R9 R1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% % |) A8 h4 {# e1 p3 _. T& y7 m
filename="test.htm" * J3 O; K4 ]2 r+ T( z# w; R
if request("body")<>"" then ' p( ^; B% b J: U
set fso = Server.CreateObject("Scripting.FileSystemObject")
) z8 M j( c' L K% M/ @set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 3 a# X E; {7 X& U$ U. Q. u$ w
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
( _# O9 _! `; c) t3 ^& M& D( Ahtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" + I6 U9 T1 d6 ~; }5 W- Q) L
htmlwrite.close O. k) I; l" D
set fout=nothing
+ R- j. A/ }/ x2 A( bset fso=nothing : ?; q7 G# C$ R
end if ; P+ T) F- B' c5 |2 {: J
%> . a' V' S; D4 \+ Y1 k; h* V
<form name="form" method="post" action=""> 2 V$ ~- x( P9 Y2 i$ t& k! b7 p
<input name="title" value="Title" size=26>
1 q( K+ b2 J9 [( @% M$ h% Q<br>
' c6 ~9 a6 A+ S" u3 V; D: I<textarea name="body">Body</textarea> , \ s! ` ^1 w
<br> , t0 _3 [! q+ ^* ~# P- a
<br>
8 K1 V9 h, n' C<input type="submit" name="Submit" value="生成html"> 5 D7 t- l: {& _
</form>
0 Z, y! U1 A' M) i2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. L8 P7 `1 c+ }, I5 ?. k
template.htm ' //模板文件 <html>
* X: G" A# K* ?& `0 E<head>
8 \/ N/ O$ P% M<title>$title$ by aspid.cn</title>
' N8 u% M+ [5 y7 b2 N# C3 h* }</head>
/ b' D& ~* m9 N: @' Q<body> 1 ^4 _8 K% R6 z+ D
$body$
+ W7 m( Z9 X, P2 P* ~! Q: Y4 J</body> 1 [+ S. b! W7 i3 i
</html> ? ; N8 w* C9 F; T; ]3 d# v: n
, J# x- Y3 [( YTestTemplate.asp '// 生成Html <% / f+ E* f* U% D% D# s
Dim fso,htmlwrite % K& _" i4 U; j8 E
Dim strTitle,strContent,strOut 6 i0 n6 V7 p* }% i( I3 Y& [
'// 创建文件系统对象 , ]. a' H/ o+ z% i* ?9 C9 r
Set fso=Server.CreateObject("Scripting.FileSystemObject")
5 [% e. E: P- W* W* q'// 打开网页模板文件,读取模板内容
& Q3 ~$ T+ f7 i6 PSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 5 B) X% @2 B2 f4 v- f( s* X
strOut=f.ReadAll / s9 t, j2 E& z' F* d) r9 b( \
htmlwrite.close 2 W8 y7 \% D( o! S4 Q
( B! V! C6 n+ Z$ w) \strTitle="生成的网页标题" 1 |! f2 T2 M" l% L( l) s
strC
& l& S) L- k$ E1 A# r X) k( o9 q* D5 d4 R
'// 用真实内容替换模板中的标记 : N( i& `* Q. j6 o: j
strOut=Replace(strOut,"$title$",strTitle) * `% M6 P6 N, t2 L5 N
strOut=Replace(strOut,"$body$",strContent)
: J+ q4 e3 P% z. M0 U0 w' b6 W. ^/ d _6 y
'// 创建要生成的静态页 , ]8 j5 M: x) a/ h0 z4 K3 _$ s& }
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
* q9 W$ S/ U9 ] v
( Z5 _. R( Q$ A m2 {4 S9 g'// 写入网页内容
9 {0 Q0 C6 p K6 ?; q C1 qhtmlwrite.WriteLine strOut 9 A( h. P" h; l* e) T( K5 s; |
htmlwrite.close
( I8 G( V d& D4 x$ Q
' p1 }) b5 J" s2 DResponse.Write "生成静态页成功!" % P/ Q; |) n* x. q' i
2 `. O5 N/ `8 U8 n& Q'// 释放文件系统对象 0 G& m3 C! W& |
set htmlwrite=Nothing 2 f Y, ?4 ~8 w4 o6 _
set fso=Nothing
3 ?& t8 A1 r8 k7 h ]! P* }%> + J9 s5 ?# F, J. L2 z3 I& b8 F3 ^" Z# a
0 m; i" V% A( G
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ' }( \) q& I5 ^- f! x0 ?
<%
! S6 ^1 P' n$ F" C z" Y/ [* P- `+ d: [. O
'常用函数
4 F. z% `9 b0 Q) A0 l'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
: V1 c6 N* q6 Dfunction getHTTPPage(url) + Z3 x7 F* a' W7 C
dim Http
% `7 \# |1 ^1 q# F3 Pset Http=server.createobject("MSXML2.XMLHTTP") : Y- [/ `5 V' {. J, w9 F
Http.open "GET",url,false
3 P$ s) Y' d0 wHttp.send()
! t. U9 \: k4 X/ h0 }" dif Http.readystate<>4 then 6 x, v2 Y: @6 f- C* P
exit function
2 o9 Z7 f# f3 {7 l9 l9 ~end if , \% w8 _/ B/ T& K
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") $ `3 t/ s( b& U; e: A
set http=nothing 6 k8 p4 \0 [. o$ Y
if err.number<>0 then err.Clear
8 m. V; y- D1 t& zend function * i" |- K4 \- t% o9 d
- |- u/ B# A, b5 B
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 + x6 E: a, I$ w, K: f6 C, i* m
Function BytesToBstr(body,Cset)
1 F) P" t+ X6 [6 l0 Fdim objstream . R& `* d5 y4 R
set objstream = Server.CreateObject("adodb.stream")
7 d7 Q$ G1 O* |: }0 L- {objstream.Type = 1
) A9 N! I) m/ Q& A6 y" sobjstream.Mode =3
6 g2 {+ y4 X2 U s0 b+ r" f2 y2 ?9 kobjstream.Open $ C8 `3 ^$ D% N0 v, g; A1 b
objstream.Write body
) l3 c+ }9 h' ~4 `/ v3 vobjstream.Position = 0 : n) K: V: ~" `0 L
objstream.Type = 2
8 r S- o: k+ y( w* \; R$ ^2 wobjstream.Charset = Cset
! i6 I& ^ t9 d! Q5 @ Z7 \BytesToBstr = objstream.ReadText 4 Y( Y" s$ W/ Q" J) J+ x8 x
objstream.Close
: Z7 Q! g& s6 Q4 yset objstream = nothing . ]: V$ e9 w5 B
End Function
1 S$ }# Y q: A, P3 \. C, s5 x; O5 q \7 r! F9 G/ q
1 o4 I6 p3 V( M6 Z+ [
txtURL=server.MapPath("../index.asp") + r4 | j3 j) s- u7 E
7 }" G2 G8 t" ~: d D5 c0 \3 Z0 IsText = getHTTPPage(txtURL)
7 ^3 _. R4 k9 T! k- _" W; j3 [7 ] R# E7 n0 _3 L: j
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 3 V' a) M( x$ g, E
filename="../index.htm" ' I- d4 W$ t3 z2 }9 n% T3 @* e- m. u
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
$ l# K! E( \/ M! Z6 X! C r# {openFile.writeline(sText)
2 A* x6 _7 z+ `; m1 XSet OpenFile=nothing 1 [( z! n' U2 A* }4 f! U3 [
; w: n6 i& I. U4 M! w! ?5 g
%>
/ V8 w5 Y% z6 C* ~. i3 G<script>
9 G; t% h5 ?- _alert("静态网页生成完毕");
0 U( y. R( @+ o" g: ]history.back();
. _" e1 s7 N4 z2 [</script> |
|