Delphi is a great tool for writing Web server applications of all shapes and sizes, but debugging those applications is difficult, to say the least. I’ve had some success using IntraBob, which acts as a host application for ISAPI DLLs. And other developers have had some success loading Microsoft’s Internet Information Server as a host application, but that process is long and error-prone. No solution was available that satisfied every developer until Delphi 6 introduced Web App Debugger. Let’s take a look at how you can access this executable and use it to debug your Web server applications.

Finding your target
Web App Debugger is part of Delphi 6 Professional and Enterprise editions, and it’s completely integrated into the environment. When you want to produce a new WebBroker or WebSnap application, you can use Delphi’s Web Server Application Wizard to access a selection of targets—one of which is the Web App Debugger. To see this, start Delphi 6, choose File | New | Other and select the Web Server Application icon from the Object Repository. This will open the New Web Server Application dialog box, shown in Figure A.

Figure A
New Web Server Application

As you can see, we can produce a number of possible Web server targets using Delphi (and C++Builder, for that matter). A CGI or Win-CGI application will be loaded (and unloaded) for every incoming request, while an ISAPI/NSAPI DLL or Apache Shared Module typically remains loaded between requests. The Web App Debugger executable is significantly different because it’s available only for debugging purposes. It’s important to realize that a Web App Debugger executable project should not be deployed on the Internet—for that, you need to pick one of the first four targets.

Fortunately, it’s not hard to “transform” a project for a specific target into one for another target. As this sidebar explains, you just have to share the Web module with two or more Web server application projects.

The debugging process
To debug, you select the Web App Debugger executable, which prompts you to enter a value for the CoClass Name, such as WAD42. A CoClass name is required for the Web App Debugger executable project (WAD executable project, for short) because a WAD executable project will contain a COM object that can be used by a COM client (the Web App Debugger itself).

After you click OK in the New Web Server Application dialog box, a new WAD executable project will be created. It consists of a main project file and at least two new units. The first unit is a main form that ensures that the out-of-process COM object can be accessed. The second unit is the Web module, which would also have been created for any other Web server application target. Save the project and place the main unit in the file WADForm.pas, the Web module in WebMod.pas, and the project main file itself in WAD42.dpr.

As I mentioned, the Web module is the part that all Web server applications have in common, and you can move it from one project to another or share it between projects producing different targets. For our example, double-click on the Web module to start the Web Actions Editor. Click Insert to create a new action and then set the default property of this item to True. Move to the Events tab of the Object Inspector and double-click on the OnAction event handler to write the code shown in Listing A. In practice, you would write more complex code, but this is enough to demonstrate the main debugging feature: setting a breakpoint inside a Web server application.

Adding a breakpoint
Now, set a breakpoint on the line with Response.Content and press [F9] to run your application. This will present you with the main form, nothing more. But the WAD executable project is now waiting to be used, so we can start the Web App Debugger, which can be found in the Tools menu of the Delphi IDE.

Click the Start button, shown in Figure B, to start the Web App Debugger “engine.” This will turn the Default URL into a live link. When you click on it, a browser window opens to show an overview of all registered WAD executables on your local machine.

Figure B
Clicking Start turns the Default URL into a link.

Figure C shows only two registered servers. The first one is the Web App Debugger support utility ServerInfo. The second one is our example application WAD42 with the WAD42 COM object inside.

Figure C
Registered WAD servers

Select the second item in the list and click Go to start this application. The next thing you know, you’ll be inside the Delphi 6 IDE, shown in Figure D. As you can see, the breakpoint was just triggered, stopping your application.

Figure D
Breakpoint triggered in Delphi 6 IDE

Of course, once you’re inside the IDE on the breakpoint, you can use all Delphi IDE debugging features (stepping, view watches, the call stack, modules, the event log, etc.). When you continue the application, the output is generated and shown inside the Web browser, where we clicked on the Go button.

You can try this again, appending something like ?Name=Bob in the URL of the browser so that you actually pass a value to the Request.QueryFields variable. The output will also appear in the browser, as shown in Figure E.

Figure E
New output of WAD Web server application

Web App Debugger Log
After running your Web App Debugger application a few times, you may want to examine the Log page of the Web App Debugger, shown in Figure F. It contains some useful information like the elapsed time (but remember that it also includes the time you spent in the debugger), the request path, the content length, etc. This information is especially useful if you want to know how long a specific database operation takes. Not that the exact time is any indication of the real time it will take on the deployment Web server, but at least you can measure the effect of some optimization techniques, which you can try locally instead of on your actual deployment server.

Figure F
Log page of Web App Debugger

Tailor it to fit your needs
If you want some more features in the Web App Debugger, you’ll be happy to know that you can add them yourself. Delphi 6 includes full source code for the Web App Debugger. This is a little-known fact, since the source code is not located in one of the Demos directories but rather exists as WebAppDbg.dpr (with helper application ServerInfo.dpr) in the Source\Internet subdirectory.

I’ve added a number of additional tabs that show the entire contents being sent back and forth between the client and server, which proves quite helpful as trace functionality. But that’s for a future article.

Debugging Delphi

What’s your experience debugging Delphi? Drop us an e-mail or post a comment below.