|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14333
- 金币
- 2452
- 威望
- 1647
- 贡献
- 1400
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
' F; A4 v7 ?0 P) ^$ G像www.aspid.cn的主站就采用了TSYS生成html文件!
: o3 K9 t3 }! @( P9 B& ]4 [, D所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. ' L: ? p: g% `
! ]7 Z Y3 u% E$ t0 c
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
5 ]1 X6 \9 ~, z. `' y* C, ofilename="test.htm"
/ c+ t$ T/ ^* a: u; ~/ Hif request("body")<>"" then
4 T* i& ]. B) U4 A; \set fso = Server.CreateObject("Scripting.FileSystemObject") * W } o$ Z! d J8 y( B/ k5 d1 Q J
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) " M4 G9 D. U" e6 G5 f) s5 r& F
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 6 n+ J. F. \' G1 I
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
% J5 M+ q9 G# a5 Mhtmlwrite.close
* `( z+ J" p5 A; p2 s* nset fout=nothing
- q* S8 s" C' q' zset fso=nothing
: x" J0 g* Z, U% qend if
@* V; b# C: G& A7 H0 s/ w8 s! V0 G%> ( v! s8 X6 G) i# G- ^! }
<form name="form" method="post" action=""> ( t" g( u3 }$ o
<input name="title" value="Title" size=26> Y! \$ p/ J k3 T* p2 w. l' e
<br>
' g. s' x4 C6 B) ^; R2 g) ?3 p<textarea name="body">Body</textarea> 8 s f8 Z: ?5 A$ y" p
<br> / |0 k( }+ x2 I) I* ~' {7 \0 q3 A
<br>
# V) U: f2 t% [6 M& ?2 @) P/ p8 u/ ~<input type="submit" name="Submit" value="生成html">
7 l% a& t) O9 {& t9 i4 e1 B</form>
" Q- c( `' e7 ]6 W2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. , B+ z) g7 N& u* f5 D* c
template.htm ' //模板文件 <html>
" z' g" [! }. p9 H5 f5 R3 s- i<head>
, m4 U3 [& h1 }/ X {) V<title>$title$ by aspid.cn</title> 1 ]. d! b1 g; s$ [6 s
</head> % W9 p0 t+ i. M/ Y. q$ n! J' v
<body> 0 B |& R$ x* T$ s
$body$
. m$ D+ {/ F: e% C</body>
3 a6 e; i7 f+ D$ K" ]' Z L) |</html> ? 9 c7 T0 n& m' m3 h" V
! o4 ~* V# H9 zTestTemplate.asp '// 生成Html <%
: t) b4 b6 f7 I/ @" ?" NDim fso,htmlwrite ?$ O" \! i0 J
Dim strTitle,strContent,strOut 6 G2 J$ }3 E# l9 D3 J) n6 q3 W
'// 创建文件系统对象 9 y& |8 m/ Y8 L) d, s
Set fso=Server.CreateObject("Scripting.FileSystemObject") 1 x- }* M2 v0 m. |0 P! y
'// 打开网页模板文件,读取模板内容
9 R2 z# G. }! H3 PSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 0 s$ [* k6 m2 l' J- I
strOut=f.ReadAll * A% y4 C% K% W
htmlwrite.close % g6 d3 ~) Q3 B" J
6 J& J$ T8 x/ e4 s4 U7 V. k, kstrTitle="生成的网页标题"
, k7 Q# u: J1 e7 s' tstrC
' }4 C, v% E0 G3 x$ n( T; u. F! o, G N
'// 用真实内容替换模板中的标记 : `3 j3 n3 W3 F! F- C
strOut=Replace(strOut,"$title$",strTitle) \1 y" M: Z& [
strOut=Replace(strOut,"$body$",strContent)
8 G7 B0 Q4 J! ?* b, N1 S) I
2 O2 e7 b9 V4 o; Z6 p'// 创建要生成的静态页
1 |0 E% ?0 K. |1 mSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) 9 `" @3 h8 D) o0 Z( ~" V7 ?
7 K; ]' h0 r, k
'// 写入网页内容
( h1 i* K9 Y& X, lhtmlwrite.WriteLine strOut " E) |6 x1 y/ r9 V4 [5 h0 ?) b
htmlwrite.close
% n" N# H0 O) Z6 ~9 ]4 h, m! `) R
. a+ ^! C. `0 F' {; FResponse.Write "生成静态页成功!"
) C* V0 a6 { s- z# t! Z b- y) M5 d" |5 |- U1 N z i
'// 释放文件系统对象 , X# x. {5 A$ |* c
set htmlwrite=Nothing
) Q4 Z& w# j9 |# Y' @set fso=Nothing
' b0 Y; g6 m3 Q4 u& J |%> , U* [" Y0 @, D" n
2 c8 a: K" k0 @+ s2 {3 M" k* ^3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. / J# x& d% k' M, n4 s) [1 L
<% # H; @- i Q& H% m, h
. z8 m1 O! h, H/ @1 _: c
'常用函数
% S* o) W N8 w7 q6 h Z2 f0 d'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 1 A$ l. l5 |8 A2 H! Z8 D4 n& H( t
function getHTTPPage(url) & M j2 a& D3 _, Y' d
dim Http
e1 {1 ]4 T a/ ?& \. }- c) Kset Http=server.createobject("MSXML2.XMLHTTP")
5 L4 n% Q& u7 ]' ?" V# ^Http.open "GET",url,false
7 v' Q6 S' V) x9 l3 S* ~Http.send()
, j6 a; [$ ]- i$ D7 O, Yif Http.readystate<>4 then a7 F8 h, p- r8 a: o
exit function + h7 s) A+ b4 ~* R9 T
end if
4 u5 K% L3 q- [- J5 |& NgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 0 K, N2 s, j; c% C
set http=nothing
; `( }& w N F) ^7 w: Eif err.number<>0 then err.Clear " _& ~& v# E) r$ y S1 Y! q
end function
8 }& I3 e3 i) a$ q) c: R7 W' [$ K4 a1 ?/ j. `7 ?2 Q
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 / [$ k/ S m0 w5 T, m, [& @
Function BytesToBstr(body,Cset)
+ O; [! | x0 O- \( {% xdim objstream
# u! l- h* L8 ^; y* Uset objstream = Server.CreateObject("adodb.stream")
) m7 `, [$ m/ @% k% J/ zobjstream.Type = 1 , b% a; r n' n1 C W1 `2 N) j) ?+ m2 o
objstream.Mode =3
6 W6 L& T5 d v& c2 g$ l2 C% Qobjstream.Open 9 ]$ l5 @& A7 S% @
objstream.Write body
7 i4 }% Z" q2 d+ V! W1 A2 ?objstream.Position = 0
; l( q9 W* f" Pobjstream.Type = 2 + z) i6 t1 s2 G8 s& `
objstream.Charset = Cset ( p3 H/ V# Z( V! _, h* W
BytesToBstr = objstream.ReadText
$ x F( i* l$ T3 qobjstream.Close
) R( z; o" Y1 v# y/ C- e2 mset objstream = nothing + w7 h# y6 B: D: h$ p- d
End Function 5 K6 K6 V, Y8 d8 n, U( m' L
2 D4 t S$ \" Q9 y7 S9 W/ p
6 i) d) F1 B4 x
txtURL=server.MapPath("../index.asp") ( {/ ~3 o$ E; s0 d
8 o3 O( q' c2 l7 j, i" V/ p
sText = getHTTPPage(txtURL)
; c: N5 [! o0 Z8 [2 j! ]% z
1 R3 \/ }0 R& P& ^/ x8 }; ?Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
8 @/ v' p2 b6 d& O6 y1 R _filename="../index.htm" * E ]* w: r$ R9 z6 n) \; P
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
f4 ^* Q8 n6 x IopenFile.writeline(sText) . ?4 G3 H8 [' n' E
Set OpenFile=nothing
+ V2 L f2 V$ {
, h1 G1 ]/ |8 j8 ]1 t7 L/ I! B%> I9 @( j! g) a! J# g( R. f
<script> ) ]- i1 J) H3 H5 h
alert("静态网页生成完毕"); 5 ^, T- r+ r: c
history.back(); ) q: a) `* P. k
</script> |
|