Habari Web Components is a web application framework for Delphi 2009 and newer. It allows to easily compose web applications with these building blocks:
- a Web Component base class which provides HTTP method handlers (OnGet, OnPost, OnPut, OnDelete etc.)
- a HTTP server run time environment, based on Internet Direct (Indy) 10.5.8
- logging, based on the open source logging framework Log4D
- optional integration with an open source message broker (Apache ActiveMQ, Apollo, HornetQ, RabbitMQ and others)
Development Process
The development process for Delphi web applications with this framework is:
- create a Web Component class by subclassing TdjWebComponent and override one or more HTTP event handlers, for example OnGet and OnPost
- choose URL path mappings, for example ‘index.html’, ‘login.html’, ‘*.html’ or ‘/secure/*’
- add the Web Component to an application context, for example ‘app’, ‘web’, or ‘domain1′
A Web Component will be executed when a HTTP clients hits its resource URL with the matching HTTP method – for example a GET request on http://127.0.0.1/app/login.html.
Web Contexts
The framework supports multiple application contexts:
- a context is the top-level directory of the web site (context myapp will appear as 127.0.0.1/myapp/index.html)
- context can hold any number of web components (and every web component belongs to exactly one context)
- a subfolder in the server installation directory can be used to serve static files
- context may be associated with HTTP ports – for example to run http://server:80/public and http://server:8888/admin simultaneously
Web Component implementation – Hello World example
This example responds to GET requests with a ‘Hello world’ response body:
type THelloPage = class(TdjWebComponent) public procedure OnGet(Request: TIdHTTPRequestInfo; Response: TIdHTTPResponseInfo); override; end; procedure THelloPage.OnGet; begin Response.ContentText := '<html>Hello world!</html>'; end;
Available event handler methods:
| OnPost | This method will answer to a HTTP POST request |
| OnGet | This method will answer to a HTTP GET request |
| OnPut | This method will answer to a HTTP PUT request |
| OnDelete | This method will answer to a HTTP DELETE request |
| OnHead | This method will answer to a HTTP HEAD request |
| OnOptions | This method will answer to a HTTP OPTIONS request |
| OnTrace | This method will answer to a HTTP TRACE request |
Context Creation and Component Deployment
The deplyoment of the new Web Component in the context ‘web’ requires one line of code:
Server.AddComponentInContext('web', THelloPage, '/hello.html');
This will make the component available at http://127.0.0.1/web/hello.html.
Note that the resource location ‘/hello.html’ is not a physical file!
- the framework uses path mappings to find the matching web component for a request URL and executes its On… method handler
- if no match is found, the framework will return a static file with the same name
- if no static file exists, the framework will return a 404 error
Supported Mapping SYNTAX
Supported mapping styles in order of priority are
- absolute paths, for example ‘/mypage.html’
- prefix mappings, for example ‘/myfolder/subfolder/*’
- suffix mappings, for example ‘*.html’ or ‘*.page’
If two web components declare overlapping mappings, they will be processed in the order of their priority. Example
- WebCompontentA maps to ‘*.html’
- WebCompontentB maps to ‘/myfolder/*’
In this case, the resource /myfolder/index.html will be handled by WebComponentB because a prefix mapping (/myfolder/*) has a higher priority than an extension mapping.
Multiple mappings
Multiple mappings per component are supported. Example:
- WebCompontentA maps to ‘*.html’, ‘*.doc’ and ‘*.pdf’
- WebCompontentB maps to ‘/secure/*’ and ‘/protected/*’
With this mapping, resource /context/secure/example.pdf will be handled by WebComponentB, and resource /context/example.pdf will be handled by WebCompontentA.
Additional Features
Static content
Static resources can be served as well. CSS, JavaScript and image files can be placed in a subfolder with the same name as the context. These files will be used when a request was not handled by any web component in this context. See screenshot below – the style sheet (CSS) and JavaScript files are served from a local directory.
VisualMM – monitor FASTMM Memory Usage
FastMM memory allocation can be monitored while the server is running. The Habarisoft VisualMM system is included, with full source.
Note: the demo uses a single-process monitor which measures only its own memory usage. For network usage and central data collection of an unlimited number of Delphi processes, VisualMM requires an open source message broker like Apache ActiveMQ, HornetQ or OpenMQ and a developer license for a Habari message broker client.
Demo Download
A demo download is available, the archive contains a Delphi console app which launches a stand-alone server and the default browser to display the web application main page. From there you can navigate between demo pages and view the Delphi source code for the running Web Components in your browser.
The server app also logs its messages to the console window. The Log4D configuration file is located in config/log4d.properties.


