有这样一个程序,是对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中的判断条件的方法。