  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14171
- 金币
- 2376
- 威望
- 1647
- 贡献
- 1324
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
5 h) n/ B4 M: i9 f: Y9 o像www.aspid.cn的主站就采用了TSYS生成html文件!
% q' s' A" n0 E/ m/ D1 d所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
" k0 ~9 L# M; T9 R8 h7 B! E$ j" b- ]0 x/ s. S- i
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 5 r0 W _( e3 G
filename="test.htm"
9 @) {7 f. z, w9 Wif request("body")<>"" then 6 M& W/ _% Y7 i+ ^! f9 U
set fso = Server.CreateObject("Scripting.FileSystemObject")
* V; E/ x5 n* xset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) : J# G1 } K/ b8 C7 M( `% v
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
* r c. o% G4 v3 x* t, R4 |- Y4 Hhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ' O) n7 O/ Y! C4 o1 ^& }& D
htmlwrite.close ) f/ F" `5 j* `9 C
set fout=nothing
3 U2 K% H4 ~6 U; u8 F6 cset fso=nothing 9 ^" x5 e, j( E; ~! ~
end if + G9 ]1 u. s! L) L# B5 T5 b
%>
3 Y8 d: o2 S6 ]' Z$ j<form name="form" method="post" action=""> % b1 C$ r: m; ~
<input name="title" value="Title" size=26>
W2 n% |% ?. d) }' o<br>
2 v5 V8 k/ T7 [- c9 O% ~/ N<textarea name="body">Body</textarea> ( P& a; j' ]0 N A
<br> 9 r# X; |8 q/ J2 ]) Z {
<br>
3 Y3 o' v3 U8 B: L<input type="submit" name="Submit" value="生成html">
6 B% W$ j+ i# q c1 i+ t</form>
2 e, |% q0 Q4 |4 C7 i g% @2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ! m4 r4 `0 j, R
template.htm ' //模板文件 <html> 7 y+ A4 u( d" a: V! _
<head> * B0 X3 X/ n; }* y" O- P
<title>$title$ by aspid.cn</title>
. K9 |4 v. n3 c' Q</head>
) |. a3 |* d! u: r9 y" }<body>
/ z& r8 H9 e4 X+ H; T$body$ 0 E( t6 b9 |( D3 j
</body> $ A( x4 V/ F9 r7 E Y( w) a
</html> ? ! m' R! l) u, }0 k/ n! s1 T* {/ n
6 H W, `! a8 b A1 S7 d, H
TestTemplate.asp '// 生成Html <%
% Q2 S1 H8 {0 ~7 ] @& |7 b, GDim fso,htmlwrite
: P8 i% ?, T- T& k- y% |8 ?' JDim strTitle,strContent,strOut
$ e7 u+ t+ P: \" s: [4 x'// 创建文件系统对象 6 Q7 H6 T. `4 e9 p( s0 Z% F5 ~
Set fso=Server.CreateObject("Scripting.FileSystemObject") / g9 V/ e1 Z* _* ], U
'// 打开网页模板文件,读取模板内容
- E+ ?' W3 Q$ k6 R3 s3 H. cSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 7 d3 l: w6 D; Q6 \* F9 [1 x" l. L1 J
strOut=f.ReadAll
}. {( p" W+ vhtmlwrite.close . J2 Y% u+ {1 V7 w' l/ U
) N) [$ o8 L F( d' B! y
strTitle="生成的网页标题" ) q; Q F4 D) [+ u4 `0 l( c* [
strC
B( k2 P0 i) g+ @; u8 n+ E
6 j5 m p |0 D'// 用真实内容替换模板中的标记 5 y0 N* l4 K9 ]
strOut=Replace(strOut,"$title$",strTitle)
+ Z$ v9 ^ R! D2 ]( y$ nstrOut=Replace(strOut,"$body$",strContent) 6 v! p4 Z$ E% z5 Z4 h
* j: t6 ~* Z+ B6 G' {/ L- O'// 创建要生成的静态页
# i3 e; S( i4 t4 N' d# ]7 l! L9 nSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
9 ^4 K' z! b4 v6 U( x* x. F/ u* _0 i4 H% T2 z. N% N9 W
'// 写入网页内容
) S, Y) R/ w5 W# C: m K! b/ Whtmlwrite.WriteLine strOut 6 E) Y3 ~4 S9 x+ o
htmlwrite.close % }) r% p! _& r$ H
) P2 |7 G7 M r2 v, u: l
Response.Write "生成静态页成功!" ' J6 M7 S$ T- c' P
% c5 s5 M) ~) a6 @( X" M5 a
'// 释放文件系统对象
; W% E0 `' [/ m& U9 s* h) e# `- ?set htmlwrite=Nothing
, g! |" [* \& Dset fso=Nothing
) p! U, {+ p9 s# {8 m%> ; y- n% v+ `* O9 y) G
7 B: i2 e, r# g% N3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. $ C! P4 ?- `6 G/ S' F4 u3 S9 b
<% ( P5 f1 E& G7 I8 w4 R3 X
) ] V' F7 B s$ K( w
'常用函数 9 j; }7 E( J- _7 H4 e, f
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 - g. y! u* U$ p5 v) \$ N2 m
function getHTTPPage(url) / ^1 r# r2 g8 p( q
dim Http - U1 H& e6 z. l9 @7 K% m0 w
set Http=server.createobject("MSXML2.XMLHTTP")
4 C( G' @) s; I ZHttp.open "GET",url,false
, w# S$ ^+ x, ^- ^$ B: h# l$ @Http.send() $ F4 S9 [3 U$ j
if Http.readystate<>4 then
, p+ b0 L4 }0 C7 Y2 Wexit function ( u, `; c R' e8 }8 @
end if 2 ?6 v T% l5 h6 s& r7 z6 h- y! }
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") " S, w1 r. Y: F4 w; v
set http=nothing
- z5 r u+ G! r' K! eif err.number<>0 then err.Clear
$ o& F7 u/ D9 [3 O+ k" c$ N$ Bend function 5 E4 x5 S$ `3 g4 f( h A" s% A
) r, h9 {! t0 j- `* \& X3 n3 ]'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
: ^8 G& e1 u3 u gFunction BytesToBstr(body,Cset)
% ], W R: i+ fdim objstream : q5 S) ]9 z, w$ |3 l; N H7 }; E
set objstream = Server.CreateObject("adodb.stream")
/ t) v2 R& y# R( d) Wobjstream.Type = 1
* W9 y3 I$ D5 ^& eobjstream.Mode =3 3 [4 k. c, V5 l% s: y' t; N
objstream.Open
) m( ^/ n, @$ v' a/ z9 M4 zobjstream.Write body . e, y# x' U5 U/ T e+ o
objstream.Position = 0 1 e6 r$ k/ U. v2 s/ I6 X2 ~# L
objstream.Type = 2 % j& G) e6 V* c3 }. U( I- X5 h. q
objstream.Charset = Cset % Q! r" @/ }9 e; [; `; F" {
BytesToBstr = objstream.ReadText
. B1 L( C- w: w/ ]- r8 Cobjstream.Close 1 o4 i4 W2 ?( {5 c/ R D* B; I P
set objstream = nothing
% \8 E5 e% ~- ~: U( MEnd Function # k" H5 w, U5 }8 Q
. Y- b2 R# s& t! ?& t
- _2 p0 K& }/ o. p: BtxtURL=server.MapPath("../index.asp")
x0 ?7 ^ ?/ _) \9 K9 n# {4 @) l1 t0 Z3 C5 I/ S
sText = getHTTPPage(txtURL)
) Z: U5 @) }- Q* I2 P
" _2 u! Z- C% x. f. C+ QSet FileObject=Server.CreateObject("Scripting.FileSystemObject") + ]* Y8 Z% c" ^5 i* V
filename="../index.htm"
3 E, U* `8 V+ L( V) \* XSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 0 ?, |7 t/ ~7 y. ~7 W8 }
openFile.writeline(sText) 2 V, b) h* s3 u/ A
Set OpenFile=nothing
9 a) P! I: c7 `0 S. t# F1 N) h: ^- h8 v& A# [* ^" [! M; F: ?* f6 c) {
%> 5 ^, M9 M6 u# o9 p
<script>
( K: o+ G# M* S9 J7 {# ^alert("静态网页生成完毕"); 8 z, A r* y x$ ~5 U; Z
history.back();
, q2 J. e8 F! p</script> |
|