Delphi Internet Express: The WebServer Application

By: Charles Calvert

Abstract: The Internet Express allows you to easily create XML based web server applications in Delphi.

Creating the WebServer Application

It is now time to create the web server that will talk to your Midas Server and serve up data over the web using HTTP. This web server will be a separate binary file that talks to your Midas Server

To work with two projects at the same time, use the Project Manager. You can access the Project Manager by choosing View | Project Manager. I like to dock the Project Manager as shown in Figure 2.

The docked project manager

Figure 2: Docking the Project Manager so it is easy to use.

Push the New button at the top of the Project Manager or else right click on the top node in the Project Manager and choose Add New Project. Be sure that you don't choose File New to create a new project.

You want to create a Web Server Application, which is available on the bottom right of the New Page in the New Items dialog, as shown in Figure 3.

Web Server Application Icon

Figure 3: Selecting the Web Server Application icon.

You can now build an ISAPI, CGI or WinCGI application. Choose to build a CGI application, as shown in Figure 4.

Picking the CGI App

Figure 4: Choosing to create a CGI application in the New Web Server Application Dialog.

When you are done, you should be looking at form that has the same general appearance as a data module. Save your work. Call the unit in your project MainWebServer and call the project itself the Country WebServer.

Setting up the Web Module

It is now time to drop components on the TWebModule you created. These components will talk to your server, bring down the data from it in XML format, then format the XML with HTML. Your web server application will server up the HTML, and then the web server you are using will serve the data up over the web. (The whole formula: The browser on a client machines talks HTTP to your web server, which talks to your web server application, which in turn talks to your midas server. In return, the midas server sends information to your web server application, which sends it to the web server, which sends it back to the browser. Phew!)

To get started working with the web module, turn to the Midas page of the Component Palette. Find the TDComConnection component and drop an instance of it down on your web module. Set the Name property to CountryConnection.

Drop down the Server Name property and select the name of your server. It should be called CountryServer.Country object. If you do not see the name of your server in the list, then your server is not yet registered. To register your server, run it once. If for some reason you don't see it, the likely causes are: 1) On your server you used a TDataModule rather than a TRemoteDataModule. 2) You are trying to use a Delphi 4 Midas server.

Turn to the Internet Express page in the Component Palette and drop down a TXMLBroker. Select the Name property of the broker and call the component the CountryBroker.

Set the RemoteServer property of the CountryBroker to the CountryConnection, and set the ProviderName to the CountryProvider. You should be able to pick both of these options from the drop down lists in the respective editors of the RemoteServer and ProviderName properties. If you don't see the CountryProvider listed as an option in the ProviderName editor, then something is wrong with your server. If you used the Delphi 5 TDataSetProvider, you should have to do nothing more than ensure that the Export property is set to true, which it is by default. You should also make sure that the DataSet property of the TDataSetProvider is set to the CountryTable.

Go to the Internet Express page of the Component Palette and snag a TMidasPageProducer. Set the Name property to CountryPageProducer.

There are three things you need to do now:

  • Choose Project | Options from the Delphi menu. Turn to the Directories/Conditional page. Set the OutPut directory to C:/Inetpub/Scripts, or to the directory on your system where your web server expects to find CGI scripts. The C:/Inetpub/Scripts directory is default directory for scripts if you are using the Internet Information Server or the Personal Web Server.
  • On the CountryPageProducer, set up the IncludePathURL property to point to the directory where your Delphi WebMidas java script files are located. By default, Delphi installs these files in the Delphi5SourceWebMidas directory. You should copy the entire contents of this directory to your C:/Inetpub/Scripts directory, or to another sensible location on your system. When you are done, you should be able to find the err.html, xmldb.js, xmldisp.js, xmldom.js, xmlerrdisp.js and xmlshow.js files in your scripts directory. These files are needed to make your web server application properly handle the XML that Delphi produces. Suppose you put your web server application in the Scripts/Temp directory, and the java script files in the Scripts directory. In that case, you would set the IncludePathURL to http://MyServer/Scripts, where MyServer is the name your server. If the web server application and the java script files are in the same directory, then you don't have to put anything in the IncludePathURL property. In some cases you may have to grant java script access for the directory where you have placed your java script. For instance, in a Windows 2000 beta, I needed to go into the Control Panel and open up the Internet Information Services applet that controls the Internet Information Server. In that application I set Read and Script Source Access to True for my Scripts directory.
  • Right click on the TMidasPageProducer component and select Action Editor. Right click on the Action Editor itself and choose Add to create a web action item. In the Object Inspector, set the Producer property of the web action item to CountryPageProducer by selecting it from the drop down list. This ensures that any HTML produced by the TMidasPageProducer will be sent out over the web. In this case, you want this action to be the default action, so set the Default property for the WebActionItem to True. If you did not set this property as the default property, you could still access it, but you have to pass in /CountryPageProducer as a parameter to your CGI application. See figure 5.

Web Action Item

Figure 5: Setting up an Action item.

I have listed the three steps above all in a row, because it might be helpful for you to think of them as a checklist. You must complete each of these items to make your server work correctly, and yet none of them necessarily appear as an intuitive part of creating your web producer. As a result, I find it best to remember that there are three miscellaneous actions that I must perform before my producer is complete, so I tick them off in my head, just to be sure I've completed all the key steps.

Designing the HTML for your Producer

Now, at last, you are ready to produce the HTML that you will send to the web server, and hence to the user's browser. To do this, double click on the CountryPageProducer to bring up the Web Page Editor, as shown in Figure 6.

Web Page Editor

Figure 6: The Web Page Editor as it first appears when you double click on TMidasPageProducer.

The Web Page Producer has two pages in its bottom half. The Browser page shows what your HTML will look like to the user. At first, this page is blank, since you haven't written an HTML yet. The HTML page shows the HTML that will be generated by the Web Page Producer. You don't write the HTML itself. Instead, you use Delphi components to automatically generate the HTML. As you walk through the steps in this section of the paper, you should switch back and forth between the Browser and HTML pages, seeing the changes that are made as you step through the process.

When you start, the HTML generated by the Web Page Producer looks like this:

This is the minimum amount of code found in any page produced by the Internet Express.

Right click on the words "CountryPageProducer" at the top left of the Web Page Editor to bring up the Add Web Component Dialog. Select DataForm from the list box and click the okay button.

Now if you look at your HTML you will see that it looks like this:

The DataForm component added the Form code to your HTML file. Other components will generate much more code, and much more complex code, but this is the general pattern that you will see followed throughout the rest of this process.

Right click on the DataForm1 component and choose FieldGroup. Click Ok. Check out the HTML that was generated. Turn to the Browser page and you will see the warning showed in Figure 7.

Warning message

Figure 7. A warning generated by the Web Page Producer. Treat warnings as hints that tell you what you should do next.

I'm not really sure whether "warning" was the right word for these messages. From my point of view, this is really more of a hint. This particular message is telling you that you need to connect field group to a TXMLBroker. To do this, turn to the Object Inspector and set the XMLBroker field to CountryBroker.

Now look at the Browser page and you will see HTML has been generated to wrap each of the fields in your table, as shown in Figure 8.

Wrapped Fields

Figure 8: The Browser after you connect up the XMLBroker property of the field group.

At this stage, you should probably right click on the words "Field Group", and select "Add All Fields". Now you will be able to work with each of the fields displayed in the Browser page. For instance, you could select one of the fields, such as Area, and delete it. You are not, of course, delete a field from the database itself, but only choosing not to display one of the fields in your browser.

At the bottom of the list of fields you will find one called FieldStatus1. This field shows the status of the current record. For instance, if the user modifies a row in a dataset, then this field will display the letter M. It will not hurt if you delete this field, but you might want to keep it around at first, at least until you understand what it can and can't do for you.

Right click on DataForm1, choose New Component, and select DataNavigator. This generates HTML that creates the equivalent of a TDBNavigator on your HTML form.

Use the XMLComponent property of the Navigator to connect it to FieldGroup1. If you want, right click on the navigator, choose New Component, and select the buttons that you want to display in the Navigator. If you don't do this, the navigator will display with the completely serviceable buttons that appear by default. If you add all the available buttons, you will find some new functionality that may or may not be useful to you when debugging your applications. I will not discuss that functionality in this paper, but will only point out that it is available, and that you might eventually want to take the time to experiment with it. In particular, you might want to check out the ShowXMLButton option.

You are at last finished creating your web server application. To test it, select Project | Build Country Web Server from the Delphi menu.

Open either IE, Netscape, or the browser of your choice and type in the following URL: http://MyServer/Scripts/CountryWebServer.exe, where you may have to change one or more portions of the URL to fit the circumstances of your current server set up. See Figure 9 to view the results of your efforts.

Country Web Server in Action

Figure 9: The CountryWebServer in action.

Using the navigator, you can, of course, iterate through the records in the database, insert new records, delete records, or anything you would normally do with a TDBNavigator.

Server Response from: ETNASC04