返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 3 |$ o# T3 r: j' e+ h8 a
www.aspid.cn的主站就采用了TSYS生成html文件! ( k6 k& B" P' K' v" ~
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.
: H( `- L+ t3 }7 @* x5 n( ]
- o+ H3 a2 W9 x1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
6 }- R# {) W0 M7 R; J; `filename="test.htm"
  {0 ^# d& ~% I6 Zif request("body")<>"" then 0 L6 G1 B. g( T
set fso = Server.CreateObject("Scripting.FileSystemObject")
1 _0 A! f9 p* N( o) _, l, Tset htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) 7 [1 I5 w/ j3 u
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
; I% Q# V3 r7 h: dhtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" % J$ g$ V! `9 x" P% j
htmlwrite.close ( E3 u' n7 b1 G' m
set fout=nothing
* C1 U* v7 G) ?set fso=nothing
# r! ~) W& x1 f- L* iend if
) s  q8 _  v! [6 x$ [8 @/ `; s%>
! L/ J$ D: w1 ^8 K. m" |5 x<form name="form" method="post" action=""> ; w  z) I, A2 f
<input name="title" value="Title" size=26>
. |7 v; s8 x0 M# W# i<br> 3 F! U  _( |( g# X6 x
<textarea name="body">Body</textarea>
4 B+ L3 d2 J4 a$ M<br> ) g0 d2 X7 Q' t- g; ^
<br>
0 @9 q! a, f( X, q! a7 g) v<input type="submit" name="Submit" value="生成html">
' G4 I1 T) `1 O9 o</form>
3 V- y/ m1 n- f2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
/ R% E' C% r4 y3 Q# `template.htm ' //模板文件 <html>
% s4 m; l- J3 d; v. z- X9 w+ U3 w<head>
- @7 _6 j5 C. ~- n( x/ t<title>$title$ by aspid.cn</title> & I0 w, L) \, u' s- d4 ^4 K
</head> ) w7 X0 r' {3 g. u( p/ r0 Q, t
<body>
7 U- y# A" c  u0 G$body$ . _; S, E3 b7 b5 s" x
</body>
+ w/ e0 u7 c% E9 y; i0 L; I</html> ?
! C  i' I' U3 O2 K: P. ]+ V" Z( J1 w- y6 \8 M% J: L
TestTemplate.asp '// 生成Html <%
& P: R" L- Z8 D# h7 QDim fso,htmlwrite * `/ ~; y) V+ T  B
Dim strTitle,strContent,strOut
, z& z6 u: o! u! i; f'// 创建文件系统对象
2 H, n& n' j' F6 ], N: BSet fso=Server.CreateObject("Scripting.FileSystemObject")
: \3 G% U: u. u) G" h/ ^# f0 u& y'// 打开网页模板文件,读取模板内容 % O2 m1 b. R# q# V' r1 ^
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) - p5 \) w% g/ S$ c6 u
strOut=f.ReadAll
4 g2 ~- g2 i0 L# b5 G" phtmlwrite.close
( ~6 Q/ `& h8 R: s) |7 \! n, ~! ]
strTitle="生成的网页标题"
' g( h* p6 ~! A6 {$ p3 OstrC , f) Q+ _; n" t4 K
/ x+ z% E' A% t' h' ~( r9 K
'// 用真实内容替换模板中的标记
& H8 n7 c) a! l% ]! Q: S' sstrOut=Replace(strOut,"$title$",strTitle) " V# P7 o% x/ E% G: m( y3 D6 U. b
strOut=Replace(strOut,"$body$",strContent)
  S, r& ?: P" V! {4 V+ W$ |6 {3 m, w! s5 T3 h8 a! B; f2 }
'// 创建要生成的静态页 , `6 W3 d6 p5 {
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
6 k4 b1 e8 s$ W. E. q% F0 f* D+ j9 E+ v: g
'// 写入网页内容 6 H% U& d+ C4 Q. x+ U
htmlwrite.WriteLine strOut
5 m- S+ ?5 v3 }% M+ E8 Y/ m) Yhtmlwrite.close $ S# Q( e  G- Y

2 H+ F, K- G- S; Z, X6 b9 @! y; vResponse.Write "生成静态页成功!"
, Q! E4 j. ?  S; G; e  n
# B' s2 U! \% p" e; x+ i'// 释放文件系统对象
0 ]/ k3 V. {' S2 V/ qset htmlwrite=Nothing
7 D; B0 `5 G/ A1 oset fso=Nothing
0 t/ Q! `- e, V" d8 _3 U%>
6 s" u* M. Y: A) N" D" w
+ N" b  J$ k5 ]% u  _3 Q3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. 8 l7 i5 D9 t+ s- X
<% 8 `; l( k" U4 j- u  R

5 C9 k( `; F) o- K'常用函数
1 B7 e* ]8 e" k$ {'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 3 |3 C8 ^; D, ^8 h* o
function getHTTPPage(url) * Q, X7 I5 i9 x  M  t  d
dim Http
% ]: v5 U; F7 U6 p+ Q' ]set Http=server.createobject("MSXML2.XMLHTTP")
# B+ d. T7 p4 u/ OHttp.open "GET",url,false 7 P2 y. F, L( Y$ V- k$ s) B7 A
Http.send() 1 K4 g) i6 R) C2 `$ N5 [- R3 ]
if Http.readystate<>4 then
# W6 b) v0 Z) Y6 _( u3 _- m- rexit function # K2 j; Z! Q. \  e
end if # U, T# X5 A" n7 q3 D/ k
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
$ `& M- s2 d9 l/ [# Vset http=nothing
! ^4 b3 |9 a6 o, Lif err.number<>0 then err.Clear
6 ]8 X' H8 J' B# i8 Z3 H& ^4 Fend function
/ i4 `( }' A. x/ V# l4 M- C( s6 U  ^6 x# L0 W) L& X* n
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
9 d  p$ N5 e4 L4 yFunction BytesToBstr(body,Cset)
* _8 g4 x, }% ]$ _  ddim objstream 0 I& F" k! n% S$ ]" ]# L, C) t
set objstream = Server.CreateObject("adodb.stream") ' a  k* _" e& m, R: [
objstream.Type = 1
- o& M; e8 P6 a8 r0 Aobjstream.Mode =3
8 }0 h8 q4 |8 u7 S* X) Nobjstream.Open ; y' R9 N2 S1 M6 ^
objstream.Write body
$ x( o9 P& ~; K8 {. Pobjstream.Position = 0 / c; h) V. e  i5 U0 B/ M
objstream.Type = 2 - r3 d) D' T" }4 C, z4 M
objstream.Charset = Cset ! e" e0 o  j! V& h9 f; x+ s0 d
BytesToBstr = objstream.ReadText
: {2 P* n& X0 w7 m, @; z) zobjstream.Close
* Z- R$ s0 F# ~9 Lset objstream = nothing 8 o8 U+ e# g* [" ^& v7 B8 I
End Function
, e1 d5 q8 J7 |3 E! k9 ?% H8 n
/ Y& p% O. D4 n4 z$ _2 o# b( j, m
txtURL=server.MapPath("../index.asp")
* j' m# U9 w- z7 N' }) X0 a6 s4 `- C0 m+ C4 ^
sText = getHTTPPage(txtURL)
1 ^( z# @0 }) M3 n5 E8 i" u1 ?4 m9 Z8 x- z
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
7 t' L% D! g+ p; J( S0 Wfilename="../index.htm" # i6 G% m# n5 W: J
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
: d1 f: `) I8 j2 v; H+ V. R2 OopenFile.writeline(sText) 5 p. q' _7 p2 C8 z
Set OpenFile=nothing 1 w" Y8 S7 u7 s: S* M
0 E7 e+ I1 \2 C) c& a+ j. b
%> / y( p( ?8 g. t
<script>
8 T$ b4 D  i- w4 R6 Q4 Oalert("静态网页生成完毕");
/ B/ R# p# r. u# |history.back(); 9 m' o- d) m' b- X! d
</script>

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