|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14389
- 金币
- 2480
- 威望
- 1647
- 贡献
- 1428
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
$ R1 f- B! c* \9 X$ p* X像www.aspid.cn的主站就采用了TSYS生成html文件! / r6 D+ u3 n. M6 K2 l. Z7 U
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. , j2 N3 M6 K Q) O. w( I3 N: k
' ]% j# C: K* A K
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
3 J. g9 i4 S' o6 d; ?! O sfilename="test.htm" + h- W M4 [3 q2 c- b, A% y2 Y
if request("body")<>"" then 2 O; i9 ? j: G* l4 {
set fso = Server.CreateObject("Scripting.FileSystemObject") # a3 O( R4 Z, \9 {* R) \% u
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) ]( _2 c, e+ a! {3 [
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
& x J, R$ h* U% @7 lhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
! A$ O! v9 N# R" Ehtmlwrite.close
; |. E X" B. _4 qset fout=nothing 1 y! ^! m! k0 M( c" N2 B1 p
set fso=nothing
+ U. s6 t9 ]2 n9 W' g; ~6 Nend if : o5 t+ _# c, Z, b' s6 @# `# J
%>
( G9 w9 Q' ?1 q2 h: l<form name="form" method="post" action=""> ) h6 f6 h) h6 J4 ]$ Y1 ~
<input name="title" value="Title" size=26>
, f6 y% r% ]) |5 v<br> % q; S+ T1 h" Z
<textarea name="body">Body</textarea> " V) S8 S0 f4 u+ }# w. w- y( j
<br>
6 R3 a3 p' H& l8 T4 \- K<br> 4 \$ W! s5 r. I `1 w
<input type="submit" name="Submit" value="生成html">
1 N# [6 ~6 X, n6 [! c- c/ d</form> L6 Z5 Q! }; J; S1 r. a* b
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
: [/ W& f9 W! H' e7 jtemplate.htm ' //模板文件 <html>
+ a% m" k' a; A" \5 _<head>
$ o4 p8 b" p3 }- E2 j( c2 E<title>$title$ by aspid.cn</title> 1 { H D3 n0 [6 _
</head> : o8 }; S3 K! h2 u# @2 l
<body> ( s% v! O, X/ [
$body$
" G/ j) s5 K( Q</body> 0 f( s! H7 ~+ J: s8 r* m% \
</html> ? 1 z# [" g0 A( `" D# k: a
( l( W# `. x+ \# QTestTemplate.asp '// 生成Html <% T o. D4 H1 w) M9 r
Dim fso,htmlwrite 1 Y+ P! z0 Y3 q. I& w( u
Dim strTitle,strContent,strOut
, e/ t" H# r1 R- J8 O7 W'// 创建文件系统对象
( n7 g$ \. k1 _8 V' QSet fso=Server.CreateObject("Scripting.FileSystemObject")
0 @6 {0 a0 @ x K, i'// 打开网页模板文件,读取模板内容
' V3 |; X" f& q+ PSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
, p8 r' Z4 y" ?3 QstrOut=f.ReadAll
4 D, x, o0 Q' V6 Bhtmlwrite.close
8 F* @4 P, x/ c4 t6 @+ [
% l8 |) V- |/ h2 i: astrTitle="生成的网页标题"
) w6 C6 {1 }0 ~strC
/ k& T5 v/ P2 x6 x9 v4 L L. {. _4 d# {# D5 d) V. J
'// 用真实内容替换模板中的标记 0 L( r0 Z s. _9 R; }
strOut=Replace(strOut,"$title$",strTitle)
, N* `5 U" ?$ {5 o, ^- vstrOut=Replace(strOut,"$body$",strContent) : ~* F/ ?0 S7 O6 J; `! A, _$ {, d
& m- |5 F: ^- f7 L4 U* p6 g2 M'// 创建要生成的静态页
7 P6 O# }9 d, ~* s+ ^& LSet htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) # K3 t1 \9 y- S
1 Z% t B$ D5 v' s k( H4 {
'// 写入网页内容 5 ]7 O& [/ v8 _" B
htmlwrite.WriteLine strOut
- K" S% D( [/ F4 dhtmlwrite.close $ ^* ]; a( Q5 ?
4 B/ a! i. Z) M6 ?# I- IResponse.Write "生成静态页成功!"
2 o% c5 P+ C) a( q
3 M% E/ {0 s" S3 f& H'// 释放文件系统对象
) }% A6 P8 q) J( @* k# q. F5 Gset htmlwrite=Nothing 4 l8 l1 K& P. i0 o) o( H
set fso=Nothing ( P0 f. ^6 a6 n3 c
%> 1 }% k0 ?8 t6 U) r; |
# |5 [1 o3 R$ J/ V2 j
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
/ b0 C$ T0 c9 x& h. l/ N3 T<% ) W0 Z) I8 h- W v! S/ z( G* q
- [* e) d" O$ B2 p( ^0 v r) [& [
'常用函数
# F6 V: Y* z' \# t'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 + j+ w- t6 F/ c$ m
function getHTTPPage(url)
5 t3 v$ z/ \9 b, o, ]dim Http # [! a; |" M: g( B) S8 p( G7 M- ~
set Http=server.createobject("MSXML2.XMLHTTP")
8 t+ Z* ?( f9 e+ p Y2 X8 wHttp.open "GET",url,false
: \ A$ r" }! V9 H8 ]Http.send() 8 e4 \2 t V) E5 Z9 Y, y% \, p
if Http.readystate<>4 then
- `0 y/ N0 C, d- b! @exit function # A) `3 _% Z S1 s6 h$ Z1 g
end if ( O( J$ N/ f, \- a
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
5 `2 e3 F) ^+ {% S* @8 `/ Mset http=nothing 6 N! {- r" t' \6 K) G
if err.number<>0 then err.Clear
0 H B& S b- M; r$ m1 x) j0 B& qend function / }- Z2 e; o6 u9 w! | p; T
, P3 Q7 p6 h9 N'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
8 j; H) \+ L5 C) BFunction BytesToBstr(body,Cset) 2 a3 t# H( n/ _* S# E+ M
dim objstream
# W" i5 D! y* f% c- {: Jset objstream = Server.CreateObject("adodb.stream") & [9 Z. b" a- n# G0 d6 O% l( R
objstream.Type = 1 ( d: O) d3 A7 z+ ^8 x1 M
objstream.Mode =3 5 z' G5 R9 P% }; _+ D1 N: N! \
objstream.Open 9 [3 J4 y( h2 {" w- Q2 t
objstream.Write body ! W! @9 t) [, h& {- K
objstream.Position = 0 & c* Y8 t2 U: g3 t( _& H" r
objstream.Type = 2 6 @6 D! t6 }# q; h8 _' L
objstream.Charset = Cset
1 ~# L# U9 k/ ]2 v' N- n& h3 ZBytesToBstr = objstream.ReadText
S& d# r! t! M# Uobjstream.Close
; M8 |% m' l8 W* {8 R+ U$ `set objstream = nothing # b- Q: U% J& I. A& a9 H
End Function : z4 Y( s# o* p3 Q, j- M
5 @8 b& n U( Y% j4 F9 ]9 E9 H
& a, j. f( y# Q5 EtxtURL=server.MapPath("../index.asp")
* _: J. r( H( J: q
, p% Z. d) s6 tsText = getHTTPPage(txtURL)
% V: E- W8 `. J! \; _+ i+ a! O& q+ G3 z" f
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 8 c0 u9 Z# a' E5 p# u9 f5 i) u
filename="../index.htm" 0 ]6 s* G9 p. h9 o$ h, z, H3 u% d9 n
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ( P; L p! n- R( L! N
openFile.writeline(sText)
( @/ j3 i! V% \% w# g5 \Set OpenFile=nothing
' ?+ S S* T" @3 _$ x+ \4 h& o% A7 J4 C5 |; B
%>
. w& b3 v9 [# `& T- i# L \<script>
+ u& A/ H9 x; ]! {- T, f8 K9 m, valert("静态网页生成完毕"); M3 Y2 R3 B0 l4 b
history.back(); * Y$ l* K! `4 _# l' j
</script> |
|