1. Tech

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/library/bluc/text/uc060901a.htm

was emailed to:

Thanks for sharing About.com with others!

RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
ISAPI tutorial for Delphi developers
Page 1: A brief introduction to web-broker technology
 More of this Feature
• Pg 2: Using this tutorial
• Pg 3: Getting started
• Pg 4: Web Actions
• Pg 5: The first ISAPI app
• Pg 6: TWebRequestObject
• Pg 7: Request-responding
• Pg 8: TPageProducers
• Pg 9: Cookies made easy
• Pg 10: Stateless HTTP
• Pg 11: DB enabled apps
• Pg 12: FAQ
• Pg 13: DB apps - Part 1
• Pg 14: DB apps - Part 2
• Pg 15: DB apps - Part 3
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Internet programming
• CGI and ISAPI with Delphi

Article submitted by: Hadi Hariri

Delphi's Web-broker technology allows you to create four different types of web server applications:

  • ISAPI
  • NSAPI
  • CGI Standalones
  • Win-CGI

    ISAPI/NSAPI are virtually the same thing. One is Microsoft specification and the other is Netscapes. ISAPI stands for Internet Server Application Programmers Interface. From now one we will refer to both ISAPI/NSAPI as just ISAPI. NSAPI will actually disapear soon becoming ISAPI the standard.

    NOTE: Don't confuse the terms Web Server with Web Server Application (or Extension). The Web Server is a HTTP server that you buy (or get free) to host your web sites. Examples of these are Internet Information Server from Microsoft and Apache. A Web Server Application (or Extension) is a sort of add-on to your Web Server that provides additional functionality and facilites. I will use the terms application/extension interchangably in this tutorial.

    CGI's are basically the same as the ones you have seen on the Internet for so long. They read information from the standard input and write to the standard output.

    A Win-CGI receives information from a INI file written by the server, write the result back to this file and the server then sends it to the client.

    Delphi doesn't do anything magical with this technology. It doesn't mean that if you don't use it you can't create ISAPI's or CGI's. You could create a a webserver app without Delphi's web-broker technology, but let's put it this way: 'it makes life much much easier'. What the web-broker does (a part from other things) is encapsulate the requests and responses in objects that are very intuitive and easy to use, thus making the communication much easier.

       Difference between ISAPI's and CGI's
    This tutorial will mainly focus on creating ISAPI's. However, nearly everything mentioned is applicable to CGI's. The difference between them is that:

    An ISAPI is a dynamic link library (dll). As like any other dll, it is loaded into memory and stays there until it is no longer needed. What does this mean? Well for one thing, the load time is smaller; it only loads once. This reduces response time for a client request. CGI's on the other hand are like normal executables. Everytime a request is made, the exe is loaded, the request is served and then the executable is unloaded from memory. This makes everything slower.

    It's not all benefits though. ISAPI's have their disadvantages. Let's take a look at the TISAPIApplication and see what these disadvantages are.

    Each time a request is made, a new instance of the webmodule is created. This also creates a new instance of all the objects the webmodule contains. If you declare a variable in the webmodule as global, all requests to your webserver application will be refering to the same variable. Imagine this scenario:

    1. A client makes a request to your webserver application and sets the global variable x to value 10.
    2. While the first client is still waiting for a response, another client makes a request and set the value of the variable x to 20.
    3. Now the first client needs the value of x to return a response. It reads it and instead of retrieving the value 10 as it set it, it instead retrieves 20.

    Conclusion: DON'T USE GLOBAL VARIABLES. Every request creates a new instance of the webmodule and the objects it contains. This does not include global variables. It doesn't matter whether you declare it as global, in the private or public section, it is still global to all requests.

    So, the main disadvantage to ISAPI's over CGI's is that you have to be carefull with the code you write. You can't use global variables just like that. There are work-arounds to this such as using some kind of unique access to them (critical sections, mutexes or semaphores).

    I read that you can make an existing application web-enabled very easily, is this true?

    You might have read that Delphi's RAD power let's you make your application web-enabled. This is true to an extent. However, don't ever think that by just dropping a component, viola, you are web-enabled. No. What Delphi offers you is a component called the TWebDispatcher. Basically what this component does is convert a HTTP request into a webaction. We will look at what a web action is later. A TWebModule already has a TWebDispatcher component. You do not need to add one ( you can't drop a TWebDispatcher into a TWebModule).

    Therefor a TWebmodule is a TDataModule + TWebDispatcher. What they say when they tell you that you can make an existing application web-enabled, is that if you have a TDataModule with non-visual components, bussiness rules, etc. you can drop a TWebDispatcher onto it and make a webserver application. The TWebDispatcher will take care of converting the HTTP requests into webactions. But don't think for one minute, that in some magical way all the forms in your application will appear in the clients browser.

       What is CacheConnections?
    When you make a ISAPI application, the Application is instantiated to a descendant of TWebApplication. This gives you a property called CacheConnections. It's of boolean type and what it does is that if you set it to True, saves inactive web-modules in the cache. This allows a new request to be served without having to create a new instance of the webmodule again. It uses a cached one. However, changes made to the contents of the webmodule might effect the new request. This implies you having to initialize what you need on each request. If you set CacheConnections to False, every request to the web server application will create the webmodule, serve the request and then destroy it. This is the same behaviour as a CGI and you can see that it takes one of the major benefits away from ISAPI's. By default CacheConnections is set to True.

       What is an ISAPI filter and what is an ISAPI extension?
    Think of an ISAPI filter as just that: a filter. It sits between your webserver and the client. Everytime a client makes a request to the webserver, it is passed through this filter.

    An ISAPI extension on the other hand, is similar to a normal application. You have to call it to activate it. This tutorial focuses on the latter. For information about creating ISAPI filters, look at thelinks. in the overview.

    HTTP is stateless

    Rember this phrase. It is very important to understand what this means before starting to program internet applications:

    1. A client makes a request to your webserver application (extension). The server replies with a response.
    2. The same client makes a new request. The server does not know what request was made previously by the same client or what state the client was left in.

    There are ways to work around this. We will see how to keep information with the use of fat urls, cookies and hidden fields. But always remember this. You don't know what state the client was in.

    Next page > How to use this Tutorial > Page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

  • All graphics (if any) in this feature created by Zarko Gajic.

     More Delphi
    · Learn another routine every day - RTL Quick Reference.
    · Download free source code applications and components.
    · Talk about Delphi Programming, real time.
    · Link to the Delphi Programming site from your Web pages.
    · Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
    · NEXT ARTICLE: ADO Cursors - DB/10.
    Chapter ten of the free Delphi Database Course for beginners. How ADO uses cursors as a storage and access mechanism, and what you should do to choose the best cursor for your Delphi ADO application.
     Stay informed with all new and interesting things about Delphi (for free).
    Subscribe to the Newsletter
    Name
    Email

     Got some code to share? Got a question? Need some help?

    ©2014 About.com. All rights reserved.