|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14317
- 金币
- 2444
- 威望
- 1647
- 贡献
- 1392
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
! n# u: i2 W6 y像www.aspid.cn的主站就采用了TSYS生成html文件! 8 Y- F- r* ]7 X7 K D, N
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
4 Y2 _; [+ M! V, d6 X
4 ` n9 T& |4 G; G4 @1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
# g1 C8 f& V8 efilename="test.htm" $ N8 R$ w z( z1 _( i
if request("body")<>"" then
6 ]4 k8 Z2 D. {4 f& T+ Q% _8 ?set fso = Server.CreateObject("Scripting.FileSystemObject")
8 h. ?& I! s# L5 `9 Pset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ! `3 i7 c& O! j
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 4 `; N% n) j/ R) J A
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" % N. H2 R8 V+ O
htmlwrite.close
0 u/ T8 f' V/ q! eset fout=nothing ( R( z6 U+ G4 T9 Q& A
set fso=nothing
3 g# r9 h- ] d- X5 H/ E3 o" `6 c- Rend if V8 |+ s# T, u- v* Q; E4 O; }
%>
2 r- x3 I2 E5 f4 J- Q<form name="form" method="post" action=""> # F+ V4 c9 t* n" F
<input name="title" value="Title" size=26> 9 [3 t" _* g' K
<br> 9 K L+ \ r. r
<textarea name="body">Body</textarea>
b: M9 p; l( J7 B8 w; I<br> ; Q, Z. o0 G1 c5 a* m+ m3 T0 E. F0 Q" g
<br>
" m. |5 J9 n; w/ X3 A<input type="submit" name="Submit" value="生成html"> & V, j$ A# Z, Q! i$ ]
</form>
: {: S# L& ?# ?/ _( o6 T2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. - r1 \% B9 m$ a' l' L
template.htm ' //模板文件 <html> % S" [6 X8 w: ~$ R, v
<head>
9 X: ?; Z" o. x7 S" C% S<title>$title$ by aspid.cn</title> , {7 c8 { l2 V# K' ?
</head> # i$ C8 H5 x' X' T, {
<body> % i/ w9 [6 D! y* a
$body$
- c8 ]5 ]0 I1 w* |) S7 i4 P' r& Q. Y</body> 6 P- W+ C% A1 m3 ]3 t. z5 `6 W
</html> ? 9 i' ?) X2 F' [4 h; c1 ?
8 @$ ^% x) n* ^+ [) O" i( V( GTestTemplate.asp '// 生成Html <% # b) I, s8 ]9 W! H
Dim fso,htmlwrite
/ ?& V9 r& S+ Z2 G# TDim strTitle,strContent,strOut 1 R7 n% h* P, H! r# N h
'// 创建文件系统对象 6 f4 z0 M b6 `) E! u
Set fso=Server.CreateObject("Scripting.FileSystemObject")
" U; t+ z6 N }; D'// 打开网页模板文件,读取模板内容 ) W3 m0 Q* g6 w& `" b+ s
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
8 \, B% |+ y7 hstrOut=f.ReadAll
3 z- o' q" t7 \! ~* Y( @. Thtmlwrite.close ! ]( B; R. S6 I$ J, C/ |
* |, D. x! s& m2 g' l. T' K2 XstrTitle="生成的网页标题"
" A) v% T1 D2 }7 \# r& H- k" \$ SstrC
& P% ?* v1 m O( w+ w2 C* z
5 J) j- |& G$ h9 j3 O% u/ N) \'// 用真实内容替换模板中的标记 2 ?) U* _( x8 j1 }+ V( g
strOut=Replace(strOut,"$title$",strTitle)
( u, i" u5 }) i& r A2 ~strOut=Replace(strOut,"$body$",strContent) * ~* {* H5 h) ~8 a& \( [( z
9 m% X$ `+ X: A8 Y# Y'// 创建要生成的静态页
" p1 r5 H4 s) Z4 Y' g& HSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
! h" h% t' i# o
8 n$ o7 D5 M! }) W& J L, S'// 写入网页内容
! G; z* D' E5 }* c g' Ohtmlwrite.WriteLine strOut
. d7 E4 {! r& |& E( b0 Uhtmlwrite.close 5 ]) u; a! T- d
1 l1 q+ Q. \9 q
Response.Write "生成静态页成功!" ! n6 K. K, j. F9 d2 X
9 k6 J& N- D3 @, { |1 R'// 释放文件系统对象 . Q; V& a: e7 i S- [
set htmlwrite=Nothing
" F2 N; D( V }5 {set fso=Nothing * y* \+ ?2 ^ ~( v) R, `6 H( K
%> 1 @" T1 A) z$ t
/ a& H. Y8 X9 p3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
v2 I& f0 l4 X/ Y$ R<%
' j3 b/ g' j! H- P1 |- D6 Y1 N
6 ^& \: d5 V' A+ |$ Q'常用函数 & V& K! i) I% A3 j
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 / p9 L) a$ } i; V( q$ w% V" F
function getHTTPPage(url) 3 l7 ~; j5 s9 }
dim Http 2 |6 J6 {1 Z S: N: F+ e; P
set Http=server.createobject("MSXML2.XMLHTTP")
' b' f9 l* @& U6 m% s2 R4 }Http.open "GET",url,false ) X) S- R- U+ Z! b) q
Http.send()
7 w" |+ G' G$ I* ?8 z9 H- V, {3 f3 uif Http.readystate<>4 then 7 \ o( L; {+ r
exit function
& m: L6 a4 @& ^& T k1 Fend if
/ j5 t/ C/ Y, L! t0 XgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
0 f9 c% B* C7 K8 A; t yset http=nothing
" @2 c4 j. Y! X7 iif err.number<>0 then err.Clear
( q' x; E% T- x3 u! \$ Y5 q& pend function ; i- m- p! D3 D. N& h$ s: |$ T
8 p' [- M4 M- p'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
1 r+ @& @8 ]) YFunction BytesToBstr(body,Cset)
- V. M$ h- ?, u! q/ y% L( Cdim objstream . q; V; X, I+ F2 | r. @/ @$ O
set objstream = Server.CreateObject("adodb.stream")
2 q! ^' o: B, mobjstream.Type = 1
5 ^7 Q7 {) v! `" @6 @objstream.Mode =3
7 o: }5 H! E# N, M/ hobjstream.Open 5 S, a3 [/ N- G9 T
objstream.Write body 0 o) q' n3 m- }
objstream.Position = 0 9 B5 q Q o9 _+ h9 N3 V/ H
objstream.Type = 2 7 e- o. {( S0 e* q. t
objstream.Charset = Cset
E4 p2 h$ C6 z7 W3 ]$ `2 N) s! Y, \BytesToBstr = objstream.ReadText
: g2 ?( P1 N7 Y6 vobjstream.Close
! ~ ^: X3 ]3 U5 j$ }set objstream = nothing
: J1 b0 x5 ~) Z. i/ _End Function " @) ~3 e& h) ^. N2 H4 c# \$ v
9 C" M$ i1 i: p- h
9 ?+ M% Y% ^/ ZtxtURL=server.MapPath("../index.asp")
# X7 J x$ N6 z) B
) V7 u/ A5 F# ksText = getHTTPPage(txtURL)
9 _: W: t( }6 u& C
8 V6 e# H7 D4 M, ESet FileObject=Server.CreateObject("Scripting.FileSystemObject") & M/ i7 }, r4 u! o9 H
filename="../index.htm"
1 S" ^( j h) ^& n8 R SSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
; ~, `. q0 A: J8 J, @; H; _6 bopenFile.writeline(sText) 0 Y8 Q& }- Y& A! T
Set OpenFile=nothing
5 a# [2 R# E0 v% j0 ?
" i, {& [) F- N# H% f! }%> 5 d/ ?* C( x& R0 c1 A6 d
<script>
2 `( o- H0 I5 ]# |2 b# halert("静态网页生成完毕"); " ~9 \3 C, W, z1 s8 P9 M
history.back(); ) U4 X; _4 ?6 {+ Z
</script> |
|