为什么要生成静态首页?
1、如果你首页读取的数据库次数比较多,速度很慢,而且占用很多服务器资源。使用静态页面访问速度当然快多了
2、搜索引擎容易搜索到
3、如果程序出问题,也能保证首页能访问。
4、其他的太多,自己想:)
应用方式:
如果你的首页是index.asp,你可以生成index.htm (默认访问顺序必须是index.htm,index.asp)。这样访问者第一次访问到你的网站的时候打开的是index.htm 。你可以把网站首页的链接做成index.asp,这样从网站任何一个页面点击首页的链接出现的就是index.asp,这样保证的信息更新的及时性(毕竟index.htm需要每次手动更新)。
方法一:
直接将首页文件包含在表单文本框中,将首页代码最为数据提交,然后生成静态页面。
代码如下:
代码如下:
<BR /><% <BR />'------------------------------------------------------------ <BR />'使用表单提交生成静态首页的代码 <BR />'确保你的空间支持FSO,且首页代码内容较少 <BR />'------------------------------------------------------------ <BR />dim content <BR />content=Trim(Request.Form("content")) <BR />if content<>"" then <BR />call makeindex() <BR />end if <BR />sub makeindex() <BR />Set Fso = Server.CreateObject("Scripting.FileSystemObject") <BR />Filen=Server.MapPath("index.htm") <BR />Set Site_Config=FSO.CreateTextFile(Filen,true, False) <BR />Site_Config.Write content <BR />Site_Config.Close <BR />Set Fso = Nothing <BR />Response.Write("<script>alert('已经成功生成首页!')</script>") <BR />end sub <BR />%> <BR /><form name="form1" method="post" action=""> <BR /><textarea name="content"> <BR /><!-- #i nclude file="index.asp" --> <BR /></textarea> <BR /><br> <BR /><input type="submit" name="Submit" value="提交"> <BR /></form> <BR /> <BR />缺点: <BR />1、如果首页中包括<@ ..>标记,会提示出错。 <BR />2、如果首页代码较长,用表单无法提交过去(表单数据长度有一定的限制)。 <BR />解决方案: <BR />1、去掉index.asp中的<@ >标记 <BR />2、使用eWebEditor,提交支持大数据(能自动分割) <BR />优点: <BR />可以在生成时对内容实时修改。 <BR />方法二: <BR />直接使用XMLHTTP获取index.asp的代码 <br /><br /><span><U></U></span> 代码如下:<BR /><% <BR />'---------------------------------------------------------- <BR />'使用XMLHTTP生成静态首页的代码 <BR />'Curl 为你的首页地址,确保你的空间支持FSO <BR />'----------------------------------------------------------- <BR />dim read,Curl,content <BR />Curl="http://www.51frw.cn/index.asp" <BR />read=getHTTPPage(Curl) <BR />if read<>"" then <BR />content=read <BR />call makeindex() <BR />end if <BR />sub makeindex() <BR />Set Fso = Server.CreateObject("Scripting.FileSystemObject") <BR />Filen=Server.MapPath("index.htm") <BR />Set Site_Config=FSO.CreateTextFile(Filen,true, False) <BR />Site_Config.Write content <BR />Site_Config.Close <BR />Set Fso = Nothing <BR />Response.Write("<script>alert('已经成功生成首页!')</script>") <BR />end sub <BR />Function getHTTPPage(url) <BR />dim http <BR />set http=Server.createobject("Microsoft.XMLHTTP") <BR />Http.open "GET",url,false <BR />Http.send() <BR />if Http.readystate<>4 then <BR />exit function <BR />end if <BR />getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") <BR />set http=nothing <BR />if err.number<>0 then err.Clear <BR />End function <BR />Function BytesToBstr(body,Cset) <BR />dim objstream <BR />set objstream = Server.CreateObject("adodb.stream") <BR />objstream.Type = 1 <BR />objstream.Mode =3 <BR />objstream.Open <BR />objstream.Write body <BR />objstream.Position = 0 <BR />objstream.Type = 2 <BR />objstream.Charset = Cset <BR />BytesToBstr = objstream.ReadText <BR />objstream.Close <BR />set objstream = nothing <BR />End Function <BR />%> <BR /> <br /><br />2、模板分离批量生成 <br /><br />模板文件中要替换的内容均以{...}括起来 <BR />为力求简洁,去掉了错误处理代码(replace中要来替换的字符串参数不能为null值,当然fso也应该做错误检查)。 <BR /><span><U></U></span> 代码如下:<BR /><% <BR />' --------------------------------------------------------------------------------------------------------------------- <BR />' 出自: kevin fung http://www.yaotong.cn <BR />' 作者: kevin fung 落伍者ID:kevin2008,转载时请保持原样 <BR />' 时间: 2006/07/05落伍者论坛首发 <BR />' ---------------------------------------------------------------------------------------------------------------------- <BR />Dim start '该变量为指针将要指向的记录集位置,通过参数动态获得 <BR />Dim Template '模板文件将以字符串读入该变量 <BR />Dim content '替换后的字符串变量 <BR />Dim objConn '连接对象 <BR />Dim ConnStr '连接字符串 <BR />Dim sql '查询语句 <BR />Dim cnt:cnt = 1 '本轮循环计数器初始化 <BR />start = request("start") '获取本轮指针的开始位置 <BR />If IsNumeric(start) Then start = CLng(start) Else start=1 <BR />If start=0 Then start = 1 '如果start <BR />ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath("DataBase.mdb") <BR />sql = "select * from table_name" <BR />Set objConn = Server.CreateObject("ADODB.Connection") <BR />objConn.Open ConnStr <BR />set rs = Server.CreateObject("ADODB.Recordset") <BR />rs.open sql,objConn,1,1 '打开数据集 <BR />rs.AbsolutePosition = start '最关键的一步,将指针指向start,start通过参数动态获得 <BR />Template = getTemplate(Server.MapPath("template.html"))' template.html为模板文件,通过函数getTemplate读入到字符串,模板文件中要替换的内容均以{...}括起来 <BR />While Not rs.eof And cnt<= 500 '500是设定一次请求生成页面的循环次数,根据实际情况修改,如果太高了,记录集很多的时候会出现超时错误 <BR />content = Replace(Template,"{filed_name_1}",rs("filed_name_1")) '用字段值替换模板内容 <BR />content = Replace(content,"{filed_name_2}",rs("filed_name_2")) <BR />...... <BR />content = Replace(content,"{filed_name_n}",rs("filed_name_n")) <BR />genHtml content,Server.MapPath("htmfiles/"&rs("id")&".html") '将替换之后的Template字符串生成HTML文档,htmfiles为存储静态文件的目录,请手动建立 <BR />cnt = cnt + 1 '计数器加1 <BR />start = start + 1 '指针变量递增 <BR />rs.movenext <BR />wend <BR />If Not rs.eof Then '通过刷新的方式进行下一轮请求,并将指针变量start传递到下一轮 <BR />response.write "<meta http-equiv='refresh' content='0;URL=?start="&start&"'>" <BR />Else <BR />response.write "生成HTML文件完毕!" <BR />End if <BR />rs.Close() <BR />Set rs = Nothing <BR />objConn.Close() <BR />Set objConn = Nothing <BR />Function getTemplate(template)'读取模板的函数,返回字符串,template为文件名 <BR />Dim fso,f <BR />set fso=CreateObject("Scripting.FileSystemObject") <BR />set f = fso.OpenTextFile(template) <BR />getTemplate=f.ReadAll <BR />f.close <BR />set f=nothing <BR />set fso=Nothing <BR />End Function <BR />Sub genHtml(content,filename)'将替换后的内容写入HTML文档,content为替换后的字符串,filename为生成的文件名 <BR />Dim fso,f <BR />Set fso = Server.CreateObject("Scripting.FileSystemObject") <BR />Set f = fso.CreateTextFile(filename,true)'如果文件名重复将覆盖旧文件 <BR />f.Write content <BR />f.Close <BR />Set f = Nothing <BR />set fso=Nothing <BR />End Sub <BR />%> <BR />
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!