Home > Articles > Programming > General Programming/Other Languages

Delphi 6 WebSnap Advanced Topics

  • Print
  • + Share This
Delphi 6 introduces a new web application framework called WebSnap that brings the strengths of Rapid Application Development (RAD) to web development. Building on WebBroker and InternetExpress, WebSnap is a big leap forward for Delphi developers who want to use their favorite tool to build web applications. This article covers some advanced topics that allow you to more finely tune your WebSnap applications.
This sample chapter is excerpted for Delphi 6 Developer's Guide, by Xavier Pacheco and Steve Teixeira.
From the author of

LocateFileServices

The development of WebSnap web applications usually requires the coordination of various resources. HTML, server-side script, Delphi code, database access, and graphics all need to be properly tied together into a single application. Most often, many of these resources lie embedded in and are managed via an HTML file. WebSnap provides support for separating HTML from the implementation of a dynamic web page, meaning that you can edit the HTML files separately from the web application's binary file. However, by default that HTML must reside in files in the same location as the binary file. This isn't always convenient or possible, and sometimes you may want HTML to reside in locations away from your binary. Or you might want to get HTML content from sources other than files, such as a database.

WebSnap provides the ability to get HTML from any source that you want. The LocateFileService component allows you to get HTML from any file location, include files, or any TStream descendant. Being able to access HTML from a TStream means that you can get the HTML from any source as long as it can be placed in a TStream. For example, HTML can be streamed from a RES file embedded in your application's binary file. A demo application can show how this is done. Naturally, you'll need some HTML to embed. Using a text editor or your favorite HTML editor, take the wmLogin.html file as a template and save it in your demo application's directory as embed.html. Then add some text to the file to note that the file is embedded in the RES file. That way, you'll know for sure that you have the right file when it is displayed.

Then, of course, you need to embed this HTML into your application. Delphi easily manages this via RC files, automatically compiling them and adding them to an application. Therefore, use Notepad or some text-handling tool to create a text file, and call it HTML.RC. Save it in the same directory as your demo application and add it to your project. Then add this text to the RC file:

#define HTML 23 // HTML resource identifier
EMBEDDEDHTML HTML embed.html

When this is included in a Delphi project, Delphi will compile the RC file into a RES file and include it in your application.

When the HTML is in your app, create a new page with a TPageProducer and call it Embedded. Save the file as wmEmbedded. Then go to the Home page and select the LocateFileServices component. Go to the Object Inspector Events page and double-click on the OnFindStream event. You'll get an event handler similar to this one:

procedure THome.LocateFileServiceFindStream(ASender: TObject;
 AComponent: TComponent; const AFileName: String;
 var AFoundStream: TStream; var AOwned, AHandled: Boolean);
begin

end;

The key parameters here are the AFileName and AFoundStream parameters. You'll use them to get the HTML from the embedded resources. Make your event handler resemble the following:

procedure THome.LocateFileServiceFindStream(ASender: TObject;
 AComponent: TComponent; const AFileName: String;
 var AFoundStream: TStream; var AOwned, AHandled: Boolean);
begin
 // we are hunting up the Embedded file
 if Pos('EMBEDDED', UpperCase(AFileName)) > 0 then  begin
  AFoundStream := TResourceStream.Create(hInstance, 'EMBEDDED', 'HTML');
  AHandled := True; // no need to look further
 end;
end;

AFileName will be the unqualified name of the HTML file that Delphi would use as a default. You can use that name to determine which resource to look up. AFoundStream will be nil when passed into the event handler, so it's up to you to create a stream using the variable. In this case, AFoundStream becomes a TResourceStream, which grabs the HTML from the resources in the executable. Setting AHandled to True ensures that the LocateFileServices makes no further effort to find the HTML content.

Run the application, and you'll see your HTML show up when you display the Embedded page.

  • + Share This
  • 🔖 Save To Your Account