HOW TO: Use an ASP Debug Object to Debug ASP Pages

Article translations Article translations
Article ID: 299986 - View products that this article applies to.
This article was previously published under Q299986
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step guide provides Active Server Pages (ASP) code that Web developers can use to debug ASP applications. This article demonstrates how to build your own custom "debug object," which is an ASP page that is written in JScript or VBScript. This is a simple, lightweight alternative to using the Microsoft Visual InterDev debugger. It requires no server configuration and enables you to check the value of changing variables in your code, as well as display the values of all ASP collections and variables.

Although the Visual InterDev debugger is the most powerful tool for debugging ASP pages, there are a number of scenarios in which it cannot be used. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
288965 HOWTO: Create a VBScript Class or JScript Object to Debug ASP Pages

Requirements

  • Microsoft Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server
  • Microsoft Internet Information Server (IIS) 5.0
NOTE: If you using an earlier version of Windows (prior to Windows 2000), make sure you have Microsoft Windows Script version 5.1 or later installed. Although the features that are described in this article also work on Windows NT with IIS 4.0 installed and configured, this sample assumes that you are using Windows 2000.

Prepare the Web Site

  1. Right-click the Windows Start menu, and then click Explore.
  2. In Windows Explorer, click to select your Web server's root folder, which is typically C:\Inetpub\Wwwroot\.
  3. From the File menu, point to New, and then click Folder. Type DebugTest to name the new folder.
  4. Right-click the new DebugTest folder, and then click Properties.
  5. On the Security tab, add the Everyone group, and select the Read and Write check boxes to allow these permissions to the Everyone group. Click Apply and then click OK to accept the changes.

    NOTE: Use these permissions for this development sample only. Consult your Network administrator to determine your specific security settings.
  6. On the Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager.
  7. Under Internet Information Services, double-click to expand the entry for the local server.
  8. Right-click the Default Web Site, point to New, and then click Virtual Directory. In the wizard, follow these steps:
    1. When prompted, type DebugTest in the Virtual Directory Alias text box, and then click Next.
    2. When you are prompted to type the Web Site Content Directory, click Browse, select the newly created DebugTest directory, and then click Next.
    3. When you are prompted to select Access Permissions, select Read and Run scripts (such as ASP). No other access permissions are necessary for this example. Click Next to complete the wizard.
  9. Double-click to expand the Default Web Site, right-click DebugTest, and then click Properties.
  10. On the Directory tab, verify that the Web name (as entered in step 8a) is listed in the Application Name text box under Application Settings. If it is not, click Create to create the application.
  11. Close the Properties dialog box and Internet Information Server.

Create a VBScript Debug Object

This section demonstrates how to create the Debug ASP page, named VBScriptDebug.asp. Furthermore, a sample ASP page named MyVBScript.asp demonstrates how to incorporate the Debug object into any ASP page through a server-side include file.

VBScriptDebug.asp

  1. On the Start menu, point to Programs, point to Accessories, and then click Notepad.
  2. Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file:
    <SCRIPT RUNAT=Server Language=VBScript>
    
    Class clsDebug
    
    Dim blnEnabled
    Dim dteRequestTime
    Dim dteFinishTime
    Dim objStorage
    
    Public Property Get Enabled()
       Enabled = blnEnabled
    End Property
    
    Public Property Let Enabled(bNewValue)
       blnEnabled = bNewValue
    End Property
    
    Private Sub Class_Initialize()
       dteRequestTime = Now()
       Set objStorage = Server.CreateObject("Scripting.Dictionary")
    End Sub
    
    Public Sub Print(label, output)
       If Enabled then
           objStorage.Add label, output
       End if
    End Sub
    
    Public Sub [End]()
       dteFinishTime = Now()
       If Enabled then
         PrintSummaryInfo()
         PrintCollection "VARIABLE STORAGE", objStorage
         PrintCollection "QUERYSTRING COLLECTION", Request.QueryString()
         PrintCollection "FORM COLLECTION", Request.Form()
         PrintCollection "COOKIES COLLECTION", Request.Cookies()
         PrintCollection "SESSION CONTENTS COLLECTION", Session.Contents()
         PrintCollection "SERVER VARIABLES COLLECTION", Request.ServerVariables()
         PrintCollection "APPLICATION CONTENTS COLLECTION", Application.Contents()
         PrintCollection "APPLICATION STATICOBJECTS COLLECTION", Application.StaticObjects()
         PrintCollection "SESSION STATICOBJECTS COLLECTION", Session.StaticObjects()
       End if
    End Sub
    
    Private Sub PrintSummaryInfo()
       With Response
        .Write("<hr>")
        .Write("<b>SUMMARY INFO</b></br>")
        .Write("Time of Request = " & dteRequestTime) & "<br>"
        .Write("Time Finished = " & dteFinishTime) & "<br>"
        .Write("Elapsed Time = " & DateDiff("s", dteRequestTime, dteFinishTime) & " seconds<br>")
        .Write("Request Type = " & Request.ServerVariables("REQUEST_METHOD") & "<br>")
        .Write("Status Code = " & Response.Status & "<br>")
       End With
    End Sub
    
    Private Sub PrintCollection(Byval Name, Byval Collection)
       Dim varItem
       Response.Write("<br><b>" & Name & "</b><br>")
       For Each varItem in Collection
         Response.Write(varItem & "=" & Collection(varItem) & "<br>")
       Next
    End Sub
    
    Private Sub Class_Terminate()
       Set objStorage = Nothing
    End Sub
    
    End Class
    
    </SCRIPT>
    					
  3. From the File menu, click Save.
  4. In the Save in drop-down list box, browse to the DebugTest folder that you created earlier. In the File name text box, type VBScriptDebug.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.
  5. Leave Notepad open.

MyVBScript.asp

  1. In Notepad, from the File menu, click New to create a new text document.
  2. Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file:
    <%@ Language=VBScript %>
    
    <% Option Explicit %>
    
    <!-- #include file="VBScriptDebug.asp" -->
    
    <%
    'Create a session variable to test the debugger.
    Session("MySessionVar") = "Test variable"
    
    'Declare variables.
    Dim x
    
    'Declare your Debug object.
    Dim Debug
    
    'Instantiate the Debug object.
    Set Debug = new clsDebug
    
    'Enable the Debug object. In the future, you can set it to false to disable it. 
    'The rest of your debug code can be left in the page.
    Debug.Enabled = true
    
    'The following code shows how to use the Debug.Print method
    'and how to provide a label to check your variables.
    x = 10
    Debug.Print "x before math", x
    x = x + 50
    Debug.Print "x after math", x
    
    'Create a cookie to test the debugger.
    Response.Cookies("TestCookie") = "Hello World"  
    
    %>
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    </HEAD>
    <BODY>
    
    <FORM action="" method=POST id=form1 name=form1>
    <INPUT type="text" id=text1 name=text1>
    <INPUT type="submit" value="Post Info" id=submit1 name=submit1>
    </FORM>
    
    <FORM action="" method=Get id=form2 name=form2>
    <INPUT type="text" id=text2 name=text2>
    <INPUT type="submit" value="Get Info" id=submit2 name=submit2>
    </FORM>
    </BODY>
    </HTML>
    
    <%
       'Call the End method to display all of the debug values.
       Debug.End
       Set Debug = nothing
    %>
    					
  3. From the File menu, click Save.
  4. In the Save in drop-down list box, browse to the DebugTest folder that you created earlier. In the File name text box, type MyVBscript.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.
To view the page, start your Web browser, and type the HTTP location of the page in the Address bar. If you saved the file in the above-mentioned location, type http://<servername>/DebugTest/MyVBScript.asp in the Address bar.

How to Use the VBScript Debug Object in an ASP Page

The MyVBScript.asp file demonstrates how to enable the VBScript Debug object in an ASP page. To understand how to use the Debug object, remember the following key points:
  1. To see how include files work, use Notepad to reopen MyVBScript.asp on the Web server. Approximately five lines into the code, notice that the following include code appears:
    <!-- #include file="VBScriptDebug.asp" -->
    						
    This line of code demonstrates how to include the Debug object (which is located in VBScriptDebug.asp) in your own ASP page.
  2. After the VBScriptDebug.asp page has been referenced through the server-side include file, the Debug object must be instantiated as follows:
    'Declare your Debug object.
    Dim Debug
    
    'Instantiate the Debug object.
    Set Debug = new clsDebug
    
    'Enable the Debug object. In the future, you can set it to false to disable it.
    'The rest of your debug code can be left in the page.
    Debug.Enabled = true
    						
    The Debug object is declared and then instantiated by setting the object to a new instance of the Debug class. After it is instantiated, the Debug class is enabled by setting Debug.Enabled to true.
  3. The following portion of code
    'The following code shows how to use the Debug.Print method
    'and provide a label to check your variables.
    x = 10
    Debug.Print "x before math", x
    x = x + 50
    Debug.Print "x after math", x
    						
    demonstrates how to use the Debug object to check variable values. You call the Print method of the object, pass it a string that describes what the variable is, and then pass in the variable itself. As the sample shows, you can use the Print method repeatedly on the same variable to track how its value changes through the code. When you browse to this ASP page in a browser, the Print method causes your variable values to be displayed so that you can check them.
  4. At the bottom of MyVbscript.asp, notice the following code:
    <%
       'Call the End method to display all of the debug values.
       Debug.End
       Set Debug = nothing
    %>
    						
    This code tells the Debug object to proceed and print all of the intrinsic ASP variables (Session, Application, Server, and so on), as well as any variables that you passed in. The Debug object allows your ASP page to run as normal. When the ASP page is enabled with Debug.Enabled = True, it displays all of the different variable values at the bottom of your page.
  5. When you are finished debugging, if you still want to leave the Debug object and Print statements in the code for future debugging, set the line that currently enables debugging to Debug.Enabled = False. This disables debugging but allows you to leave all the code in your page and simply set Debug.Enabled = True again when you want to debug.

Create a JScript Debug Object

If you use server-side JScript, the following ASP sample pages provide the same functionality as the preceding VBScript sample.

JScriptDebug.asp

  1. On the Start menu, point to Programs, point to Accessories, and then click Notepad.
  2. Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file:
    <SCRIPT LANGUAGE=JScript RUNAT=Server>  
    
    function objDebug()
       {
       var Now = new Date();
       this.RequestTime = Now;
       this.FinishTime = null;
       this.Print = Print;
       this.Storage = Server.CreateObject("Scripting.Dictionary");
       this.Enabled = false;
       this.End = StopDebug;
       }
    
    function StopDebug()
       {
       var Now = new Date();
       Debug.FinishTime = Now;
       if(Debug.Enabled == true)
         {
         PrintSummaryInfo()
         PrintDictionary("VARIABLE STORAGE", Debug.Storage)
         PrintCollection("QUERYSTRING COLLECTION", Request.QueryString())
         PrintCollection("FORM COLLECTION", Request.Form())
         PrintCollection("COOKIES COLLECTION", Request.Cookies())
         PrintCollection("SESSION CONTENTS COLLECTION", Session.Contents())
         PrintCollection("SERVER VARIABLES COLLECTION", Request.ServerVariables())
         PrintCollection("APPLICATION CONTENTS COLLECTION", Application.Contents())
         PrintCollection("APPLICATION STATICOBJECTS COLLECTION", Application.StaticObjects())
         PrintCollection("SESSION STATICOBJECTS COLLECTION", Session.StaticObjects())
    
         }
    
       }
    
    function Print(label,output)
       {
       if(Debug.Enabled == true)
         {
         Debug.Storage.Add(label,output)
         }
       }
    
    function PrintSummaryInfo()
       {
       var TotalTime = new Date();
       TotalTime = (Debug.FinishTime - Debug.RequestTime) / 1000
       Response.Write("<hr>")
       Response.Write("<b>SUMMARY INFO</b><br>")
       Response.Write("Time of Request = " + Debug.RequestTime + "<br>")
       Response.Write("Time Finished = " + Debug.FinishTime + "<br>")
       Response.Write("Elapsed Time = " + TotalTime + " seconds<br>")
       Response.Write("Request Type = " +    Request.ServerVariables("REQUEST_METHOD") + "<br>")
       Response.Write("Status Code = " + Response.Status + "<br>")
       }
    
    function PrintDictionary(theName, dict)
       {
       var i
       var KeyArray = (new VBArray(dict.Keys()));
       Response.Write("<br><b>" + theName + "</b><br>")
       for(i=0;i < dict.Count;i++)
         {
         Response.Write(KeyArray.getItem(i) + " = " + dict(KeyArray.getItem(i)) + "<br>")
         }
       }
    
    function PrintCollection(theName, theColl)
       {
       var x
       Response.Write("<br><b>" + theName + "</b><br>")
       for(x=1;x < (theColl.Count + 1); x++)          
         {
         Response.Write(theColl.Key(x) + " = " + theColl.Item(x) + "<br>")
         }
       }
    
    </SCRIPT>
    					
  3. From the File menu, click Save.
  4. In the Save in drop-down list box, browse to the DebugTest folder that you created earlier. In the File name text box, type JScriptDebug.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.
  5. Leave Notepad open.

MyJScript.asp

  1. In Notepad, click New on the File menu to create a new text document.
  2. Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file:
    <%@ Language=JScript %>
    
    <!-- #include file="JScriptDebug.asp" -->
    
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <%
    Session("MySessionVar") = "Test variable"
    
    //Note the difference in declaring the object variable.
    var Debug = new objDebug();
    var x
    Debug.Enabled = true
    x = 10
    
    //Notice that you have to include parentheses with the Print method.
    Debug.Print("x before math", x)
    x = x + 50
    Debug.Print("x after math", x)
    
    Response.Cookies("TestCookie") = "Hello World"  
    
    %>
    </HEAD>
    <BODY>
    <FORM action="" method=POST id=form1 name=form1>
    <INPUT type="text" id=text1 name=text1>
    <INPUT type="submit" value="Post Info" id=submit1 name=submit1>
    </FORM>
    
    <FORM action="" method=Get id=form2 name=form2>
    <INPUT type="text" id=text2 name=text2>
    <INPUT type="submit" value="Get Info" id=submit2 name=submit2>
    </FORM>
    
    <P></P>
    
    </BODY>
    </HTML>
    
    <%
    //Notice that you must use empty parentheses with the End method.
    Debug.End();
    %>
    					
  3. From the File menu, click Save.
  4. In the Save in drop-down list box, browse to the DebugTest folder that you created earlier. In the File name text box, type MyJScript.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.

How to Use the JScript Debug Object in an ASP Page

The MyJScript.asp file demonstrates how to enable the JScript Debug object in an ASP page, which is very similar to how the VBScript Debug object is used. To understand how to use the Debug object, remember the following key points:
  • The JScriptDebug.asp page is included in the MyJScript.asp file by using the same method as above, a server-side include directive:
    <!-- #include file="JScriptDebug.asp" -->
    						
    This line of code demonstrates how to include the Debug object (which is located in JScriptDebug.asp) in your own ASP page.
  • Instantiation of the Debug object is slightly different through JScript than through VBScript:
    //Note the difference in declaring the object variable.
    var Debug = new objDebug();
    					
  • Closing the Debug object in JScript is also different. Note the use of parentheses to close the Debug object:
    //Notice that you must use empty parentheses with the End method.
    Debug.End();
    					

Pitfalls

The only significant drawback to using these Debug objects is that they cannot provide as many debugging features as the Visual InterDev Debugger. However, in cases where you cannot use the Visual InterDev debugger, these objects may be a useful option.

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
192011 HOWTO: Configure Your Web Server for Visual InterDev 6.0 ASP Debugging
299982 HOW TO: Create Server-Side Include Files to Package ASP Scripts
For more information, see the following Microsoft Scripting Technologies Web site:
http://msdn2.microsoft.com/en-us/library/ms950396.aspx

Properties

Article ID: 299986 - Last Review: August 8, 2007 - Revision: 1.1
APPLIES TO
  • Microsoft Active Server Pages 4.0
Keywords: 
kbaspobj kbhowto kbscript KB299986
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com