asp生成html代码调用数据库

  • 格式:docx
  • 大小:15.75 KB
  • 文档页数:7

ASP生成HTML静态页面2009-06-01 14:06ASP网站做了几个,内容比较多的一直都用伪静态HTML,这样只是达到了搜索引擎方便抓取的效果,真正的并没有降低服务器负载。

ASP生成HTML并不难,关键是要建立好模版,模版一般保存与数据库中,有的也支持存在文件中。

同样的在生成文件的命名,文件夹的建立。

1,文件夹的建立一般是当前年月命名,这就存在一个文件,如果文件夹存在则会报错,所以需要判断文件夹是否存在:代码:<%Set fso = Server.CreateObject("Scripting.FileSystemObject")if (fso.FolderExists(Server.MapPath(folder))) then'判断如果存在就不做处理else'判断如果不存在则建立新文件夹fso.CreateFolder(Server.MapPath(folder))end if%>2,文件名的生成,代码如下:<%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())%>3,要生成静态HTML,则需先建立模版,现把模版代码存于数据库中,操作如下:1),建立新数据库asp2html.mdb2),设计新数据库表c_moban字段m_id(自动编号,主关键字);字段m_html(备注类型)。

并将下列完整的代码拷贝至m_html字段代码:<html><head><meta http-equiv="Content-Type" content="text/html; charset=hz"><title> | 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>4,设计新闻信息表设计新数据库表c_news字段c_id:自动编号,主关键字字段c_title:文本类型,保存文章标题字段c_content:备注类型,保存文章内容字段c_filepath:文本类型,保持生成文件的路径地址字段c_time:日期/时间类型,默认值:Now()5,功能函数页lib.asp代码如下:<%'生成文件名的函数function makefilename(fname)fname = fnamefname = 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, ">", "&gt;")fString = replace(fString, "<", "&lt;")fString = Replace(fString, CHR(32), "&nbsp;")fString = Replace(fString, CHR(13), "")fString = Replace(fString, CHR(10) & CHR(10), "<br>")fString = Replace(fString, CHR(10), "<br>")HTMLEncode = fStringend function%>6,数据库连接页conn.asp代码如下:<%set conn = Server.CreateObject("ADODB.Connection")connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("asp2html.mdb") conn.Open connstr%>7,信息输入表单add.html页代码:<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>8,数据提交处理页addit.asp代码如下:<%'容错处理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,2rs.addnewrs("c_title")=c_titlers("c_content")=c_contentrs("c_filepath")=filepathrs.updaters.closeSet 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,1mb_code=rs1("m_html")rs1.closeset rs1=nothingconn.closeset conn=nothingc_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_codefout.close文章添加成功,<a href="showit.asp">浏览</a>9,修改数据页change.asp代码:<!--#include file="conn.asp" --><!--#include file="lib.asp" --><%id=request.querystring("c_id")%><%if request.form("submit")="change" thenc_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_idrs.Open sql,conn,3,2rs("c_title")=c_titlers("c_content")=c_contentrs("c_time")=now()rs.updaters.closeSet 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,1mb_code=rs1("m_html")rs1.closeset rs1=nothingconn.closeset conn=nothingc_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_codefout.close%><%response.redirect("showit.asp")%><%end if%><%if id<>"" thenSet rs = Server.CreateObject ("ADODB.Recordset")sql="select * from c_news where c_id="&idrs.Open sql,conn,1,1c_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>10,删除数据页delete.asp代码:<!--#include file="conn.asp" --><%c_id = request.querystring("c_id")sql = "Select * from c_news where c_id="&c_idSet rs = Server.CreateObject ("ADODB.Recordset")rs.Open sql,conn,2,3filepath=rs("c_filepath")Set fso = CreateObject("Scripting.FileSystemObject")fso.DeleteFile(Server.mappath(filepath)) Set fso = nothingrs.deleters.closeSet rs = Nothingconn.closeset conn=nothing%><%response.redirect("showit.asp")%>。