返回列表 发帖

ASP生成静态Html文件技术汇总

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
: w6 t/ o* h. F3 z2 hwww.aspid.cn的主站就采用了TSYS生成html文件! / s8 u6 G$ E0 d& L$ k
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 5 q0 E5 c! p7 ]$ V4 N

; H8 W" j4 ^3 F9 S( \+ i. q) C1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
$ `3 T# l. {! j  C6 B) Wfilename="test.htm" % }0 C# V: ^/ ^& s/ O/ u" ?
if request("body")<>"" then
0 @) P4 }9 t) T) p) ~set fso = Server.CreateObject("Scripting.FileSystemObject")
. M- P0 N* Z8 q- O( v' Cset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) " T4 Z; M& O7 W# I0 Y
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 6 P+ _, E8 ?3 P8 ?5 r+ c
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
' [3 }' q( F; Jhtmlwrite.close ; g. d3 Y: a& m
set fout=nothing
6 |" [) J! `% M! G) v' T4 ?set fso=nothing
5 N$ G8 d6 N3 T! t. L, d$ xend if ) x, i6 Z  i: u% i5 i# I, M. C
%> % R1 x0 L  c9 X+ b" s$ i, J$ L0 k
<form name="form" method="post" action="">
' |  X# ]$ \: S5 B% M+ q" n<input name="title" value="Title" size=26>
; E7 z  |) O: B<br>
' U+ V# v! {: D6 I# ]9 m<textarea name="body">Body</textarea> 2 I2 M& d% n. j; D! X
<br> " V, x% O; V/ ~5 |/ a7 I: s) y. t" ?
<br> ( p; @0 P+ H: W
<input type="submit" name="Submit" value="生成html"> $ b7 g$ ?4 J5 I( |7 b2 h  l
</form> 8 b" U3 G. r; S& g: }* L6 k! H
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
- @" @3 x, S2 ^3 r7 M% Ttemplate.htm ' //模板文件 <html>
2 `5 z$ e! |5 x# Y( `<head>   T3 E+ ~4 k) z4 H. I2 i
<title>$title$ by aspid.cn</title>
! r9 s3 L% Y5 P8 w* `: ^</head>
, F  @0 F6 `5 j' ^<body>
+ i7 r2 B4 @4 Y5 g, Q3 o# ?% j$body$
& T# t" n# F* O9 Z. a/ e$ h</body>
; V* G# V) ?% d, K# d, l</html> ?
, N4 N& M# ?/ x3 ]# D0 r! `: n1 x5 _' V8 j$ S% i! t
TestTemplate.asp '// 生成Html <% . Q5 ^6 v% o# E0 ^) ?. T6 ?
Dim fso,htmlwrite
5 T. B' {2 f+ w7 ~! |Dim strTitle,strContent,strOut
3 Z. C! v; h! a% m4 y' G'// 创建文件系统对象 $ f$ W4 A0 L: R) D7 V
Set fso=Server.CreateObject("Scripting.FileSystemObject")
0 X3 p6 L% ]# o'// 打开网页模板文件,读取模板内容
7 `3 B- p' x  X* ^( ~( B3 E5 ]1 zSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))   z2 K" H2 e$ d& n  G* D* k* y
strOut=f.ReadAll
/ w4 L& f2 ]7 ^9 xhtmlwrite.close
+ N( H: N8 B4 P7 E+ `; J
3 U9 l' [6 |5 {' u* f+ i& X( e4 [strTitle="生成的网页标题"
/ J3 O6 `8 Z3 r/ K3 \9 BstrC
( f3 j0 ]: j0 n( K0 Y- l
2 ?1 X! w  ^+ q'// 用真实内容替换模板中的标记
- o  v4 S, I7 T: E3 X! x- UstrOut=Replace(strOut,"$title$",strTitle)
7 B5 a. x& o! n: U: IstrOut=Replace(strOut,"$body$",strContent)
% c8 q/ q$ g: a# V; U5 a5 @8 m# v: r
'// 创建要生成的静态页 / K6 B& a. V; [. _% r
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true) % ~! X' ]- @; V  X
* @# i- \  c0 D' L& @
'// 写入网页内容 0 J0 E; D& P7 L
htmlwrite.WriteLine strOut   h$ C3 A" l: \, {, U1 `# t
htmlwrite.close   U5 A+ j  V; W+ w7 |

9 c6 Y* H7 a' |* p% _- [Response.Write "生成静态页成功!"
* e3 A  ]2 k4 o3 E7 `6 D6 H
. v5 H3 D2 `- h. i+ J! Y$ U% u'// 释放文件系统对象
9 g7 ]0 a  G" e% b2 }/ E7 Wset htmlwrite=Nothing 5 }. ~+ u, z. e7 o, j" D7 q
set fso=Nothing
2 w! P3 Q# u+ e" T3 f; r/ B%> / Z9 ^: O2 I4 H: Y' h

0 N, Q+ `. i. N( h4 i  G5 f& l3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
! ], v- ?  m4 X$ v, j. R3 l" X; C) v<%
5 j' `  W! L% j8 F5 T2 H& F+ D
/ v' @$ O0 y5 ]* n' L0 V'常用函数 $ x3 _4 ~0 d5 b9 l* |' Y' b4 _" e
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 ' ]7 Q9 Z+ v8 {3 W6 i, y  h
function getHTTPPage(url)
4 x9 c! B6 q  q2 b4 {3 sdim Http
9 o! S% }. K: \# a0 q+ K/ ?set Http=server.createobject("MSXML2.XMLHTTP") % Z2 h" p; F( J+ T" C
Http.open "GET",url,false
! s1 B: h2 c. ?; m  mHttp.send()
' b" C1 \, a, N% i# e5 ~, N. Aif Http.readystate<>4 then * W! F# W7 a4 h% ^/ M
exit function + c; d! M8 B6 S) |- f
end if
# k% |* v! T6 s4 bgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
; }( S5 ^# @, A6 t6 |( Kset http=nothing 2 F* b9 ?7 Z1 P4 g, ?* w
if err.number<>0 then err.Clear
. L- Y5 ?/ V: v  v5 V0 N7 mend function
  v3 `9 A" ~3 A$ |) ?1 e, N+ f' m4 P
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
; V2 h7 @3 M; ], UFunction BytesToBstr(body,Cset) + l' O! i$ h$ v" L0 b) y- v4 I
dim objstream 2 \! y9 e* T2 k2 U! V5 y
set objstream = Server.CreateObject("adodb.stream")
/ V( b/ {. ]7 c7 t- ~objstream.Type = 1 ) t, a4 F* I4 a( A3 s0 A: s% j; ?; w
objstream.Mode =3
9 q  F* `9 e8 Q" pobjstream.Open
: s/ Y6 D" M; ]: m( Jobjstream.Write body
4 S+ a: t, |; v. M& g3 |objstream.Position = 0
4 d2 i- Q$ O( Y! Sobjstream.Type = 2
. ~, n, g4 g% ~( ?objstream.Charset = Cset
- j: h9 B+ F% i- q( ^" d% U5 \BytesToBstr = objstream.ReadText
/ A( E' x: ]! U# z- @; Jobjstream.Close . F9 s% C1 d; W, m6 N5 ^
set objstream = nothing
8 Q; Y2 ]/ F: t: ~+ iEnd Function & R7 h/ p% r5 z8 F

5 ?- `2 m( P& s# ^! r
1 c+ ]' E0 ]3 [9 \- |! N. rtxtURL=server.MapPath("../index.asp") 7 {: e2 J* B( P

! G6 w2 ^8 L. T, w5 _# w, p& Z. A% ZsText = getHTTPPage(txtURL)
( U" S7 y* Y' F$ Q' `$ ]% y6 a, u& @+ B: F5 c, }8 r7 q: u7 f
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
2 `( C" O" l; n3 Efilename="../index.htm" " H; ^$ |+ W& J0 ~# ?% {
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 % \, m) J$ J4 }' E
openFile.writeline(sText)
/ w1 `& R0 C9 b& ]$ n* |5 B- CSet OpenFile=nothing & a  u5 W. A" `. X
8 [; ]' u$ y! Z+ N3 ?
%> ( ~0 [4 B# s7 T# z9 J0 D0 K! ?8 S7 c# e$ H
<script> 0 v  K* v! D( T: K  b
alert("静态网页生成完毕");
- J$ e, R& T2 W6 ghistory.back(); - i9 I2 x  V6 g  O; K8 F
</script>

返回列表
【捌玖网络】已经运行: