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:
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
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/)
File - New - ActiveX - ActiveX Library
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
Add new Property: Name=table type=BSTR
Add text to the property’s Get method
Save all
Run – Register ActiveX Server
Map new virtual directory ‘DUG’ in Personal Web Manager
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.
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!