返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 2 j; g+ P2 `: t: K+ O* g9 Z
www.aspid.cn的主站就采用了TSYS生成html文件! # k: B4 ^+ c  I* P
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. 6 T4 k! k5 e/ D, ?

6 n1 L, J1 J5 Q8 C2 Q1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <% 5 }0 m" W; K  B
filename="test.htm" 1 Q, a: O5 K& S* _" b+ m
if request("body")<>"" then 0 z9 f! \# `( P/ ^
set fso = Server.CreateObject("Scripting.FileSystemObject")
- D. E1 Q6 p. V' {set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&"")) % r1 e/ Q% K( G- w, u$ I2 ?# o
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
, R) E% A/ T8 y& i" ]4 ghtmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>" ; V; t9 Y% S% d0 t) d7 e
htmlwrite.close ( N2 `5 a/ B8 s! t; a8 v- u
set fout=nothing & T3 e; P! J3 ^8 G* M$ v; x
set fso=nothing
% X6 T3 C( U% a7 H" yend if
& M0 Y8 u) z4 j* {* O+ j%>
3 P9 z$ L5 N/ I4 b( z<form name="form" method="post" action="">
. ^: P8 E( C7 {0 t/ L<input name="title" value="Title" size=26>
+ R9 E( Y/ @5 [1 g4 I/ i<br>
; {3 y' W$ Z) M% Y4 Z% ]+ s% Y" y<textarea name="body">Body</textarea> * V3 h: K. m1 @2 B$ B0 O/ Y
<br>
0 S" F5 R: b7 ^5 S* N<br> 0 i( ?$ r' X/ Y. [, L$ y
<input type="submit" name="Submit" value="生成html"> ; W5 E% i  ^3 `5 k0 N1 n  M) u/ ?
</form>
; Y; X8 d; i1 A+ o- w# t8 o, K2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. : i. b( d6 I6 k8 w1 J
template.htm ' //模板文件 <html> / i0 }: H, y) s" Z2 g) s6 K
<head>
) o0 A: U2 ?' A. {2 M<title>$title$ by aspid.cn</title>   T6 H4 [, i) s& B! D4 Q0 e1 e
</head> * I* v: W0 j- D$ [) g! k
<body>
5 A+ [7 L) R8 P- n$ V0 }$body$ , }+ O& g* u5 j1 E$ k
</body> 1 f4 _/ q4 X7 R1 b7 n
</html> ? & \- R7 t1 u* v; Z1 P$ m) `0 m

, V3 \" R: {# C. k" h: |TestTemplate.asp '// 生成Html <%
& s5 L1 t; S+ c' X# EDim fso,htmlwrite
& ^5 ^: r! u6 `# ^Dim strTitle,strContent,strOut
% F9 N1 S4 k$ _'// 创建文件系统对象
( d# n3 r' a) _8 B2 z" @/ USet fso=Server.CreateObject("Scripting.FileSystemObject")
% a* G' f$ B8 H- W'// 打开网页模板文件,读取模板内容
4 _, _$ `, }6 ^0 H- |- e4 _Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) 3 P- `: W, b: h" \& C) O, m
strOut=f.ReadAll
$ T* H& D/ N6 {* \9 W' |htmlwrite.close
+ L7 m5 b3 A  Y3 ~' u. X$ B* {4 ~$ r, o! m
# Y& d0 }6 p; u4 S0 b1 X8 F$ zstrTitle="生成的网页标题"
7 `9 q4 y8 H0 u: T' NstrC
3 A4 P+ r7 t6 V0 ]5 H8 ]1 B( ~
* {- f8 A3 d5 U'// 用真实内容替换模板中的标记 3 `$ c4 S3 s$ u5 I8 n5 p
strOut=Replace(strOut,"$title$",strTitle) , |, V* h2 J0 `" w% E
strOut=Replace(strOut,"$body$",strContent) 9 j5 \' y/ l9 s5 z4 I

! I9 o3 v3 C$ h'// 创建要生成的静态页 * o& _! {9 k2 [- C4 c' }
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
+ ?( Q% x7 b) M7 A  D# H4 Y% _' C0 }' S+ T' q7 b- Q
'// 写入网页内容
- F# G$ {' h, w  M' xhtmlwrite.WriteLine strOut
2 F/ ]9 h' p8 ^3 F% Y8 shtmlwrite.close
0 C' ^0 c6 x* R  u
% S, l" E% c  {& F6 _Response.Write "生成静态页成功!"
( V$ j  T2 F5 r8 d0 c
# ?1 F' }8 A' }# V'// 释放文件系统对象
# m# Q% \5 F; ?5 y- Hset htmlwrite=Nothing
1 u' C! T+ p6 k# |set fso=Nothing
$ b8 G& ^! K6 o+ \$ K%>
+ T8 ?$ E* Z4 \
: \! [% Z4 B. Z2 k$ t3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
# _% f7 [% q2 u7 J<% " P* C# S" W& k: U% Q/ G) T' a

6 X$ ~4 R) c9 Q' L4 {+ K5 ^- a/ l; o% T'常用函数
0 {+ W7 ]! ?9 l5 m  d0 W'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
* K2 {& L5 T0 X; \+ R: E! P/ sfunction getHTTPPage(url)
2 q+ Y" ?3 i* adim Http
! N6 ?* V1 ]: B. Eset Http=server.createobject("MSXML2.XMLHTTP") ' j( l7 m7 {! S# r; e
Http.open "GET",url,false $ `6 c6 A+ x& ?- M% p! q) q* I3 p
Http.send() 3 c- T9 a- R. H* G
if Http.readystate<>4 then 9 o0 f' h! Z8 _1 b$ Y) x+ o
exit function
1 e& W& ?, _# q: m1 `end if
0 Z" U% B; G  T+ ]7 v# QgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") : \7 p  E% \( p! `# L( D
set http=nothing % ~6 p6 r5 Y9 Y
if err.number<>0 then err.Clear ) N. X1 x; L5 r  H1 A5 D
end function ' ]$ B, ]! o2 Y8 i

/ Q" [3 w2 a1 _! `1 U0 i'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 6 @0 y, M8 t  \
Function BytesToBstr(body,Cset)
- C' _3 ^  y: g. R2 bdim objstream , q% \( j; X( T" z7 N0 \
set objstream = Server.CreateObject("adodb.stream")
" v1 u! d; e" O9 lobjstream.Type = 1
! i0 t+ {3 Q7 _) w# Gobjstream.Mode =3 ; Y5 W. S( ?/ u" A" u: L% @) K: l
objstream.Open ) Q5 w6 i$ |1 k  w
objstream.Write body $ i" K; {1 }$ g( N2 V
objstream.Position = 0
  w2 p; |/ a8 R( eobjstream.Type = 2
! W1 c9 c: M2 G- v% m( ~3 ^* W. Eobjstream.Charset = Cset
" O8 d8 R8 X4 mBytesToBstr = objstream.ReadText
3 c; T  K4 Y5 ^' K5 tobjstream.Close % n6 g0 x1 J& P: a' j9 v. n7 F
set objstream = nothing
+ _  k4 ^, x, z! t' REnd Function . F) ?4 [+ p# I/ u' j
% _% l) N0 V' i

/ I8 i. w- K4 Y& h9 q0 ^! JtxtURL=server.MapPath("../index.asp")
  f0 q/ _/ B8 c  e2 V2 D/ O& ^& C; t0 N+ P
sText = getHTTPPage(txtURL) ; A4 O* G5 T' W2 Q& h
0 u. C; s! r$ M5 E: |
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
" Z, f( w7 h* _& R$ \8 Y5 Hfilename="../index.htm" ' m+ M/ c4 }' Y+ S
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 : B' E6 ~/ i2 S7 m
openFile.writeline(sText)
+ h' w; y) S6 z) h9 fSet OpenFile=nothing % @9 f  l9 w( V( L

4 k; d6 Q) g/ p3 ^  g; g%>
# ~8 S# ^1 m2 i& ~9 \8 c+ _, K<script>
. ]* p, @( P. F+ K- Q3 L$ galert("静态网页生成完毕");
* j/ G( d7 A! `history.back(); / `! n1 g* D( I6 j/ y
</script>

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