Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 3 - Building a Web Service from Scratch with D...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Building a Web Service from Scratch with Delphi
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-11-06

    Table of Contents:
  • Building a Web Service from Scratch with Delphi
  • Writing the Methods
  • Adding state
  • Testing the service

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a Web Service from Scratch with Delphi - Adding state


    (Page 3 of 4 )

     

    Now there's just one more thing we need to do before testing our service. CGI applications by their very nature do not have state, in other words they do not remember or hold any data. So for example, if we now run this service and enter a text for the setTxt procedure, the text will not be remembered by the application, so we need to find a way to store the data somewhere. To solve this problem, we are going to create two more methods that will help in this regard. In the implementation units' "public" section add the following two methods:

    Constructor Create; Override;

    Destructor Destroy; Override;

    Then in the implementation section add the following code:

    constructor TMyWebServer.Create;
    Var 
      f : textfile;
      buff:string;
    begin 
      inherited;
      AssignFile(f, 'c:ATxtfile.txt');
     Reset(f);
     ReadLn(f, buff);
    TheTxt:=buff;
     closefile(f);
    end;
    destructor TMyWebServer.Destroy;
    Var
     f : textfile;
    begin
      AssignFile(f, 'c:ATxtfile.txt');
     Rewrite(f);
     WriteLn(f, TheTxt);
     CloseFile(f);
      inherited;
    end;

    Basically what these two methods do is write the text entered by the user to a text file called "Atxtfile." So all that the application should do is read the text from this file when the getTxt method is called.

    After all these changes your two units should look something like this:

    Interface unit:

    { Invokable interface IMyWebServer }
    unit MyWebServerIntf;
    interface
    uses InvokeRegistry, Types, XSBuiltIns;
    type
      { Invokable interfaces must derive from IInvokable }
      IMyWebServer = interface(IInvokable)
      ['{20BC8DE1-3A20-462D-AAF0-7B96FFDD72CD}']
        { Methods of Invokable interface must not use the default }
        { calling convention; stdcall is recommended }
        Procedure SetTxt(Const AValue : String); stdcall;
        Function GetTxt : String; stdcall;
      end;
    implementation
    initialization
      { Invokable interfaces must be registered }
      InvRegistry.RegisterInterface(TypeInfo(IMyWebServer));
    end.

    Implementation Unit

    { Invokable implementation File for TMyWebServer which implements IMyWebServer }
    unit MyWebServerImpl;
    interface
    uses InvokeRegistry, Types, XSBuiltIns, MyWebServerIntf,IniFiles;
    type
      { TMyWebServer }
      TMyWebServer = class(TInvokableClass, IMyWebServer)
      Private 
      TheTxt : String;
      public
        Procedure SetTxt(Const AValue : String); stdcall;
        Function GetTxt : String; stdcall;
       Constructor Create; Override;
        Destructor Destroy; Override;
      end;
    implementation
    constructor TMyWebServer.Create;
    Var 
      f : textfile;
      buff,ATxt:string;
    begin 
      inherited;
      AssignFile(f, 'ATxtfile.txt');
     Reset(f);
     while not EOF(f) do begin
     ReadLn(f, buff);
    TheTxt:=ATxt;
    end;
    closefile(f);
    end;
    destructor TMyWebServer.Destroy;
    Var
     f : textfile;
    begin
      AssignFile(f, 'ATxtfile.txt');
     Append(f);
     WriteLn(f, TheTxt);
     CloseFile(f);
      inherited;
    end;
    function TMyWebServer.GetTxt: String;
    begin 
      Result := TheTxt;
    end;
    procedure TMyWebServer.SetTxt(const AValue: String);
    begin 
     TheTxt := AValue;
    end;
    Initialization
      { Invokable classes must be registered }
      InvRegistry.RegisterInvokableClass(TMyWebServer);
    end.

    Save all your project files in one directory, including the interface and implementation units. Then in the Delphi IDE go to Projects, then options, and then set the output directory to "X:inetpubwwwrootcgi-bin." "X" is for whatever drive name your web server root is located on. The cgi-bin directory is not automatically included in the webroot for those of us who run IIS, so create it and give it execute rights. We set the output directory to the cgi-bin directory because we won't have to copy our exe to the cgi-bin directory every time we make changes to our application. The exe will automatically be placed in that directory when we recompile it. Unlike most other web services, our web service does not need an Internet connection, but requires a web server.

    Compile and run the service. An EXE file should now be created; to verify, check your cgi-bin directory. Then open up your browser and enter "http://localhost/cgi-bin/thenameofyourproject.exe" You should see something like this in your browser:

     

    There you can see our two methods listed and also a link to the web service WSDL file, which we will need to create a client. Click on this link and you should see something like this:

    If you are using IE, go to File|Save As... and save the file.  Call it whatever you like (I saved mine as IMyWebServer), as long as its extension is xml. Also make sure to save the file in your project directory and not your cgi-bin dir. This way we know where to look for the WSDL file.

    More Delphi-Kylix Articles
    More By Leidago


       · Web services are a exiting new technology, this article will explain how you can...
     

    DELPHI-KYLIX ARTICLES

    - Delphi Wrapper Classes and XML
    - Delphi and the DOM
    - Delphi and XML
    - Internet Access: Client Service
    - Finishing the Client for an Internet Access ...
    - The Client for an Internet Access Control Ap...
    - User Management for an Internet Access Contr...
    - Important Procedures for an Internet Access ...
    - Server Code for an Internet Access Control A...
    - Constructing the Interface for an Internet A...
    - Building a Server Application for an Interne...
    - Building an Internet Access Control Applicat...
    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT