ASP发布静态页面(HTML)的基本原理

  转载:http://blog.sina.com.cn/s/blog_563d5db40100apvu.html  


ASP页面每次执行都要向服务器发送请求并处理,所以现在大中型网站都把页面直接发布成HTML文件格式,这样就可能减轻服务器的负担,访问量越大优势就越明显。所以用ASP发布HTML是一个很重要的技术.本站也根据这一发展趋势把站点前台改为了ASP发布的HTML形式,当然搜索一块没有改,因为改过来之后作用也不是很大,每搜索一次发布一次,对服务器的负担并没有多少减轻,目前本站大部分版块都已经完成,只有网上调查一块还未完成,等全部完成之后会提供源程序下载。
    目前来说ASP发布HTML主要有两种方法:一种是使用JS发布,另一种是使用FSO发布,不过使用JS发布原理也是使用FSO发布。FSO发布的原理就是把要产生的HTML代码利用FSO写入一个新建文件中,当然前提发布是服务器开放了FSO.下面我们就FSO发布HTML静态页面的一些主要问题进行解析。
    FSO全称FileSystemObject(文件系统对象),功能相当的强大,理论上讲只要服务器提供FSO文件功能,那么就可以通过web程序对服务器的磁盘、文件夹、文件进行读、写、更新……操作。在这里我们对发布HTML文件,基本上就只要用到对文件进行读写更新操作就差不多了。发布的基本原理就是先建立一个FSO对象,<% set fso=createobject("scripting.filesystemobject") %>

然后利用fso的createtextfile方法建立一个可读写的文本文件(我们在这里假设建立一个show.html文件),

<% dir=server.mappath("show.html")

set fso_add=fso.createtextfile(dir,true) %>

其中createtextfile(dir,boler)方法有两个参数,dir参数表示产生文件的路径,boler参数为布尔型参数,当值为true时,如果dir文件已经存在,就覆盖原文件,值为false时,如果dir文件已经存在,则不覆盖原文件。

    产生了一个fso_add对象之后,就可以用该对象对文本文件(show.html)进行写操作,把要发布的内容一行一行的写进去,在这里要用到一个writeline()方法,例如我们往里面写html的head部分

 

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Language' content='zh-cn' /><meta http-equiv='Content-Type' content='text/html; charset=gb2312' />

<title>不想说乔丹</title></head>

 

我们就可以如下写入:

<% fso_add.writeline"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Language' content='zh-cn' /><meta http-equiv='Content-Type' content='text/html; charset=gb2312' />

<title>不想说乔丹</title></head>" %>

把HTML所有的代码全部用fso_add.writeline写入到show.html文件中,写完之后再释放fso_add和fso对象

<% fso_add.close

set fso=nothing %>

这是单个文件的发布,如果批量发布的话,要用到循环语句,让数据库内每条记录再重新发布一次或是选择一个起止ID号进行发布,并生成分页程序。

     以上是基本的FSO发布HTML文件原理,当然数据库如果不复杂的话,这种方法生成就可以,但是如果网站模块比较多,首页显示的内容分属的模块比较多,使用这种方法发布首页就会比较麻烦,每更新一次数据库就要重新完整发布一次首页,涉及的首页代码就会比较多,比较的烦,这个时候可以考虑首页使用js给合,把每一个模块的首页部分做在一个js里面,然后再在首页里面

<script type="text/javascript" src="XXXX.js"></script>

 

即可,但这样做有一个很大的问题,因为使用js调用组合,所以浏览器会一个一个js进行调用,当第一个js没有调用完不会调用显示第二个js调用,其实这样执行的效果并不高,调用效率比较差,只是这样发布首页会比较省事,更新一个模块的内容,只要更新首页该模块相对应的js文件就OK了,也是利用FSO重写js文件。所以还是使用FSO重写首页的全部内容,前台执行效率就会比较高,由刚才这一段讲述大家可以看出,使用js组合方式,后台发布会简单一些,但是前台的执行效率还是会不高,当然js一次下载之后,第二次执行的效率还是会比较高,但还是没有纯HTML执行效率高。

     发布静态页面还有一个比较麻烦的问题就是分页部分,这在看起来觉得相当的复杂,不过做起来之后也不是很复杂,只是一个多重循环。现把http://www.wgg.com.cn技术文档模块的分页部分发布代码贴在下面,并分析一下重点代码。

<%

'发布技术文档首页

page_size=37   '定义每页显示条数

set fso=createobject("scripting.filesystemobject")

set rs=server.createobject("adodb.recordset")

sql="select b_id,b_title from content order by b_time desc,b_id desc"

rs.open sql,conn,1,1

if not (rs.eof and rs.bof) then 

'计算出总页数

if rs.recordcount mod page_size=0 then

page_count=int(rs.recordcount/page_size)

else

page_count=int(rs.recordcount/page_size)+1

end if

'循环输出页数

for k=1 to page_count

                     '创建发布页面的文件路径

dir=server.mappath("../content/index_"&k&".html")

                     '建立文本文件,如文件存在,则覆盖

set fso_add=fso.createtextfile(dir,true)

                     'write_head函数是一个写文件head部分的自定义函数,该函数的作用就是写html文件的head部分和顶栏部分,该函数代码这里不再提供,跟前面所说差不多

call write_head

                     'write_left函数是一个写html文件左栏的自定义函数,该函数的作用就是写HTML文件的左栏相同部分,该函数代码这里不再提供。

call write_left

                     '利用FSO写html文件右栏广告/宣传图片部分

fso_add.writeline"<div id='center'><script src='/center_topimg.js' type='text/javascript'></script><div class='cen_div'><ul>"

'循环输出每页记录,并输出每条记录的超链接

i=(k-1)*page_size+1

while i<=k*page_size and not rs.eof

titles=trim(rs("b_title"))

titles1=changechr(titles)

titles=conleft(titles1,40)

fso_add.writeline"<li><a href='/content/show_"&trim(rs("b_id"))&".html' title='"&titles1&"'>"&titles&"</a></li>"

rs.movenext

i=i+1

wend

fso_add.writeline"</ul></div>"

'利用FSO写分页部分

fso_add.writeline"<div class='page_updown'>共"&rs.recordcount&"条记录,第"&k&"/"&page_count&"页"

if k<>1 then

fso_add.writeline"<a href='index_1.html' title='首页'><img src='/img/fri_pg.gif' alt='首页' /></a><a href='index_"&k-1&".html' title='上页'><img src='/img/pr_pg.gif' alt='上页' /></a>"

end if

if k<>page_count then

fso_add.writeline"<a href='index_"&k+1&".html' title='下页'><img src='/img/nex_pg.gif' alt='下页' /></a><a href='index_"&page_count&".html' title='尾页'><img src='/img/end_pg.gif' alt='尾页' /></a>"

end if

fso_add.writeline"</div></div>"

                     'write_copyright函数是一个写文件copyright部分(html底栏)的自定义函数,该函数的作用就是写html文件的copyright部分,该函数代码这里不再提供

call write_copyright

                      '关闭fso_add对象

fso_add.close

next

end if

rs.close

set rs=nothing

'释放fso对象

set fso=nothing

response.write"<script type='text/javascript'>alert('批量发布完成!');location.href='cont.asp';</script>"

%>

     以上就是本人对ASP发布HTML静态页面的一些初步介绍,具体再根据具体情况而定,上面已经提到了基本的原理。

    下面把http://www.wgg.com.cn技术文档后台批发布的全部源代码贴上来,这里将只提供简单的注释,各位有兴趣可以看一下:

----------------------------------------------------------------

<!-- m_top.asp是后台top-顶文件,m_chk.asp是后台验证是否登录的文件,m_left.asp是后台左栏管理快捷列表文件,fso_write.asp里面包含write_head(),write_left(),write_copyright()三个函数 -->

<!--#include file="../inc/m_top.asp"-->

<!--#include file="../inc/m_chk.asp"-->

<!--#include file="../inc/m_left.asp"-->

<!--#include file="../inc/fso_write.asp"-->

<!-- center begin -->

<div id="m_center">

<%

'发布所有技术文档页面

set fso=createobject("scripting.filesystemobject")

set rs=server.createobject("adodb.recordset")

sql="select * from content order by b_time desc,b_id  desc"

rs.open sql,conn,1,1

if not (rs.eof and rs.bof) then

for i=1 to rs.recordcount 

dir=server.mappath("../content/show_"&trim(rs("b_id"))&".html")

set fso_add=fso.createtextfile(dir,true)

call write_head

call write_left

titles=trim(rs("b_title"))

titles=changechr(titles)

content=rs("b_content")

if cint(trim(rs("b_html_encode")))=1 then

content=ubbchr(content)

else

content=changechr(content)

end if

if trim(rs("b_pic"))="" or isnull(rs("b_pic")) then

n_pic=""

else

n_pic="<a href='/b_pic/"&trim(rs("b_pic"))&"' title='相关图片' rel='external'><img src='/b_pic/"&trim(rs("b_pic"))&"' alt='相关图片' /></a>"

end if

fso_add.writeline"<div id='center'><script src='/center_topimg.js' type='text/javascript'></script><div class='cen_div'><div class='show_title'>"&titles&"</div><div class='center_t'>发布时间:"&trim(rs("b_time"))&"</div><hr />"&n_pic&content&"</div></div>"

call write_copyright

rs.movenext

next

end if

rs.close

set rs=nothing

fso_add.close

set fso=nothing

'发布首页技术文档模块内容

set fso=createobject("scripting.filesystemobject")

set rs=server.createobject("adodb.recordset")

sql="select top 16 b_id,b_title from content order by b_time desc,b_id desc"

rs.open sql,conn,1,1

if not (rs.eof and rs.bof) then

dir=server.mappath("../cont.js")

set fso_add=fso.createtextfile(dir,true)

fso_add.writeline"document.write("&chr(34)&"<ul>"&chr(34)&");"

for i=1 to rs.recordcount

titles=trim(rs("b_title"))

titles1=changechr(titles)

titles=conleft(titles1,40)

fso_add.writeline"document.write("&chr(34)&"<li><a href='/content/show_"&trim(rs("b_id"))&".html' title='"&titles1&"'>"&titles&"</a></li>"&chr(34)&");"

rs.movenext

next

if rs.recordcount<16 then

for k=1 to (16-rs.recordcount)

fso_add.writeline"document.write("&chr(34)&"<li>"&chr(34)&");"

next

end if

fso_add.writeline"document.write("&chr(34)&"</ul>"&chr(34)&");"

fso_add.close

set fso=nothing

end if

rs.close

set rs=nothing

'发布技术文档首页

page_size=37   '定义每页显示条数

set fso=createobject("scripting.filesystemobject")

set rs=server.createobject("adodb.recordset")

sql="select b_id,b_title from content order by b_time desc,b_id desc"

rs.open sql,conn,1,1

if not (rs.eof and rs.bof) then 

'得出总页数

if rs.recordcount mod page_size=0 then

page_count=int(rs.recordcount/page_size)

else

page_count=int(rs.recordcount/page_size)+1

end if

'循环输出页数

for k=1 to page_count

dir=server.mappath("../content/index_"&k&".html")

set fso_add=fso.createtextfile(dir,true)

call write_head

call write_left

fso_add.writeline"<div id='center'><script src='/center_topimg.js' type='text/javascript'></script><div class='cen_div'><ul>"

'循环输出每页记录

i=(k-1)*page_size+1

while i<=k*page_size and not rs.eof

titles=trim(rs("b_title"))

titles1=changechr(titles)

titles=conleft(titles1,40)

fso_add.writeline"<li><a href='/content/show_"&trim(rs("b_id"))&".html' title='"&titles1&"'>"&titles&"</a></li>"

rs.movenext

i=i+1

wend

fso_add.writeline"</ul></div>"

'显示分页部分

fso_add.writeline"<div class='page_updown'>共"&rs.recordcount&"条记录,第"&k&"/"&page_count&"页"

if k<>1 then

fso_add.writeline"<a href='index_1.html' title='首页'><img src='/img/fri_pg.gif' alt='首页' /></a><a href='index_"&k-1&".html' title='上页'><img src='/img/pr_pg.gif' alt='上页' /></a>"

end if

if k<>page_count then

fso_add.writeline"<a href='index_"&k+1&".html' title='下页'><img src='/img/nex_pg.gif' alt='下页' /></a><a href='index_"&page_count&".html' title='尾页'><img src='/img/end_pg.gif' alt='尾页' /></a>"

end if

fso_add.writeline"</div></div>"

call write_copyright

fso_add.close

next

end if

rs.close

set rs=nothing

set fso=nothing

response.write"<script type='text/javascript'>alert('批量发布完成!');location.href='cont.asp';</script>"

%>

</div>

<!-- center end -->

<!--#include file="../inc/copyright.asp"-->