您的位置:首页 > 编程语言 > ASP

动态asp网页批量生成静态html网页问题

2009-05-01 16:48 627 查看
<%
'读取模板
set rs=server.createobject("adodb.recordset")
rs.open ("select tradeinfo_sort from worldec_Temp"),conn,1,1
pencat=rs("tradeinfo_sort")
rs.close
'读取要生成的条数
if request("page")<>"" then
if cint(request("page"))<1 then
currentPage=1
else
currentPage=cint(request("page"))
end if
else
currentPage=1
end if

'************** 读取条数 ***************
MaxPerPage=1
if request("all")="all" then
set rs=server.createobject("adodb.recordset")
rs.open ("select * from class_1 order by sortID desc"),conn,1,1
end if
if not rs.eof then
rs.pagesize=MaxPerPage
mpage=rs.pagecount '得到总条数
rs.move (currentPage-1)*MaxPerPage
if mpage>=currentPage then
sortid=rs("sortid")
sortname=rs("sort")
end if
end if
rs.close

L_BODY=L_BODY&"<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"">"
L_BODY=L_BODY&"<TR>"
sqltype="select * from Class_2 where sortid="&sortid&""
set rstype=conn.execute(sqltype)
if rstype.eof then
L_BODY=L_BODY&"<br>No secondary industry classification<br><br>"
else
j=1
do while not rstype.eof
L_BODY=L_BODY&"<TD valign=top align=center>"
L_BODY=L_BODY&"<table border=0 cellspacing=""0"" cellpadding=""2"" width=95%/>"
L_BODY=L_BODY&"<tr><td align=""left"" height=25 valign=top width=2%/><img src=""../../images/icon_4x7_arrow2.gif"" width=""4"" height=""12""></td>"

批量生成静态页面
1 info_html.asp
2 info_html_sure.asp

info_html.asp
=========================================================
<%if session("admin")="" then
response.Write "<script language='javascript'>alert('网络超时或您还没有登陆!');window.location.href='login.asp';</script>"
response.End
else
if session("flag")>1 then
response.Write "<p align=center><font color=red>您没有此项目管理权限!</font></p>"
response.End
end if
end if
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>批量生成静态页面----进度条</title>

<style>
div,body{font-size:12px}
#load{z-index:1;width:500px;height:11px;border:1px #82AF2C solid;}
#loading{position:absolute;z-index:2;height:11px; background-color:#B4C96B; overflow:hidden;}
#div1{position:absolute;z-index:3;width:100%;height:100%;line-height:13px;text-align:center;}
.red_font {color: #FF0000}
.green_font {color: #669900}
</style>
</head>

<body>

<script language="javascript">
//创建XMLHttpRequest对象
var temp_id;
var request = false;
try {
request = new XMLHttpRequest();
}
catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed) {
request = false;
}
}
}
if (!request)alert("Error initializing XMLHttpRequest!");
//获取信息
function kaishi(btn)
{
btn.disabled = true;
btn.value = "批量生成中..";
document.bg.location.href="info_html_sure.asp";
temp_id=setInterval("getInfo()",1000)
document.getElementById("tong_ji").style.display="";

}
function stop_read()
{
clearInterval(temp_id);
document.getElementById("btn").value="开始批量生成静态页面";
document.getElementById("btn").disabled= false;
}
function getInfo() {
var url = "/news_html.txt?id=" + Math.random();
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
//更新页面
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText.split(",");

document.getElementById("div1").innerText = response[0];
document.getElementById("loading").style.width = response[0];
document.getElementById("now_page").innerText = response[1];
document.getElementById("all_page").innerText = response[2];
if(response[0]=="100.00%")
{
stop_read();

}
}
else if(request.status == 404) {
alert ("Requested URL is not found.");
}
else if (request.status == 403) {
alert("Access denied.");
}
else
alert("status is " + request.status);
}
}
</script>

<table width="511" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="511" height="50"> </td>
</tr>

<tr>
<td height="96">  <div id="load"><div id="loading"></div><div id="div1"> </div>
</div></td>
</tr>
<tr>
<td height="75" align="center" valign="top" class="green_font" id="tong_ji" style="display:none">已生成 <span class="red_font" id="now_page"> </span> 页静态页面,共 <span class="red_font" id="all_page"> </span> 页需要生成。</td>
</tr>
<tr>
<td height="30" align="center"><input name="btn" style="border:1px solid #333333;height:25px" type="button" id="btn" value="开始批量生成静态页面" onclick="kaishi(this);" /></td>
</tr>
<tr>
<td height="150" align="center"> </td>
</tr>
</table>
<iframe width="0" height="0" id="bg" name="bg" ></iframe>
</body>
</html>

info_html_sure.asp
==================================================================
<%
if session("admin")="" then
response.Write "<script language='javascript'>alert('网络超时或您还没有登陆!');window.location.href='login.asp';</script>"
response.End
else
if session("flag")>1 then
response.Write "<p align=center><font color=red>您没有此项目管理权限!</font></p>"
response.End
end if
end if

session("help")=int((10-1+1)*Rnd + 1)
server.ScriptTimeout=9999
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
''''''''''''''''''''''''''''''''''
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
''''''''''''''''''''这里是conn对象打开数据库的动作,asp代码需要自己改造,本文不作解释'''''''''
set conn=server.createobject("adodb.connection")
str="Driver={Microsoft Access Driver (*.mdb)};DBQ=" & server.mappath("/")&"/datec/#mtccc.asp"
conn.open str
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
set tp=server.CreateObject("adodb.recordset")
tp.open "select newsid from inform",conn,1,1
if not tp.bof and not tp.eof then
for ixi=1 to tp.recordcount
if not tp.bof and not tp.eof then
call make_html(tp("newsid"))
''''''''''''''''''''''在此往gaga.txt文件写入进度信息'''''''''''''''''''''''''''''''''''''''''
set temp_fso=Server.CreateObject("Scripting.FileSystemObject")
Set temp_file=temp_fso.OpenTextfile(server.MapPath("/")&"/news_html.txt",2,true) 'true为不存在自行建立
temp_file.writeline(formatnumber((ixi/tp.recordcount)*100,2)&"%,"&ixi&","&tp.recordcount)
Set temp_file=nothing
''''''''''''''''''''''''''''将这些信息以逗号连接在一起,ajax读取后js再分开'''''''''''''
tp.movenext
else
exit for
end if
next

end if
tp.close
set tp=nothing
function make_html(chuan_id)
temp_id=chuan_id
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from inform where newsid="&temp_id,conn,1,1
if not rs.bof and not rs.eof then

'''''''''''''''''''此处是要生成的静态页的地址''''''''''''''''''''''''''''''''''''''''''
txtURL="http://"&Request.ServerVariables("server_name")&"/info_html_jt.asp?id="&temp_id
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
sText = getHTTPPage(txtURL)
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
filename=server.MapPath("/")&"/news/info_"&temp_id&".html"

Set openFile=FileObject.OpenTextfile(filename,2,true) 'true为不存在自行建立
openFile.writeline(sText)
Set OpenFile=nothing
rs.close
set rs=nothing
end if
end function
function checksql(kustr)
checksql=server.htmlencode(replace(replace(replace(kustr,"<>"," "),"* from "," "),"="," "))
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("<script>alert('已经成功生成首页!')</script>")
end sub
%>
<form name="form1" method="post" action="">
<textarea name="content">
<!-- #i nclude file="index.asp" -->
</textarea>
<br>
<input type="submit" name="Submit" value="提交">
</form>
缺点:
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("<script>alert('已经成功生成首页!')</script>")
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 "<meta http-equiv='refresh' content='0;URL=?start="&start&"'>"
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
%>

ASP静态化后,静态html页面点击计数解决方法

实现方法很简单,通过在html页面放置代码<script type="text/javascript" src="计数器.asp"></script>即可,具本如下:

假设有个news的表,有一字段id,每一条新闻对应1个id.另有一个hit字段,用来统计被点次数。

1、在你要统计点击次数的html页面,加入以下代码(这里count.asp与本文件同录目)
<script type="text/javascript" src="count.asp?id=新闻相应的IP"></script> '这里若你是从ASP静态化成HTML的,那新闻相应的IP如通过调动数据库自动填,使用很方便

2、将下面的代码保存成count.asp
<!--#include file="conn.asp"--> ***这里创建数据库连接,自已搞了
totalhits=0
<%
sql="select * from news where id="&request.querystring("id") '按get来的id搜索相应记录
set rst=server.CreateObject("adodb.recordset")
rst.open sql,conn,1,3
rst("hit")=rst("hit")+1
rst.update
%>

totalhits=<%=rst("hit")%>

<%rst.close
set rst=nothing
%>

document.write (totalhits) ***这句是需要在当前页面位置输出点击数值才用

本站静态化后,就是通过这样实现点击统计的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: