ASP中查错之案例
发表时间:2024-01-10 来源:明辉站整理相关软件相关文章人气:
[摘要]有这样一个程序,是对Application集合中的元素进行活动的添加与删除,程序如下: <%@ LANGUAGE=VBSCRIPT %> <HTML> <HEAD> <TITLE>The Application Object</TITLE>...
有这样一个程序,是对Application集合中的元素进行活动的添加与删除,程序如下:
<%@ LANGUAGE=VBSCRIPT %>
<HTML>
<HEAD>
<TITLE>The Application Object</TITLE>
<STYLE TYPE="text/css">
BODY {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
INPUT {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<SPAN CLASS="heading">The ASP Application Object</SPAN><HR>
<!--------------------------------------------------------------------------->
<% 'look for a command sent from the FORM section buttons
If Len(Request.Form("cmdAdd")) Then ' 利用是否长度为0来判断
strVarName = Request.Form("txtVarName")
strVarValue = Request.Form("txtVarValue")
Application.Lock
Application(strVarName) = strVarValue ' 此处报错
Application.Unlock
End If
If Len(Request.Form("cmdRemoveThis")) Then
strToRemove = Request.Form("lstRemove")
Application.Lock
Application.Contents.Remove(strToRemove)
Application.Unlock
End If
If Len(Request.Form("cmdRemoveAll")) Then
Application.Lock
Application.Contents.RemoveAll
Application.Unlock
End If
%>
<P><DIV CLASS="subhead">The Application.Contents Collection</DIV>
<%
For Each objItem in Application.Contents
If IsObject(Application.Contents(objItem)) Then
Response.Write "Object reference: '" & objItem & "'<BR>"
ElseIf IsArray(Application.Contents(objItem)) Then
Response.Write "Array: '" & objItem & "' contents are:<BR>"
varArray = Application.Contents(objItem)
'note: the following only works with a one-dimensional array
For intLoop = 0 To UBound(varArray)
Response.Write " Index(" & intLoop & ") = " & varArray(intLoop) & "<BR>"
Next
Else
Response.Write "Variable: '" & objItem & "' = " _
& Application.Contents(objItem) & "<BR>"
End If
Next
%>
<P><DIV CLASS="subhead">The Application.StaticObjects Collection</DIV>
<%
For Each objItem in Application.StaticObjects
If IsObject(Application.StaticObjects(objItem)) Then
Response.Write "<OBJECT> element: ID='" & objItem & "'<BR>"
End if
Next
%>
<!-- collect values to execute Application methods with -->
<FORM ACTION="<% = Request.ServerVariables("SCRIPT_NAME") %>" METHOD="POST"> ' 利用Request.ServerVariables("SCRIPT_NAME")将表单提交给自身
<P><DIV CLASS="subhead">Add a value to the Application Object</DIV>
<INPUT TYPE="SUBMIT" NAME="cmdAdd" VALUE=" ">
Application("
<INPUT TYPE="TEXT" NAME="txtVarName" SIZE="15" VALUE="My_New_Value">
") = "
<INPUT TYPE="TEXT" NAME="txtVarValue" SIZE="20" VALUE="Testing, testing ...">
"<P>
<P><DIV CLASS="subhead">Remove a value from the Application Object</DIV>
<INPUT TYPE="SUBMIT" NAME="cmdRemoveThis" VALUE=" ">
Application.Contents.Remove("
<SELECT NAME="lstRemove" SIZE="1">
<%
For Each objItem in Application.Contents
Response.Write "<OPTION>" & objItem & "</OPTION>"
Next
%>
</SELECT>")<BR>
<INPUT TYPE="SUBMIT" NAME="cmdRemoveAll" VALUE=" ">
Application.Contents.RemoveAll
</FORM>
<P><DIV CLASS="subhead">Other Application Methods</DIV>
Application.Lock<BR>
Application.Unlock<P>
<!--------------------------------------------------------------------------->
<HR><SPAN CLASS="cite">©1999 <A CLASS="cite" HREF="http://www.wrox.com/">Wrox Press</A> -
<A CLASS="cite" HREF="http://webdev.wrox.co.uk/default.asp?bookcode=2610">Professional ASP 3.0</A> (ISBN: 1-861002-61-0)</SPAN>
</BODY>
</HTML>
该程序的报错信息如下:
技术信息(适用于支持人员)
· 错误类型:
应用程序对象, ASP 0102 (0x80004005)
函数需要字符串输入。
/Chapter03/application/show_application.asp, 第 22 行
· 浏览器类型:
Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; COM+ 1.0.2204)
· 页:
POST 98 bytes to /Chapter03/application/show_application.asp
· POST 数据:
cmdAdd=%A0%A0%
· A0&txtVarName=My_New_Value&txtVarValue=Testing%2C+testing+...&lstRemove=My_New_Value
· 时间:
2001年2月16日, 9:42:28
赖皮曾指出Application(strVarName) = strVarValue应为Application("strVarName") = strVarValue,虽然这样做可以通过,但于程序的原意不符,程序要做的是通过Request.Form集合来获取。而且既然是wrox的例题,应该不太会出什么问题。
步骤1:在报错的上一行加入Response.Write("AAA"),目的是检验If … Then语句是否起作用,结果发现If … Then语句起作用的。
步骤2:将出错的一句注释掉,并将Response.Write("AAA")改为
Response.Write "strVarName = " & strVarName & "<BR>"
Response.Write "strVarValue = " & strVarValue
结果为:strVarName =
strVarValue = Testing, testing ...
为什么会strVarName没有值,而strVarValue却有值呢?同样都是Text输入框,却会有不同的结果。反复观察后,发现Submit的值为" ",即为三个空格,是否会由空格引起的呢?
步骤3:遍历Request.Form集合,将两句Response.Write跟踪变量语句改为
For Each strName in Request.Form
Response.Write strName & " = " & Request.Form(strName) & "<BR>"
Next
结果为:
cmdAdd = 牋?txtVarName=My_New_Value
txtVarValue = Testing, testing ...
lstRemove = My_New_Value
果然是由于空格造成的乱码,可这源程序是例题,不应该有错呀。想到wrox用的是英文Windows 2000,我用的是中文,那么应该是由双字节的关系,就应该修改CODEPAGE。
而我忘记了中文CODEPAGE的代码,在ASP 3.0高级编程中,曾写过日文的CODEPAGE为932,日文也应该是双字节的。所以将源程序的第一行改为:
<%@ LANGUAGE="VBSCRIPT" CODEPAGE="932" %>
程序通过!
这里想向初学者提出的是,注意Response.Write和For … Each遍历来跟踪变量,找出错误的原因,注意双字节对程序的影响,还有本文中If … Then中的判断条件的方法。