一年级数学竞赛试卷:asp批量生成html静态页面方法

来源:百度文库 编辑:中财网 时间:2024/04/28 05:06:54

随着网站访问量的加大,每次从数据库读取都是以效率作为代价的,很多用ACCESS作数据库的更会深有体会,静态页加在搜索时,也会被优先考虑。互联网上流行的做法是将数据源代码写入数据库再从数据库读取生成静态面,这样无形间就加大了数据库。将现有的ASP页直接生成静态页,将会节省很多。
   
方法一(其中的i=1 to 5 可以根据你需要生成ID的值修改):

      下面的例子是将、index.asp?id=1/index.asp?id=2/index.asp?id=3/这三个动态页面,分别生成ndex1.htm,index2.htm,index3.htm存在根目录下面

<%
    dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
    Html_Temp="

    "
        For i=1 To 5
        Html_Temp = Html_Temp&"
  • "
        Item_Classid = i
        FileName = "Index"&Item_Classid&".htm"
        FilePath = Server.MapPath("/")&"\"&FileName
        Html_Temp = Html_Temp&FilePath&"
  • "
        Do_Url = "http://"
        Do_Url = Do_Url&Request.ServerVariables("SERVER_NAME")&"/main/index.asp"
        Do_Url = Do_Url&"?Item_Classid="&Item_Classid
        strUrl = Do_Url
        dim objXmlHttp
        set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
        objXmlHttp.open "GET",strUrl,false
        objXmlHttp.send()
        Dim binFileData
        binFileData = objXmlHttp.responseBody
        Dim objAdoStream
        set objAdoStream = Server.CreateObject("ADODB.Stream")
        objAdoStream.Type = 1
        objAdoStream.Open()
        objAdoStream.Write(binFileData)
        objAdoStream.SaveToFile FilePath,2
        objAdoStream.Close()
        Next
        Html_Temp = Html_Temp&"
      "
          %>
          <%
          Response.Write ( "成功生成文件:" )
          Response.Write ( "
      " )
          Response.Write Html_Temp
          %>


      方法二:


      <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
      <%Dim myconn
      myconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("数据库地址")
      set conn=server.createobject("ADODB.connection")
      conn.open myconn
         set rs=Server.CreateObject("ADODB.Recordset")
         SQL= "SELECT * FROM 产品"
           rs.Open sql,conn,1,1
      dim read,Curl,content

      do while not rs.eof
      Curl="http://localhost/post/proziliao.asp?pro_id="&rs("pro_id")&""       '这里是循环地址
      read=getHTTPPage(Curl)
      if read<>"" then
      content=read
      Set Fso = Server.CreateObject("Scripting.FileSystemObject")
      Filen=Server.MapPath("../post/proziliao_"&rs("pro_id")&".html")   '这里是生成的HTML
      Set Site_Config=FSO.CreateTextFile(Filen,true, False)
      Site_Config.Write content
      Site_Config.Close
      Set Fso = Nothing
      end if
      rs.movenext
      loop

      Function getHTTPPage(url)
      dim http
      set http=Server.createobject("Microsoft.XMLHTTP")
      Http.open "post",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
      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
      %>

      1、ASP两种简单的生成静态首页的方法
      为什么要生成静态首页?
      1、如果你首页读取的数据库次数比较多,速度很慢,而且占用很多服务器资源。使用静态页面访问速度当然快多了
      2、搜索引擎容易搜索到
      3、如果程序出问题,也能保证首页能访问。
      4、其他的太多,自己想:)
      应用方式:
      如果你的首页是index.asp,你可以生成index.htm (默认访问顺序必须是index.htm,index.asp)。这样访问者第一次访问到你的网站的时候打开的是index.htm 。你可以把网站首页的链接做成index.asp,这样从网站任何一个页面点击首页的链接出现的就是index.asp,这样保证的信息更新的及时性(毕竟index.htm需要每次手动更新)。
      方法一:
      直接将首页文件包含在表单文本框中,将首页代码最为数据提交,然后生成静态页面。
      代码如下:
      <%
      '------------------------------------------------------------
      '使用表单提交生成静态首页的代码
      '确保你的空间支持FSO,且首页代码内容较少
      '------------------------------------------------------------
      dim content
      content=Trim(Request.Form("content"))
      if content<>"" then
      call makeindex()
      end if
      sub makeindex()
      Set Fso = Server.CreateObject("Scripting.FileSystemObject")
      Filen=Server.MapPath("index.htm")
      Set Site_Config=FSO.CreateTextFile(Filen,true, False)
      Site_Config.Write content
      Site_Config.Close
      Set Fso = Nothing
      Response.Write("")
      end sub
      %>







      缺点:
      1、如果首页中包括<@ ..>标记,会提示出错。
      2、如果首页代码较长,用表单无法提交过去(表单数据长度有一定的限制)。
      解决方案:
      1、去掉index.asp中的<@ >标记
      2、使用eWebEditor,提交支持大数据(能自动分割)
      优点:
      可以在生成时对内容实时修改。
      方法二:
      直接使用XMLHTTP获取index.asp的代码

      <%
      '----------------------------------------------------------
      '使用XMLHTTP生成静态首页的代码
      'Curl 为你的首页地址,确保你的空间支持FSO
      '-----------------------------------------------------------
      dim read,Curl,content
      Curl="http://www.xx0123.com/index.asp"
      read=getHTTPPage(Curl)
      if read<>"" then
      content=read
      call makeindex()
      end if
      sub makeindex()
      Set Fso = Server.CreateObject("Scripting.FileSystemObject")
      Filen=Server.MapPath("index.htm")
      Set Site_Config=FSO.CreateTextFile(Filen,true, False)
      Site_Config.Write content
      Site_Config.Close
      Set Fso = Nothing
      Response.Write("")
      end sub
      Function getHTTPPage(url)
      dim http
      set http=Server.createobject("Microsoft.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
      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
      %>


      2、模板分离批量生成


      模板文件中要替换的内容均以{...}括起来

      为力求简洁,去掉了错误处理代码(replace中要来替换的字符串参数不能为null值,当然fso也应该做错误检查)。
      <%
      ' ---------------------------------------------------------------------------------------------------------------------
      ' 出自: kevin fung http://www.yaotong.cn
      ' 作者: kevin fung 落伍者ID:kevin2008,转载时请保持原样
      ' 时间: 2006/07/05落伍者论坛首发
      ' ----------------------------------------------------------------------------------------------------------------------
      Dim start '该变量为指针将要指向的记录集位置,通过参数动态获得
      Dim Template '模板文件将以字符串读入该变量
      Dim content '替换后的字符串变量
      Dim objConn '连接对象
      Dim ConnStr '连接字符串
      Dim sql '查询语句
      Dim cnt:cnt = 1 '本轮循环计数器初始化

      start = request("start") '获取本轮指针的开始位置
      If IsNumeric(start) Then start = CLng(start) Else start=1
      If start=0 Then start = 1 '如果start

      ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath("DataBase.mdb")
      sql = "select * from table_name"

      Set objConn = Server.CreateObject("ADODB.Connection")
      objConn.Open ConnStr

      set rs = Server.CreateObject("ADODB.Recordset")
      rs.open sql,objConn,1,1 '打开数据集
      rs.AbsolutePosition = start '最关键的一步,将指针指向start,start通过参数动态获得

      Template = getTemplate(Server.MapPath("template.html"))' template.html为模板文件,通过函数getTemplate读入到字符串,模板文件中要替换的内容均以{...}括起来

      While Not rs.eof And cnt<= 500 '500是设定一次请求生成页面的循环次数,根据实际情况修改,如果太高了,记录集很多的时候会出现超时错误
      content = Replace(Template,"{filed_name_1}",rs("filed_name_1")) '用字段值替换模板内容
      content = Replace(content,"{filed_name_2}",rs("filed_name_2"))
      ......
      content = Replace(content,"{filed_name_n}",rs("filed_name_n"))

      genHtml content,Server.MapPath("htmfiles/"&rs("id")&".html") '将替换之后的Template字符串生成HTML文档,htmfiles为存储静态文件的目录,请手动建立

      cnt = cnt + 1 '计数器加1
      start = start + 1 '指针变量递增
      rs.movenext
      wend

      If Not rs.eof Then '通过刷新的方式进行下一轮请求,并将指针变量start传递到下一轮
      response.write ""
      Else
      response.write "生成HTML文件完毕!"
      End if

      rs.Close()
      Set rs = Nothing
      objConn.Close()
      Set objConn = Nothing

      Function getTemplate(template)'读取模板的函数,返回字符串,template为文件名
      Dim fso,f
      set fso=CreateObject("Scripting.FileSystemObject")
      set f = fso.OpenTextFile(template)
      getTemplate=f.ReadAll
      f.close
      set f=nothing
      set fso=Nothing
      End Function

      Sub genHtml(content,filename)'将替换后的内容写入HTML文档,content为替换后的字符串,filename为生成的文件名
      Dim fso,f
      Set fso = Server.CreateObject("Scripting.FileSystemObject")
      Set f = fso.CreateTextFile(filename,true)'如果文件名重复将覆盖旧文件
      f.Write content
      f.Close
      Set f = Nothing
      set fso=Nothing
      End Sub
      %>