|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14387
- 金币
- 2479
- 威望
- 1647
- 贡献
- 1427
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
! b" q ^' O' @) J' T像www.aspid.cn的主站就采用了TSYS生成html文件! 2 k1 _( s" Y' p" s
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
. p1 a: K1 F/ d+ V
4 o7 ?. m0 D0 U9 u3 q1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% # p( X# M; t" o! E
filename="test.htm" 7 o1 o8 A" m/ N! O
if request("body")<>"" then
( M. d( I. y- c/ \5 _7 H: q; aset fso = Server.CreateObject("Scripting.FileSystemObject")
# M/ ^! t. l c- Cset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
% h$ L- L# _9 g- z& {( mhtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" / P2 ?8 h. _: X" i7 V2 z/ a
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
8 E3 _, _) J4 ^) R+ e5 x( Whtmlwrite.close
1 |* H# c j0 H% C6 s7 |set fout=nothing
% M- }. {$ ^$ k) ?% bset fso=nothing . \) J g# x: U: P* L
end if / h5 p/ @* L) }: Q2 G
%>
( n8 M g% b) A8 ?" n<form name="form" method="post" action=""> ) ^' z$ j, Y5 ^' f
<input name="title" value="Title" size=26> $ H, y) h* y6 ~3 w
<br> 1 H5 y; _2 K% _# x; s
<textarea name="body">Body</textarea>
7 f& g5 @5 M5 k( q<br> 4 x, @ [' J" r
<br>
# b" h% e6 W4 q# @1 v( g<input type="submit" name="Submit" value="生成html">
! i r8 f! e Q) T5 O% m5 A</form>
0 a9 D P* C" H1 o1 @2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
' ^0 |( }5 |/ ytemplate.htm ' //模板文件 <html>
4 ~: @6 V' @! D) {) Y! _$ v! F<head> ! |- A3 l2 {& {
<title>$title$ by aspid.cn</title> : w* B- R9 M9 s4 m1 X( j
</head>
( b4 f) P& o) s0 }+ ^+ f<body>
4 U1 d3 l5 ?* t! ?$body$ $ E5 Q" [. z; v
</body>
7 R6 j0 Y: c V, ?) M) Q</html> ?
" s7 f# O1 v% k0 O5 z/ A5 t. k
5 V" g2 `5 O4 s5 R( e! j* nTestTemplate.asp '// 生成Html <%
4 f' R9 q2 Z$ v9 vDim fso,htmlwrite
8 m/ `9 Q9 G/ g# N0 [4 oDim strTitle,strContent,strOut
; o$ Z9 e7 ^) c7 O. J3 m3 C'// 创建文件系统对象
' G% |9 s: S+ [% N# NSet fso=Server.CreateObject("Scripting.FileSystemObject")
* T: t& ~& e7 Y: U+ S'// 打开网页模板文件,读取模板内容 ; n) s# o) t+ Z4 N
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
% x/ a% p% ~+ e4 C1 |$ g+ ^9 ~strOut=f.ReadAll 9 U% P$ Z' M+ B, L4 C$ o
htmlwrite.close : G) U4 f% c j1 Z. c
& E5 F* ]# c" A) X+ d5 EstrTitle="生成的网页标题" 1 |1 _, ^1 E5 `) L" B1 l6 U5 |
strC ( A' V Z( i! s, K
- V- W$ j& W& l) o
'// 用真实内容替换模板中的标记
" `5 s& N4 z/ U/ s; vstrOut=Replace(strOut,"$title$",strTitle) * X' y* q% | p% y
strOut=Replace(strOut,"$body$",strContent)
8 ?# r5 q1 q7 S0 h! @; W9 L, m- j
$ ]' k& s; Q8 q# Z. z2 o'// 创建要生成的静态页
2 N) V0 w# x" E4 X M$ o4 fSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
) u( N% Z; r/ X$ q( C: d! c+ W
! o; f: Z$ t, i7 ^; `0 |- W+ T'// 写入网页内容 6 d6 j, ^" w4 r1 N; n$ e2 f, s
htmlwrite.WriteLine strOut
7 K5 }2 j. H; r! phtmlwrite.close 4 d) g) V* x0 d4 |
0 R+ L M k/ ^& d
Response.Write "生成静态页成功!" 0 \+ F! ~8 T& d: r+ j
) O$ r6 o% c9 }- o% I
'// 释放文件系统对象
O1 V! W) R" S9 D# L" fset htmlwrite=Nothing 8 V+ d6 B- z2 A/ z; A6 h* |
set fso=Nothing
8 Y) ]0 F! k* |2 v2 b, a9 T%>
/ G" }& G% f, j+ G" V$ w1 @2 h. O1 k Y6 [" R
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 4 m, X/ M! K' l( j
<% 6 l- o }# k9 Y8 H+ [
! Z( p/ @4 R5 l; Z, a5 T* O
'常用函数
2 Y7 d" F0 W$ S1 C'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ( h7 V* K* F+ T( a) D5 {% `: @2 j
function getHTTPPage(url)
0 Q8 X8 q2 q9 t2 s& r/ J; Hdim Http
! V* e6 ^$ }8 @/ I( {; c8 U {set Http=server.createobject("MSXML2.XMLHTTP") " ]7 ^5 [0 u/ W( Y2 C
Http.open "GET",url,false
0 x/ A* W7 W, a; {Http.send() " e* U$ j: x2 D* \8 W
if Http.readystate<>4 then , n. G) v: w' t; h5 k- U
exit function 1 A$ c ]6 w3 G) H" t5 d7 J
end if & I6 A- g, h2 y+ s: Z, T! k
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 5 g1 t! r! U" u U& \) C" J" X
set http=nothing
: G6 @- N5 y- b& z2 r9 L( _& Kif err.number<>0 then err.Clear
4 M5 k& m0 N4 K) x7 Wend function
1 B/ r" P B. Q$ G2 I4 c( n
, Q$ J/ D% O1 E9 \4 N1 I1 I'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 1 w4 b: J6 N2 _6 \/ D5 C/ ^4 R H
Function BytesToBstr(body,Cset)
) U) ^0 O& b1 A' v5 X9 rdim objstream & P }& r. D1 ?1 \9 b( A6 j
set objstream = Server.CreateObject("adodb.stream") ; X& H2 k: L; [1 A& l
objstream.Type = 1
. w; R9 m+ d6 X1 i# ?objstream.Mode =3
4 t: R" s' j+ G8 gobjstream.Open ! V1 y- {( t6 ^3 ~: J/ x! y6 B
objstream.Write body
6 T: l1 ]. ?" `$ @: s: Fobjstream.Position = 0 . {; B; ]$ k5 F, q
objstream.Type = 2 & x0 ^% o: Z/ ]" q0 \' ~3 V7 S
objstream.Charset = Cset
: D& t( C% n {( e$ C' oBytesToBstr = objstream.ReadText
" S, S; X( _! J+ L; d# Xobjstream.Close % U3 H% N N: e( c# I! N5 X2 n* s" u# d
set objstream = nothing / z1 R8 ?( j+ _7 A6 v. O
End Function 8 J4 Q- G: v( U5 c% K$ M
" Z$ Q0 Q( E0 y/ P2 E3 N
# G$ N: S, I) D6 d# FtxtURL=server.MapPath("../index.asp")
* G2 p" o( Y5 V( V5 m7 W5 n$ k' J4 V0 D* d! n
sText = getHTTPPage(txtURL) . L( |6 Y H, q( ?! W3 o
# }6 ^5 a2 K" S3 V- x
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
1 N/ {% B3 j$ T8 R6 g% M/ ~filename="../index.htm" x6 s( T2 D9 F
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ; ?, h! \. ^/ G5 t/ p' T
openFile.writeline(sText) ) r. x& i9 x# w3 D* v9 h0 [- w7 I
Set OpenFile=nothing
3 U+ a, Y9 y {. g
/ G/ e- z! \" t5 \%> 6 l* [% k# a8 T% Q( D. B/ H
<script>
# r0 n8 r, e* Ealert("静态网页生成完毕"); ; b# v& U$ ^- g% m- x4 w; g
history.back(); - V. Y1 j- n: r& p
</script> |
|