response重定向后为何无法调用过程而能直接使用代码(展开的过程)

hwjfshx 2008-01-18 09:35:59
conn.asp
<%
Option Explicit
On Error Resume Next
Dim rs,conn,connstr,dbpath
dbpath="database/exam.mdb"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbpath)
conn.Open connstr
If Err Then
response.write "<font size=""4"">·数据库连接错误....</font>"
response.End
End If

sub closeDb()
if not conn is nothing then
if not rs is nothing then
rs.close
Set rs = nothing
End if
conn.close
Set conn = nothing
End if
End sub
%>

我在test.asp文件处如下调用
<!--#include file="conn.asp" -->
<%
closeDb()
response.Write("<script>javascript:location='reg.asp';</script>")
%>
访问test.asp后,跳转至reg.asp(reg.asp不曾连接数据库),发现与数据库的连接不曾关闭

改动test.asp如下(把过程直接展开)
<%
response.Write("<script>javascript:location='reg.asp';</script>")
if not conn is nothing then
if not rs is nothing then
rs.close
Set rs = nothing
End if
conn.close
Set conn = nothing
End if
%>
访问test.asp后,跳转至reg.asp,且与数据库的连接正常关闭,展开的过程不论放在重定向前面或后面,都不影响效果
不明白其中原委,特来求助
...全文
121 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
什么都不能 2008-01-19
  • 打赏
  • 举报
回复
asp 服务流程

1服务器代码 解释执行
2传输html数据流
3浏览器解析html数据流
4运行js代码

了解了上述过程自然就会很明白
你的关闭代码是服务器端代码 首先执行
你的locaiton='xxxxx' 属于html流 随着html的其他内容传输到浏览器
浏览器解释执行location='xxxxx',地址转向
所以你放在哪里都是一样的。
hwjfshx 2008-01-19
  • 打赏
  • 举报
回复
我判断连接关闭是用access数据库...的ldb文件,当数据库被打开时,access会产生ldb文件,关闭后则自动销毁(- -这个...应该能做依据吧)

用response.redirect或write输出脚本转向时
若在执行非输出脚本动作时(如关闭数据库,或一些查询等),效果一样
而当转向前还有执行输出脚本的话(如write("<script>javascript:alert('something');</script>"),使用write时才能看到效果,放在后面,则二者都不会执行

我若将关闭数据库的操作直接放在转向(redirect或用write输出脚本转向)代码的前后都能正常执行
而将关闭的操作放在conn.asp中的一个sub中,而后在转向处直接调用时,不执行过程...这是我不解的地方
sunlinwh 2008-01-19
  • 打赏
  • 举报
回复
1. 你是怎样判断连接没有关闭的?
2. 你用输出脚本的方式转向,这就要等你的asp代码执行完毕了才会有客户端的转向动作,所以与前后无关。
如果你是用response.redirect转向有此现象,就可有此一问。
hwjfshx 2008-01-19
  • 打赏
  • 举报
回复
to hanpoyangtitan:

谢谢,问题解决了
问题是:我调用test.asp时,rs只是一个变量,而不是对象,在关闭数据库连接时,被Err截获而被忽略了.
判断IsObject(rs)就可以了
什么都不能 2008-01-19
  • 打赏
  • 举报
回复
改一下,不要用on error resume next
改为
Option Explicit
Dim rs,conn,connstr,dbpath
dbpath="database/exam.mdb"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbpath)
conn.Open connstr
If conn.state<>1 Then
response.write " <font size=""4""> ·数据库连接错误.... </font> "
response.End
End If


sub closeDb()
if not isNull(conn) and conn.state=1 then
conn.close()
set conn=nothing
elseif not isnull(conn) and conn.state<>1 then
set conn=nothing
end if
End sub
No_Data_Found 2008-01-19
  • 打赏
  • 举报
回复
调用过程是不是要用 call?
hwjfshx 2008-01-19
  • 打赏
  • 举报
回复
to hanpoyangtitan:

首先谢谢你将流程告诉我,受益颇多.

说说我最不明白的地方如下:
<!--#include file="conn.asp" -->
<%
closeDb()
response.Write(" <script> javascript:location='reg.asp'; </script> ")
%>
调用过程closeDb()时,无法关闭数据库连接;
而直接将closeDb()替换成"关闭连接的语句",却能正常关闭数据库连接

调用过程也是服务器代码吧?为何没有调用?...不解

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧