|
  
- UID
- 1
- 帖子
- 738
- 精华
- 28
- 积分
- 14393
- 金币
- 2482
- 威望
- 1647
- 贡献
- 1430
|
网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
# R% S* U }2 i5 u像www.aspid.cn的主站就采用了TSYS生成html文件! + L! B# q: t8 P# F& t0 B" X3 o A/ J
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
* Q* A% i: ?) J3 v5 U5 x
0 w/ O9 A+ I. H# c" b6 W1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
6 \( s( H8 k/ {/ Y8 x- ^ B9 _filename="test.htm" 0 Z3 h6 u, ` c
if request("body")<>"" then
, }$ g# U2 c0 J& H( Rset fso = Server.CreateObject("Scripting.FileSystemObject")
% V- Z9 }1 }% }( R4 H: ?* mset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
; p! ?: @5 G4 c9 y" R/ `htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
' L/ w. h4 W9 r; ^; B. z6 ]5 jhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
9 {# A! G6 W# {. S$ nhtmlwrite.close
0 W% q2 ?4 m$ |9 l3 h6 dset fout=nothing 0 i1 ^* _# M& H* a0 e: L4 `
set fso=nothing
. A( J5 o6 n) gend if
9 t/ w' m! R) Z0 i& d" ?" H%> : S& E% _* k ~( u7 f
<form name="form" method="post" action="">
( c* {7 q" m$ _6 A4 D4 x<input name="title" value="Title" size=26>
; k- y/ n6 j; a+ l# V: B) Z<br>
+ c" w# \: }% F9 }/ @$ b<textarea name="body">Body</textarea>
$ b) p7 E8 T4 B9 ^7 Y" u9 }( d<br> ; v5 N* A" A. V r# W/ n
<br>
3 W1 H( _% f+ Z0 {0 v \8 c<input type="submit" name="Submit" value="生成html">
% `7 x' r% `/ Y5 c- Q! A</form>
: i) a! N+ R0 O1 G2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
/ Q4 N. W# m& m# W5 P% R- a/ Vtemplate.htm ' //模板文件 <html> 3 l3 Y7 ]6 s9 y+ r- G& \- R
<head> $ l& u/ x$ g4 w" c' p& ?) C
<title>$title$ by aspid.cn</title> 6 V/ O# v5 E, J2 L8 E: a, U, Z
</head> 9 d A/ i6 Q2 x, _5 l# D! ^
<body> ' t( A$ r+ x5 N# s1 N* V9 O
$body$
0 D7 B; }6 s) y</body> " \" p- F9 ]* Q( `* c" V1 {* Y9 w
</html> ? 7 f, p' s: J: g/ [1 c! `0 f) f
6 @4 [" T# ^' W+ i! |( k6 Y
TestTemplate.asp '// 生成Html <% , I: S/ V: z* m- u* ~' A
Dim fso,htmlwrite , G2 p$ P, i1 V" ~& r) e1 d
Dim strTitle,strContent,strOut
1 J% \. N: Q2 I. k0 R'// 创建文件系统对象 3 w! h% f9 u) P+ q' s4 m$ ]) x
Set fso=Server.CreateObject("Scripting.FileSystemObject") ) G8 ]$ h* _- n T; O
'// 打开网页模板文件,读取模板内容
; `7 K' B7 _) W5 ]8 jSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) * y: @- w1 K3 d' n' Y$ T. ^) T# }
strOut=f.ReadAll ' c4 r# j1 V3 W. x: Z3 i
htmlwrite.close / r$ E& A3 B; @2 K3 u( j
& } ~: H9 M2 }( i0 y$ D5 w# C* cstrTitle="生成的网页标题"
, k# e& T2 O9 `strC 9 X" V, Y4 p+ F7 _! U! L+ e. \; R
, `8 l" g: ?* ~5 o. Z; R% _'// 用真实内容替换模板中的标记 ! p/ f6 _* V% c5 Z; ]9 ]
strOut=Replace(strOut,"$title$",strTitle) * g& K8 N8 H( M& ]. n s
strOut=Replace(strOut,"$body$",strContent) + T# A" P/ a' u
7 j* G9 u$ q0 U% Y'// 创建要生成的静态页 2 J+ _5 g3 ^! D7 |& k
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
3 D9 c' S) C4 ?) C- ^$ B
4 V& X- Y' Y( y8 ^+ S' @6 g, X'// 写入网页内容 6 n( D3 _* }3 T7 A; T) V7 z; g
htmlwrite.WriteLine strOut . u- y( _7 W' H6 l6 |" c9 M' b+ u
htmlwrite.close
8 y/ |* ~; W+ h6 i2 {+ i+ i' i- B* k+ Z) ]
Response.Write "生成静态页成功!" $ r7 N- _; g, Z1 W% f5 @6 e
, V4 B$ Z3 W& R# `, i' u; Y'// 释放文件系统对象
# H* D6 m% I2 x2 P' e$ l1 F: {set htmlwrite=Nothing , F4 r8 R& Z k' m
set fso=Nothing # n% {& L8 `, d; z1 L: v
%> ( Y: q- n! ^; g: L6 T) F, P
, R: T2 n8 @7 ]1 t" i
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 8 }" a, }7 z4 s# X3 @, ]5 y$ D2 ]6 m
<%
+ r0 j n B; i, l' Y* F0 x+ Y+ a' \1 g2 Y
'常用函数
& T3 Y& X+ P" R'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 4 i' R e9 a$ V# V& o
function getHTTPPage(url) 1 S. ]0 k+ q Y, ~ ^% `- K
dim Http
' L: ?2 n6 @3 w* y3 R9 nset Http=server.createobject("MSXML2.XMLHTTP") / `* v6 E* R5 @, Y p! O3 F
Http.open "GET",url,false
/ E* e8 r% ~) {/ h+ J0 m4 Z7 THttp.send() " e% _7 }3 E, s1 E( e5 M. X% n
if Http.readystate<>4 then
8 [2 }% c: x4 z( ^: gexit function
/ [) J* f/ k6 M$ p2 ^end if p; p3 _* Y D8 t, ^7 \1 F/ V. k3 F
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
* Z5 ]$ _# X8 Aset http=nothing
6 Z# O/ v/ k& O+ ?if err.number<>0 then err.Clear
. ^4 t' j0 h4 I, C. _end function : t Q& N6 H1 @0 B6 b9 p
6 `" P P" ?: Q' c
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
. ^9 F: y1 J9 ?8 e( EFunction BytesToBstr(body,Cset)
0 E: R2 i) U7 Idim objstream & Q3 }5 Q- g9 m3 {
set objstream = Server.CreateObject("adodb.stream") " X# D5 W; P6 X5 N Z
objstream.Type = 1 * X; s }$ _7 o4 Y, j/ H% n
objstream.Mode =3
2 o6 T, w' V! E! pobjstream.Open 4 _+ S' r% x; x# G
objstream.Write body 5 w6 P% [+ w8 G+ s8 x
objstream.Position = 0 1 w6 E5 Z: A5 O9 m& E }
objstream.Type = 2 7 G' d) H8 ^# E2 ~3 T
objstream.Charset = Cset
& a) ^2 } u. R0 @) k9 YBytesToBstr = objstream.ReadText
* t: @$ M* j( x- l5 [1 J# wobjstream.Close
' `% }5 ?) I6 |3 E3 ?+ V0 Vset objstream = nothing
7 z( F! f# ~3 Y. Q6 A. g0 `, F3 GEnd Function
$ b5 t2 B" S, I- d+ p" R2 y. |$ U! n: [
v6 M: {4 ]5 e% U* N
txtURL=server.MapPath("../index.asp") ! m5 J5 ^7 _* O' k5 }
1 i+ p: d# O; F3 \9 @4 I* ~$ U
sText = getHTTPPage(txtURL) * Q2 h2 ~8 o, V! B; Z' n2 x3 s% M0 v. x8 \
, P1 F, m+ K. a+ d& p
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
; O5 g: R/ c1 a3 afilename="../index.htm"
* t1 m( b* T; S4 K$ l% c" O, USet openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
, J: \5 O3 D: R2 a8 `5 i9 F4 UopenFile.writeline(sText) 6 L/ L5 h L* f _" Q
Set OpenFile=nothing 3 A$ W q* S7 O# D, R: V& k/ u
n7 z1 p! Y8 o/ S" j- p* ~
%>
* z0 j9 U( o# w4 K5 `; n<script>
9 @: Q& ~% q+ s) Ialert("静态网页生成完毕");
7 N/ o5 k$ ]- ~0 \9 z) E) c5 I/ a3 Chistory.back(); 9 R/ ^; t1 t# @) ?9 a
</script> |
|