ASP中查錯之實例 有這樣一個程序,是對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" >Wrox Press</A> - <A CLASS="cite" >Professional ASP 3.0</A> (ISBN: 1-861002-61-0)</SPAN> </BODY> </HTML>
該程序的報錯信息如下: 技術(shù)信息(適用于支持人員) · 錯誤類型: 應用程序?qū)ο? ASP 0102 (0x80004005) 函數(shù)需要字符串輸入。 /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 數(shù)據(jù): 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語句是否起作用,結(jié)果發(fā)現(xiàn)If … Then語句起作用的。 步驟2:將出錯的一句注釋掉,并將Response.Write("AAA")改為 Response.Write "strVarName = " & strVarName & "<BR>" Response.Write "strVarValue = " & strVarValue 結(jié)果為:strVarName = strVarValue = Testing, testing ... 為什么會strVarName沒有值,而strVarValue卻有值呢?同樣都是Text輸入框,卻會有不同的結(jié)果。反復觀察后,發(fā)現(xiàn)Submit的值為" ",即為三個空格,是否會由空格引起的呢? 步驟3:遍歷Request.Form集合,將兩句Response.Write跟蹤變量語句改為 For Each strName in Request.Form Response.Write strName & " = " & Request.Form(strName) & "<BR>" Next 結(jié)果為: cmdAdd = 牋?txtVarName=My_New_Value txtVarValue = Testing, testing ... lstRemove = My_New_Value 果然是由于空格造成的亂碼,可這源程序是例題,不應該有錯呀。想到wrox用的是英文Windows 2000,我用的是中文,那么應該是由雙字節(jié)的關(guān)系,就應該修改CODEPAGE。 而我忘記了中文CODEPAGE的代碼,在ASP 3.0高級編程中,曾寫過日文的CODEPAGE為932,日文也應該是雙字節(jié)的。所以將源程序的第一行改為: <%@ LANGUAGE="VBSCRIPT" CODEPAGE="932" %> 程序通過!
這里想向初學者提出的是,注意Response.Write和For … Each遍歷來跟蹤變量,找出錯誤的原因,注意雙字節(jié)對程序的影響,還有本文中If … Then中的判斷條件的方法。
|