动态asp网页批量生成静态html网页问题
<%
'讀取模板
set rs=server.createobject("adodb.recordset")
rs.open ("select tradeinfo_sort from worldec_Temp"),conn,1,1
pencat=rs("tradeinfo_sort")
rs.close
'讀取要生成的條數(shù)
? ?if request("page")<>"" then
? ?? ?if cint(request("page"))<1 then
? ?? ?? ?currentPage=1
? ?? ?else
? ?? ?? ?currentPage=cint(request("page"))
? ?? ?end if
? ?else? ?? ???
? ?? ?currentPage=1? ?? ???
? ?end if
? ?
'************** 讀取條數(shù) ***************
? ???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? ???'得到總條數(shù)
? ???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>"
批量生成靜態(tài)頁(yè)面
1 info_html.asp
2 info_html_sure.asp
?
info_html.asp
=========================================================
<%if session("admin")="" then
response.Write "<script language='javascript'>alert('網(wǎng)絡(luò)超時(shí)或您還沒(méi)有登陸!');window.location.href='login.asp';</script>"
response.End
else
if session("flag")>1 then
response.Write "<p align=center><font color=red>您沒(méi)有此項(xiàng)目管理權(quán)限!</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>批量生成靜態(tài)頁(yè)面----進(jìn)度條</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">
//創(chuàng)建XMLHttpRequest對(duì)象
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="開始批量生成靜態(tài)頁(yè)面";
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);
}
//更新頁(yè)面
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> 頁(yè)靜態(tài)頁(yè)面,共 <span class="red_font" id="all_page"> </span> 頁(yè)需要生成。</td>
</tr>
<tr>
<td height="30" align="center"><input name="btn" style="border:1px solid #333333;height:25px" type="button" id="btn" value="開始批量生成靜態(tài)頁(yè)面" οnclick="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('網(wǎng)絡(luò)超時(shí)或您還沒(méi)有登陸!');window.location.href='login.asp';</script>"
response.End
else
if session("flag")>1 then
response.Write "<p align=center><font color=red>您沒(méi)有此項(xiàng)目管理權(quán)限!</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對(duì)象打開數(shù)據(jù)庫(kù)的動(dòng)作,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文件寫入進(jìn)度信息'''''''''''''''''''''''''''''''''''''''''
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
''''''''''''''''''''''''''''將這些信息以逗號(hào)連接在一起,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
'''''''''''''''''''此處是要生成的靜態(tài)頁(yè)的地址''''''''''''''''''''''''''''''''''''''''''
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兩種簡(jiǎn)單的生成靜態(tài)首頁(yè)的方法
?
為什么要生成靜態(tài)首頁(yè)?1、如果你首頁(yè)讀取的數(shù)據(jù)庫(kù)次數(shù)比較多,速度很慢,而且占用很多服務(wù)器資源。使用靜態(tài)頁(yè)面訪問(wèn)速度當(dāng)然快多了
2、搜索引擎容易搜索到
3、如果程序出問(wèn)題,也能保證首頁(yè)能訪問(wèn)。
4、其他的太多,自己想:) 應(yīng)用方式:
如果你的首頁(yè)是index.asp,你可以生成index.htm (默認(rèn)訪問(wèn)順序必須是index.htm,index.asp)。這樣訪問(wèn)者第一次訪問(wèn)到你的網(wǎng)站的時(shí)候打開的是index.htm 。你可以把網(wǎng)站首頁(yè)的鏈接做成index.asp,這樣從網(wǎng)站任何一個(gè)頁(yè)面點(diǎn)擊首頁(yè)的鏈接出現(xiàn)的就是index.asp,這樣保證的信息更新的及時(shí)性(畢竟index.htm需要每次手動(dòng)更新)。 方法一:
直接將首頁(yè)文件包含在表單文本框中,將首頁(yè)代碼最為數(shù)據(jù)提交,然后生成靜態(tài)頁(yè)面。
代碼如下: <%
'------------------------------------------------------------
'使用表單提交生成靜態(tài)首頁(yè)的代碼
'確保你的空間支持FSO,且首頁(yè)代碼內(nèi)容較少
'------------------------------------------------------------ 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('已經(jīng)成功生成首頁(yè)!')</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> 缺點(diǎn):
1、如果首頁(yè)中包括<@ ..>標(biāo)記,會(huì)提示出錯(cuò)。
2、如果首頁(yè)代碼較長(zhǎng),用表單無(wú)法提交過(guò)去(表單數(shù)據(jù)長(zhǎng)度有一定的限制)。
解決方案:
1、去掉index.asp中的<@ >標(biāo)記
2、使用eWebEditor,提交支持大數(shù)據(jù)(能自動(dòng)分割)
優(yōu)點(diǎn):
可以在生成時(shí)對(duì)內(nèi)容實(shí)時(shí)修改。 方法二:
直接使用XMLHTTP獲取index.asp的代碼
<%
'----------------------------------------------------------
'使用XMLHTTP生成靜態(tài)首頁(yè)的代碼
'Curl 為你的首頁(yè)地址,確保你的空間支持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('已經(jīng)成功生成首頁(yè)!')</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、模板分離批量生成
模板文件中要替換的內(nèi)容均以{...}括起來(lái)
為力求簡(jiǎn)潔,去掉了錯(cuò)誤處理代碼(replace中要來(lái)替換的字符串參數(shù)不能為null值,當(dāng)然fso也應(yīng)該做錯(cuò)誤檢查)。
<%
' ---------------------------------------------------------------------------------------------------------------------
' 出自: kevin fung http://www.yaotong.cn
' 作者: kevin fung 落伍者ID:kevin2008,轉(zhuǎn)載時(shí)請(qǐng)保持原樣
' 時(shí)間: 2006/07/05落伍者論壇首發(fā)
' ----------------------------------------------------------------------------------------------------------------------
Dim start '該變量為指針將要指向的記錄集位置,通過(guò)參數(shù)動(dòng)態(tài)獲得
Dim Template '模板文件將以字符串讀入該變量
Dim content '替換后的字符串變量
Dim objConn '連接對(duì)象
Dim ConnStr '連接字符串
Dim sql '查詢語(yǔ)句
Dim cnt:cnt = 1 '本輪循環(huán)計(jì)數(shù)器初始化
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 '打開數(shù)據(jù)集
rs.AbsolutePosition = start '最關(guān)鍵的一步,將指針指向start,start通過(guò)參數(shù)動(dòng)態(tài)獲得
Template = getTemplate(Server.MapPath("template.html"))' template.html為模板文件,通過(guò)函數(shù)getTemplate讀入到字符串,模板文件中要替換的內(nèi)容均以{...}括起來(lái)
While Not rs.eof And cnt<= 500 '500是設(shè)定一次請(qǐng)求生成頁(yè)面的循環(huán)次數(shù),根據(jù)實(shí)際情況修改,如果太高了,記錄集很多的時(shí)候會(huì)出現(xiàn)超時(shí)錯(cuò)誤
content = Replace(Template,"{filed_name_1}",rs("filed_name_1")) '用字段值替換模板內(nèi)容
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為存儲(chǔ)靜態(tài)文件的目錄,請(qǐng)手動(dòng)建立
cnt = cnt + 1 '計(jì)數(shù)器加1
start = start + 1 '指針變量遞增
rs.movenext
wend
If Not rs.eof Then '通過(guò)刷新的方式進(jìn)行下一輪請(qǐng)求,并將指針變量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)'讀取模板的函數(shù),返回字符串,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)'將替換后的內(nèi)容寫入HTML文檔,content為替換后的字符串,filename為生成的文件名
Dim fso,f
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateTextFile(filename,true)'如果文件名重復(fù)將覆蓋舊文件
f.Write content
f.Close
Set f = Nothing
set fso=Nothing
End Sub
%>
?
ASP靜態(tài)化后,靜態(tài)html頁(yè)面點(diǎn)擊計(jì)數(shù)解決方法
實(shí)現(xiàn)方法很簡(jiǎn)單,通過(guò)在html頁(yè)面放置代碼<script type="text/javascript" src="計(jì)數(shù)器.asp"></script>即可,具本如下:
假設(shè)有個(gè)news的表,有一字段id,每一條新聞對(duì)應(yīng)1個(gè)id.另有一個(gè)hit字段,用來(lái)統(tǒng)計(jì)被點(diǎn)次數(shù)。
1、在你要統(tǒng)計(jì)點(diǎn)擊次數(shù)的html頁(yè)面,加入以下代碼(這里count.asp與本文件同錄目)
<script type="text/javascript" src="count.asp?id=新聞相應(yīng)的IP"></script>?? '這里若你是從ASP靜態(tài)化成HTML的,那新聞相應(yīng)的IP如通過(guò)調(diào)動(dòng)數(shù)據(jù)庫(kù)自動(dòng)填,使用很方便
2、將下面的代碼保存成count.asp
<!--#include file="conn.asp"-->??????? ***這里創(chuàng)建數(shù)據(jù)庫(kù)連接,自已搞了
totalhits=0
<%
sql="select * from news where id="&request.querystring("id")?? '按get來(lái)的id搜索相應(yīng)記錄
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)???? ***這句是需要在當(dāng)前頁(yè)面位置輸出點(diǎn)擊數(shù)值才用
本站靜態(tài)化后,就是通過(guò)這樣實(shí)現(xiàn)點(diǎn)擊統(tǒng)計(jì)的。
總結(jié)
以上是生活随笔為你收集整理的动态asp网页批量生成静态html网页问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: asp批量生成html静态页面方法
- 下一篇: 获得本页面URL地址