|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14427
- 金币
- 2499
- 威望
- 1647
- 贡献
- 1447
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ( L8 h$ l: o* K2 D; Y6 ]" o6 G
像www.aspid.cn的主站就采用了TSYS生成html文件! 7 x, [1 N( d" J/ r4 x
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 2 S* B6 w. m W! A! T3 d) z: r. [) M) A( g
6 g: z: J5 i. B |( H& x$ b1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
+ O' e( n# j& z8 Mfilename="test.htm" ; a6 [2 \8 R: [; O6 m. I8 S
if request("body")<>"" then
7 ^ ?$ U$ j2 j: W6 k7 a& r. Kset fso = Server.CreateObject("Scripting.FileSystemObject") / z3 k7 q2 w! `/ Q4 ?3 Q9 L9 N/ u1 R
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
5 s8 C) m% t' U8 ghtmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" ) \) S! K3 t* i' Q+ U
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" * b6 g+ b: `. i7 c& `: P3 w4 m
htmlwrite.close $ K0 r1 g, F2 i& o" N$ ^* d/ e( ]+ m
set fout=nothing 4 {3 |! F$ N% I% E( {
set fso=nothing ; e; ^6 x, ~$ Q0 i7 ~4 Z R* s. S @
end if
4 P! r# M) w& z6 w8 y5 Z%>
/ m( b" e! p1 P- m. X0 O1 Y7 d<form name="form" method="post" action=""> 5 \7 Z2 G, U$ j. l/ l1 o
<input name="title" value="Title" size=26>
, Q6 n, ^: o9 f+ b1 ^<br>
3 I# Y# h! P) k( A5 |3 A<textarea name="body">Body</textarea>
# [# v7 p0 ~* A' c<br>
/ `- m2 p& p' B+ {<br>
: O0 f$ [6 o* h* k5 R<input type="submit" name="Submit" value="生成html"> & m+ D& z7 L- v) C, H" I
</form>
- I) w8 Y0 {+ A2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. ( n' b6 ?- ?& w5 \. z$ E
template.htm ' //模板文件 <html> ( q' \2 o3 e) H( P* F. Y- k
<head> ' g. l& f F _2 s! I
<title>$title$ by aspid.cn</title> . F6 Y* A2 m5 |4 `/ Z
</head> : a1 {# }; R$ Y7 f
<body>
$ h/ ?& E' V9 R) `& k; h$body$
1 N" T; v% {/ J* t</body> ) H) W; _1 h8 v4 L2 \7 e* i
</html> ? 3 P) E8 y5 p+ Q
1 p; T- M3 c6 [/ j
TestTemplate.asp '// 生成Html <%
l+ A+ u0 o& |! aDim fso,htmlwrite ' R, o5 N6 B" z' e2 I+ j! K$ P
Dim strTitle,strContent,strOut % J" \% u. F; h
'// 创建文件系统对象 # j/ H- P% \ |, _+ ]
Set fso=Server.CreateObject("Scripting.FileSystemObject") : v) L# C/ L& R7 g; _; f# L# R0 }
'// 打开网页模板文件,读取模板内容
1 \0 O: ?2 Y1 E4 a% tSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 8 s9 W% ` q* T+ T" @6 J
strOut=f.ReadAll
E1 h7 t/ A1 p+ F6 ?0 dhtmlwrite.close / d o9 \; }! V; ?: N5 F
9 d8 n+ a! r; Q* R
strTitle="生成的网页标题"
/ m. C4 S* Z# g: x* G; ?4 u6 U8 r$ SstrC " p# e+ O8 F! j6 C" }" q- s3 t) Z
3 N. \! r V. a8 O& M'// 用真实内容替换模板中的标记 ; j9 b/ V1 m& n( _
strOut=Replace(strOut,"$title$",strTitle) / A4 E f5 n* T8 k
strOut=Replace(strOut,"$body$",strContent)
* J$ ~: A( T3 M7 ^ \3 g- x
1 W0 h2 ~# P s* b'// 创建要生成的静态页 8 `6 {. j9 Q. b9 ^3 {( s
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) / u9 G h: g, N: n/ Z" d. a% f" q& P0 F! K
- h9 {+ `- Q" x; z7 B7 f3 K
'// 写入网页内容 ; [* H* v8 c s8 I5 l3 p1 {( O
htmlwrite.WriteLine strOut
: H; [& w6 [) L# O# G9 i/ b& m" n0 uhtmlwrite.close
9 w0 J4 F# n1 |6 J1 e) p
! j9 y3 n: G& S- [/ bResponse.Write "生成静态页成功!" # V3 s/ ]" d0 v
! y$ h' W: ^' Z'// 释放文件系统对象
, S/ @2 z5 ~) q3 F3 T# O: w- V7 Rset htmlwrite=Nothing
* P W$ {+ u. L% m5 f3 ]set fso=Nothing
: d5 h. s; V q8 T%> |- ]7 |' b! B, o1 `
) E4 a8 h4 u( M4 m/ z! j3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
' \% j0 o2 @+ h8 w8 J<% 4 L1 s# C0 T7 b: o7 N5 k
/ l7 o' L/ O4 _1 p
'常用函数 . `8 O: Z* v7 I; L
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 * N- w/ m' K+ |. n4 _8 C
function getHTTPPage(url) : Z- n- S3 K! K' ^ t! K( x" e
dim Http + w% G# p7 Y) `
set Http=server.createobject("MSXML2.XMLHTTP") 4 }8 T5 g: t% A/ M$ i; a1 r
Http.open "GET",url,false
- ~" c9 r" f* NHttp.send()
0 F) o5 n; S6 H, h' x/ s m/ y* V, @# vif Http.readystate<>4 then " X' B- S$ e4 r5 k# R
exit function , P6 b' k k3 z* A
end if * b6 X: ]+ G4 R# V4 C/ ]0 G8 [$ U
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") $ d9 e; v0 j; b9 X
set http=nothing
% f3 K$ W- A8 e0 mif err.number<>0 then err.Clear : x6 v6 q! `. j; K+ Y# I9 c( n7 c
end function
0 b& W# c" v, G* S: ?9 t& P7 m; b( F( f( t
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
, a0 ~. e' r6 LFunction BytesToBstr(body,Cset) ; B! @4 H5 H7 n9 ?- _2 r
dim objstream
% ]$ s7 z3 Y# c: T9 }" `set objstream = Server.CreateObject("adodb.stream") . }( r3 d% o) Y4 b5 K
objstream.Type = 1
' e7 H" s$ b5 I; @' @9 V3 q* ?; `objstream.Mode =3 & W. ?0 Y* s, Q9 t
objstream.Open / s) m! o/ X0 M; |- { |
objstream.Write body
2 N' i: b+ |+ b) z, e& mobjstream.Position = 0 , C' q8 G% b& K) o: i
objstream.Type = 2 / E6 f+ s! j3 g
objstream.Charset = Cset ) u' X ^( q1 j# P) i1 z
BytesToBstr = objstream.ReadText
( v* b! w" @: r/ K1 Oobjstream.Close ' l/ n& t8 Y* ~" w- O2 N
set objstream = nothing
5 V( o- N o' Q/ t5 e( UEnd Function 8 |, W8 [ a/ p" x7 ~$ t
* @3 g' [0 K* ^+ `
i. E, o: y- V, _" x0 b4 Y- ftxtURL=server.MapPath("../index.asp")
9 {" B6 d4 G* F) x7 ^: y; W3 @! N# }
sText = getHTTPPage(txtURL)
6 l5 _9 u" k1 _$ y8 n' p0 P0 r: x( l2 F
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
+ w3 T. Q/ o2 H& W/ i2 ifilename="../index.htm"
2 d$ q1 l/ Y* w6 Y2 j' J bSet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 8 b+ @8 c0 K! a3 p5 W+ D# M* I7 {
openFile.writeline(sText) 2 q4 z& C1 l7 E( y$ E1 y9 ?$ l( |
Set OpenFile=nothing ! [0 Z8 n$ X, ^5 A* h* F1 Q
" Q/ X9 [3 o0 _3 N
%> : I3 p: o1 y$ ^2 T% n
<script>
, a6 V" U, F: \9 ~2 a1 Malert("静态网页生成完毕");
8 p5 Q! F+ T2 w* Z. [) c$ Ahistory.back(); * c1 f! i& G& i7 p/ i6 ~1 o
</script> |
|