asp生成静态页面专题一

asp生成静态页面专题一
2007年07月08日 星期日 21:27
ASP生成静态HTML文件的几种方法比较
作者/添加人:admin | 来源:网页教学网

将动态页面转换生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录(特别是对接受动态参数的页面).前台访问时,脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.

当然,凡事有利必有弊,生成HTML页面无形中也耗费大量的磁盘空间以存放这些静态文件,在编辑页面过程中除读写数据库外,也要读写服务器磁盘,页面样式的改动必须重新生成全部HTML文件,等等。

像很多搜索引擎,都可以提交网站的页面地址列表,动态文件的收录问题已经不算是个问题了(如google sitemap)。得失就要自己衡量把握了,但无论如何,我们还是要懂得如何操作的。这里就引用一下别人的文章说明几种常见的生成思路,供大家参考参考。

1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 。这是最原始的,优点是简单,缺点是页面的修改不方便,我一般用到的地方是利用它生成整站参数文件。(通常网站如标题,名称等配置保存在数据库,我将它生成config.asp保存这些变量调用,避免频繁访问数据库)

<%
filename="test.htm"
if request("body")<>"" then
set fso = Server.CreateObject("Scripting.FileSystemObject")
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
htmlwrite.close
set fout=nothing
set fso=nothing
end if
%>
<form name="form" method="post" action="">
<input name="title" value="Title" size=26>
<br>
<textarea name="body">Body</textarea>
<br>
<br>
<input type="submit" name="Submit" value="生成html">
</form>

2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.

template.htm ' //模板文件
<html>
<head>
<title>$title$ by webjx.com</title>
</head>
<body>
$body$
</body>
</html>
TestTemplate.asp '// 生成Html
<%
Dim fso,htmlwrite
Dim strTitle,strContent,strOut
'// 创建文件系统对象
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'// 打开网页模板文件,读取模板内容
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
strOut=f.ReadAll
htmlwrite.close
strTitle="生成的网页标题"
strContent="生成的网页内容"
'// 用真实内容替换模板中的标记
strOut=Replace(strOut,"$title$",strTitle)
strOut=Replace(strOut,"$body$",strContent)
'// 创建要生成的静态页
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)
'// 写入网页内容
htmlwrite.WriteLine strOut
htmlwrite.close
Response.Write "生成静态页成功!"
'// 释放文件系统对象
set htmlwrite=Nothing
set fso=Nothing
%>

3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。找到一段XMLHTTP生成Html的代码参考一下.

<%
'常用函数
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
end function

'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换

Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
txtURL=server.MapPath("../index.asp")
sText = getHTTPPage(txtURL)
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
filename="../index.htm"
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
openFile.writeline(sText)
Set OpenFile=nothing
%>
<script>
alert("静态网页生成完毕");
history.back();
</script>

小结,这三种方式是比较常用的生成HTML文件方法,我个比较喜欢使用第三种方式,因为页面改动时非常方便,就算动态页改动多大都好,只要重新用XMLHTTP读取生成一次即可。

实例讲解ASP生成静态页面方法
作者/添加人:admin | 来源:网页教学网
1、WITH TEMPLET意思是,生成的页面架构将采用某个已设定的模板,在此之前我的一篇教程中介绍过,希望各位在看本教程之前对ASP采用模板应熟悉下。

  2、ASP转变为HTML。不要我再说ASP转变成HTML的好处了吧,其中最值得知道的就是:静态HTML页和动态页对服务器的要求承受能力小得多,同样,静态HTML搜索几率远比动态页面的多得多。

  那么,我现在需要处理的技术问题就是:

  1、如何实现模板技术?
  2、如何实现2HTML技术?
  3、如何让模板技术与2HTML技术结合?

   一、先进行技术原理分析

  1、模板技术参看
  2、如何使得ASP页面转变为HTML?一般都会想到FSO组件,因为该组件能新建任何文件格式。

  那么其整个运行过程是怎么样的呢?

  a、提供信息输入页面进行信息收集;
  b、接受信息值先保存数据库,再FSO生成文件;
  c、技术性完成任务,显示刚被创建的HTML文件的路径地址。 该技术的实现过程中有如下几个难点:

  i、FSO生成的文件是直接放在一个大文件夹下,还是单独放在某个每日更新的子文件夹中?可能表述不准确,这样理解吧:相信通过FSO生成的文件随着时间的推移,文件会越来越多,管理也会越来越乱……通常你可能看到一些地址诸如 www.xxx.com/a/2004-5-20/200405201111.html 可以分析得出应该是建立了当前日期的文件夹。这样,一天就是一个文件夹的页面内容,查看管理也就显得比较合理。

  ii、我在试图通过以上方法建立文件夹的时候,又发现了第二个问题。第一次通过FSO建立以当前日期命名的文件夹,没有问题。当我有新的文件需要生成时,因为是同一个程序,所以,其又将会执行建立同样的文件夹。此时,FSO组件会发现该路径已存在……卡壳-_-! 继续处理,在首行添加代码:On Error Resume Next,达到自欺欺人、掩耳盗铃的效果。

  当然规矩的用法是判断文件夹的有无:
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if (fso.FolderExists(Server.MapPath(folder))) then
'判断如果存在就不做处理
else
'判断如果不存在则建立新文件夹
fso.CreateFolder(Server.MapPath(folder))
end if
%>
  iii、文件夹是建立了,文件该如何建立呢?主要也就是文件名的生成。当然这个就需要自己来写个函数,功能就是如何生成文件名:
<%
function makefilename(fname)
fname = fname '前fname为变量,后fname为函数参数引用
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename = fname & ".html"
end function
%>


  引用函数则:<%fname = makefilename(now())%>

  其实嘛,就是以年月日时分秒命名的文件。

  iv、最后,生成的文件该如何查看到?当然需要把生成文件的路径保存的数据库中,并且添加到相对应的记录集中了。当然,这在下面的数据库设计时会提及到。

  3、模板技术和2HTML技术的结合:将模板中特殊代码的值替换为从表单接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件。需要注意的是:替换应能将输入数据的格式或者支持UBB的代码彻底改变。

   二、再进行数据库设计

  目前数据库的设计需要两个表:一个是存放模板数据的;一个是存放信息内容的。 1,建立新数据库asp2html.mdb

  2、设计新数据库表c_moban

  字段m_id(自动编号,主关键字);字段m_html(备注类型)。

  并将下列完整的代码拷贝至m_html字段
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=hz">
<title>Cnbruce.Com | ASP2HTML TEST</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$cntop{LogContent}lt;/td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$cnleft{LogContent}lt;/td>
<td width="74%" bgcolor="#f3f3f3">$cnright{LogContent}lt;/td>
</tr>
</table>
</body>
</html>

  3、设计新数据库表c_news

  字段c_id:自动编号,主关键字
  字段c_title:文本类型,保存文章标题
  字段c_content:备注类型,保存文章内容
  字段c_filepath:文本类型,保持生成文件的路径地址
  字段c_time:日期/时间类型,默认值:Now()

   三、页面需求设计

  1、首先建立一个存放HTML页的文件夹

  在文件同一目录下,建立文件夹newsfile,夹子内部主要存放生成的HTML页面,当然内部还会采用程序方式建立以日期命名的子文件夹,以方便浏览以及管理。

  2、功能函数页面lib.asp
<%
'生成文件名的函数
function makefilename(fname)
fname = fname
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename=fname & ".shtml"
end function

'保持数据格式不变的函数
function HTMLEncode(fString)
fString = replace(fString, ">", ">")
fString = replace(fString, "<", "<")
fString = Replace(fString, CHR(32), " ")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "<br>")
fString = Replace(fString, CHR(10), "<br>")
HTMLEncode = fString
end function
%>

  3、数据库连接页面conn.asp

  完成数据库的字符串连接方法
<%
set conn = Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("asp2html.mdb")
conn.Open connstr
%>

  4、信息输入页面add.html

  其实很简单,就是表单嘛。注意action是跳转到addit.asp
<form action="addit.asp" method="post">
Title:<input type="text" name="c_title"><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"></textarea><br>
<input type="submit" value="Add">
<input type="reset" value="Reset">
</form>

  5、处理数据功能显示页面addit.asp

  首先是处理接受过来的数据,并将值写入数据库;接着将模板代码进行引用,并将其中特殊代码转换为接受值,最终通过FSO生成HTML页面。其中需要注意的还有,生成文件的路径地址保存至数据库表。
<%'容错处理
On Error Resume Next
%>

<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->

<%'接受传递值
c_title=request.form("c_title")
c_content=request.form("c_content")
%>

<%'生成HTML文件名,建立文件夹,指定文件路径
fname = makefilename(now()) 'makefilename为自定义函数
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>

<%'将接受值及路径保持至数据库表
sql = "Select * from c_news"
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,3,2
rs.addnew
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_filepath")=filepath
rs.update
rs.close
Set rs = Nothing
%>

<%'打开模板代码,并将其中特殊代码转变为接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop{LogContent}quot;,now())
mb_code=replace(mb_code,"$cnleft{LogContent}quot;,c_title)
mb_code=replace(mb_code,"$cnright{LogContent}quot;,c_content)
%>

<%'生成HTML页面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(Server.MapPath(folder))
Set fout = fso.CreateTextFile(Server.MapPath(filepath))
fout.WriteLine mb_code
fout.close
%>
文章添加成功,<a href="showit.asp">浏览</a>

  6、显示数据库表记录,并做指向HTML页的链接:showit.asp
<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->
<%
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news order by c_id desc"
rs.Open sql,conn,1,1
%>

<%
if rs.EOF and rs.BOF then
response.write ("暂时还没有文章,<a href=add.html>添加</a>")
else
Do Until rs.EOF
%>
<table width="758" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#000000">
<tr>
<td width="159" align="right" bordercolor="#CCCCCC" bgcolor="#CCCCCC"><%=rs("c_time")%></td>
<td width="591" bordercolor="#f3f3f3" bgcolor="#f3f3f3"><a href=<%=rs("c_filepath")%> target="a_blank"><%=rs("c_title")%></a></td>
</tr>
<tr>
<td valign="top" align="right" bordercolor="#ececec" bgcolor="#ececec">[<a href=del.asp?c_id=<%=rs("c_id")%>>Dell</a>][<a href=change.asp?c_id=<%=rs("c_id")%>>Edit</a>][<a href="add.html">Add</a>]</td>
<td valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF"><%=htmlencode(rs("c_content"))%></td>
</tr>
</table><br>
<%
rs.MoveNext
Loop
end if
%>

<%
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>

  7、修改数据内容页change.asp

  修改数据内容,同时也需要修改更新对应的HTML页面。修改其实就是重新生成文件,且文件名和之前一样,类似文件的覆盖。
<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->

<%id=request.querystring("c_id")%>

<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")

Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>

<%'打开模板代码,并将其中特殊代码转变为接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop{LogContent}quot;,now())
mb_code=replace(mb_code,"$cnleft{LogContent}quot;,c_title)
mb_code=replace(mb_code,"$cnright{LogContent}quot;,c_content)
%>

<%'生成HTML页面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>

<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>

<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>

  8、删除记录页del.asp

  同样!删除,除了删除数据库表中的记录,与其对应的HTML页面也需删除。代码如下:
<!--#include file="conn.asp" -->

<%
c_id = request.querystring("c_id")
sql = "Select * from c_news where c_id="&c_id
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,2,3

filepath=rs("c_filepath")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(Server.mappath(filepath))
Set fso = nothing

rs.delete
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>

<%response.redirect("showit.asp")%>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值