返回列表 发帖

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

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. ( ]) n6 z0 U5 s  ]; W
www.aspid.cn的主站就采用了TSYS生成html文件! 1 f8 r: f. R, l& ~) t, `
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获. / _( p' e* T& v4 x' ^" D5 R
5 S; r! f1 P6 d& b
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
' z6 c1 H- F5 xfilename="test.htm"
1 @! a8 n& d' p' b$ qif request("body")<>"" then
1 t' E+ F5 F0 F' Vset fso = Server.CreateObject("Scripting.FileSystemObject") $ O8 A8 G# O- |2 }0 @8 l+ {+ E3 M
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
8 N/ u# {, P  a4 J: b# ^htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>" 9 _; u7 ^6 f1 ?: b* [
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
- L: O1 w& q8 }htmlwrite.close 1 z& A( s( ?9 d/ {1 R
set fout=nothing
4 r/ s$ J0 ^: ^' p" M+ [set fso=nothing 6 O% d; {3 Q! p2 R3 W
end if * x( E/ K2 S$ o8 W& }! a# T8 R
%> 8 S4 @' }# v& n: W$ D: h
<form name="form" method="post" action="">
  T* G) ?4 _6 f<input name="title" value="Title" size=26>
- m, {3 b+ l$ Y' b% Z<br> % y5 r! H. ^1 ~' Y. w. A6 s
<textarea name="body">Body</textarea> 5 x8 p) ?1 S3 W3 H# S
<br> % J5 J* W6 k* ~. a1 z5 P
<br>
1 k* n# I9 M& v7 r/ P' H<input type="submit" name="Submit" value="生成html">
6 U) u, _7 t6 N; k1 D0 a</form> ) g9 J# p% B  }* F) e1 s
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法. / d% G6 Q6 {: Q% z- h5 R  S. z
template.htm ' //模板文件 <html>
1 @% v$ w/ d8 b6 d5 i<head>
8 Z, S( _1 a( n8 c<title>$title$ by aspid.cn</title> $ ~2 i* @* x+ K
</head> : |' c9 g8 ]2 a0 J/ M# ]3 y
<body>
0 f% Q8 q: i- T+ }8 L2 s) q  I$body$ . g% S& W# ?) Q: t
</body>
& X  {& f1 @9 A2 ]9 B% D</html> ?
0 e, Q! V) Q. S# X3 D1 R- h; g" ^- q2 f; q: M
TestTemplate.asp '// 生成Html <%
" e( x1 `$ x6 l5 `1 z! vDim fso,htmlwrite 3 g% F: d) ^6 f: w3 a1 }
Dim strTitle,strContent,strOut $ X4 u# `- P2 K( S+ p- u
'// 创建文件系统对象
5 y; j6 l) |! gSet fso=Server.CreateObject("Scripting.FileSystemObject")
6 c  o& A) W, e! s'// 打开网页模板文件,读取模板内容
# ^" ~8 S. r3 V' E( p0 g/ RSet htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm")) * ]' j& e- K6 a; t6 X% y
strOut=f.ReadAll * d) n+ B( J9 ?9 G: j: R
htmlwrite.close 3 m, P. W" N" |# J/ e% E
! u. ]7 a) i+ y. e! U
strTitle="生成的网页标题"
6 [! r( W6 e" |strC 6 l) `5 c' F7 j" ]2 f

% I6 U( x  i- y0 ^'// 用真实内容替换模板中的标记 / V) s( P" z+ _- u/ ^$ M7 l
strOut=Replace(strOut,"$title$",strTitle) , z2 \! j6 {& S3 i' [/ A
strOut=Replace(strOut,"$body$",strContent)
3 z5 r5 `6 F6 F" S  e( b5 N8 m# j3 @6 c! ?2 W7 V
'// 创建要生成的静态页 ' ^( n9 W' ?  |+ M1 S5 V# c$ @
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
! K6 }6 F+ D  C- b" X0 j: F8 u; ?
& U. c4 L' x. p1 z( \'// 写入网页内容 1 s4 a7 r1 z; t+ M' J
htmlwrite.WriteLine strOut 0 z- O& l. S. `
htmlwrite.close
/ Z% D( M/ h- N9 |* ?- m
7 @% H  V2 h: \2 B( BResponse.Write "生成静态页成功!"
8 h9 D$ f8 C6 D' f5 {: A& G7 K- Y: I/ p
'// 释放文件系统对象
) Y0 t3 \5 t" E$ Fset htmlwrite=Nothing
8 f8 }2 l, ~" S5 a* Q* xset fso=Nothing
9 y2 K' x+ R/ n  h1 \- @) z%>
8 {: w7 b4 @8 I& |" [. V4 R) H% `4 R2 U
3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下. ! `2 c, W/ q3 O, ~# u+ M
<%
$ v8 Y. X" j) `) w) E) E! f& w+ P, K) i3 {( T. @7 k
'常用函数 % ]* E3 L) R" O
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
* E. B5 i; B; ]0 P4 L& Gfunction getHTTPPage(url) : G! y1 o! ?, F, r
dim Http
# Z+ E8 l; U* A6 L' z8 G$ zset Http=server.createobject("MSXML2.XMLHTTP")
# t6 y/ X- O; ^0 \- T6 pHttp.open "GET",url,false
* H: y: p: T  p9 s8 f* @$ M0 ^" {Http.send() ! [. o  }+ B+ \# ]( t
if Http.readystate<>4 then , u7 z4 r+ n, c& V
exit function
  T4 j( X8 e- ]7 W. n2 lend if
. H- G! p: j. K& R% z! {& UgetHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 0 a  K2 i# o7 _5 d6 b+ G8 l
set http=nothing
, W3 G/ q, ?1 ^0 x0 J8 Pif err.number<>0 then err.Clear , Z- H+ m! \, d
end function / I  M* h# n9 q  g

4 o# ]+ X4 j; d9 |7 d/ b' k# O4 F'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换 : `+ P: U# B) u- H8 Z& [
Function BytesToBstr(body,Cset)
# S. S8 _) U, {0 ^/ \dim objstream
- r9 U) d" s4 L) A8 Yset objstream = Server.CreateObject("adodb.stream")
( r/ s" s! Z1 U' C$ c7 j% Oobjstream.Type = 1
, A: Q1 T8 v: _" V! ]) Iobjstream.Mode =3 & s# J7 L( [+ @* h
objstream.Open
9 r  P* {" D" R9 G* Cobjstream.Write body 3 r$ E; p' Z( J3 Y" g$ s' I; V
objstream.Position = 0
; _; c) Y7 B8 [( Dobjstream.Type = 2
+ v7 [, l1 w2 f7 W" R# qobjstream.Charset = Cset
0 @) u6 z2 d2 y, M8 _- M9 j# BBytesToBstr = objstream.ReadText
, i% B! H" z$ z# h- i7 u' Pobjstream.Close 3 A3 b" W$ z5 c' W# e: }# T  s
set objstream = nothing
6 j$ o5 y1 M2 c7 _1 b5 ZEnd Function / T1 C% K' X- w3 q$ {, `3 L' D) ?

$ O! l  l4 E* W8 p: \7 z9 f" K* Z+ N! ~5 w
txtURL=server.MapPath("../index.asp") . l$ L! f9 D. S# Q

( P4 g4 D' B- t3 @) O; R4 ~, x) ~# HsText = getHTTPPage(txtURL)
+ l$ ^) r- W. q: F  n3 s+ H3 e' c0 s1 U3 C: a. `
Set FileObject=Server.CreateObject("Scripting.FileSystemObject") 0 t* m0 a4 `* O4 z( N
filename="../index.htm" 5 y$ L4 Q- @) y9 V% _3 E6 h- ^+ T
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立 ! q+ l2 Z7 m- z
openFile.writeline(sText)
' P  K* I2 l7 _  z: C, u  Q0 f5 h" QSet OpenFile=nothing
6 e! C& p* M' L4 l* ]+ [
9 y% C' C' H2 S& A%>
% c% [' E1 V" }0 K5 `<script> 8 X; f3 D2 s& E  R' Y  W0 N
alert("静态网页生成完毕"); / c# B- F6 K- C" e3 P& _
history.back(); 3 s) R/ \5 ~( |3 K1 |+ D
</script>

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