I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

February Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Using Data   Print This     
  From the October 2008 Issue of Prestwood eMag
 
Delphi Using Data:
Developing Web Servers with Delphi
 
Posted 16 years ago on 11/7/2002 and updated 12/13/2006
Take Away: This article by Kim Berry was first published in the July 2001 Prestwood eMagazine.

KB100046



A “web server” receives an HTTP request, performs some processing, and finally submits a response. If your needs are met by serving static HTML pages or executing some CGI scripts, then you probably don’t need to develop a custom server. There are many economical hosting options available.

But if your needs are specialized or if the project entails web-enabling an existing application, then you might consider writing your own web server. The server can be an extension of IIS accessed as COM objects, or it can be a stand-alone program, providing the maximum flexibility to meet particular needs.

A web server does not need to serve HTML to a browser. The server can be dedicated to providing information in any format to applications, such as XML. As a replacement for client-server solutions, a web server could be considered to decouple the client application from the database. With this architecture, the web server can hold the business rules and translates between database and the client.

Basic Decision – Stand-alone or IIS?

There are two basic approaches you can take. While you might want to prototype and evaluate both, there are some limitations of each to consider first.

Stand-alone Program

Unless you prefer to start coding HTTP from the socket level, you will want to use a commercial library to provide the HTTP framework. We chose the Indy Components, which are included in Delphi 6, and can be downloaded at no charge for earlier versions:

http://www.nevrona.com/Indy/

The advantage of developing a stand-alone solution is that it is an application with a form user-interface. This allows buttons and other logging controls to appear on the screen. An application is easier to step through in the debugger, and it provides the maximum flexibility in how requests are processed.

The downside is that the server is more complex to develop. The threading is visible in the application and sometimes must be considered. It is not integrated into the server’s O/S. And none of the features of IIS, such as ASP, will be available.

ASP COM OBJECT

Pros: Although not technically a “server,” COM objects provide a powerful means of processing HTTP requests. Again, Delphi is well-suited for developing COM objects.

Advantages of implementing a web server as COM objects are that threading is hidden, the O/S provides much of the framework and configuration, and it runs within IIS, providing support for ASP and configuration.

The drawbacks are that there is no user-interface to the COM objects. They are more difficult to debug within the Delphi environment. Once accessed, they remain in memory until the PC is rebooted. And there are configuration limitations in how pages are mapped to services.

Example – Indy Web Server

This section provides an outline for getting a Delphi web server running. In fact “no coding is necessary” because the Indy Components include a demo. Once running you can modify it to meet your specific requirements 

I found that I had to make the following modifications in order to run the HTTPServer demo::

  • Change property cbActivate.Enabled from True to False
  • Change edPort.Text from ‘80’ to ‘81’;
  • TidHPTTPServer.Bindings: Remove the default server bindings (otherwise exception 10049)
  • When prompted the first time it is compiled, provide path to Indy source code for debugging
  • //I had to comment out HTTPServer.SessionState := cbManageSessions.Checked;

 Run it: http://local-server-name/index.html

Indy Web Server - Virtual Page

In addition to serving physical pages, the server can respond to virtual pages

Clone ServeMyVirtualFolder from ServeVirtualFolder  

procedure TfmHTTPServerMain.ServeMyVirtualFolder(AThread: TIdPeerThread;

  RequestInfo: TIdHTTPRequestInfo; ResponseInfo: TIdHTTPResponseInfo);

var

  html : String;

begin

  ResponseInfo.ContentType := 'text/HTML';
  html := '';
  html := html + '

' + DateTimeToStr(Now)+ '

';
  html := html + '

Requested: ' + RequestInfo.Document + '

';
  html := html + '

Params: ' + RequestInfo.UnparsedParams + '

';
  html := html + '';

  ResponseInfo.ContentText := html;
end;
 

Example – ASP COM on IIS Server

COM objects are instantiated and accessed by VBScript embedded within an ASP Page. A simple example of ASP that would display the current time is:

 Time: <% = Time %>

 

 The ASP for this demo is:

<% Set DelphiASPObj = Server.CreateObject("dugproj.dugclass") 
   text1 = DelphiASPObj.table 
%> 

<%=text1%>

Creating a COM Object in Delphi

I borrowed the steps from the article “Creating ASP com objects with Delphi 5
by Jan Verhoeven, modified 24 april 2000
” (http://www.jansfreeware.com/articles/)

  1. File - New - ActiveX - ActiveX Library
  2. File - New - ActiveX - Active Server Object
    • CoClassName=dugclass
    • Instancing=Multiple Instance
    • Threading Model=Both
    • Active Server Type=Page Level Events Model
    • Options= (checked) Generate a template test script for this object
  3. Add new Property:  Name=table type=BSTR
  4. Add text to the property’s Get method
  5. Save all
  6. Run – Register ActiveX Server
  7. Map new virtual directory ‘DUG’ in Personal Web Manager

http://local-server-name/dug/dugclass.asp

Power of ASP COM Objects

  • Can be called from Delphi, VB, or ASP
  • Many resources: www.aspobjects.com
  • XML, dynamic charts

Summary

There are significant differences between using Delphi to create a stand-alone web server vs. writing ASP COM objects for an IIS server. Either solution can be prototyped with minimal effort. Your choice will depend on the requirements.


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2
very nice, but how can I access virtual web pages ?
Posted 14 years ago

Comment 2 of 2

I never would've guessed that Developing Web Servers with Delphi would be as simple as all this. I've currently got to get a custom eassy worked on and as soon as that's worked out, I'll head over to developing a server. But I'll need a complete team with me for the task.

Posted 11 days ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1236A1
Enter key:
Article Contributed By Kim Berry:

Kim Berry is an experienced coder currently specializing in VS.Net C# coding of WinForm and WebForm applications. Kim currently works part time for Prestwood IT Solutions and participates in this online community when time allows. Kim worked fulltime at Prestwood Software for four years and is still available for part time evenings and weekends work. He was one of the main Prestwood developers developing ASPSuite and has coded in many languages including C, Visual Basic, Delphi, and Visual Studio.Net.

Visit Profile

 KB Article #100046 Counter
8905
Since 4/2/2008
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

3,229 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2018 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech