Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Dr.Bob's Delphi Notes Dr.Bob's Delphi Clinics
 Guessing using WebBroker
See Also: other Dr.Bob Examines columns or Delphi articles

Some of you may have seen the discussion of IntraWeb and WebSnap, where a simple application was implemented in both IntraWeb and WebSnap. The application was meant to show the differences in the tools (for a simple application). However, the example application is so simple, that it was - in my personal view - too simple for WebSnap. I would never have used WebSnap for a number-guessing application, but I would use WebBroker instead.
So, without wanting to say anything bad about IntraWeb or WebSnap, I just want to present my WebBroker solution (also written in five minutes), which works with Delphi 3 and higher, Kylix 1 and higher, and even with C++Builder 3 and higher!

Implementation WebBroker
Files

  1. GuessWebBroker.dpr
  2. WebMod.dfm
  3. WebMod.pas
  4. WebMod.html (can be integrated into WebMod.dfm)

GuessWebBroker.dpr

  program GuessWebBroker;
  {$APPTYPE CONSOLE}
  uses
    WebBroker, CGIApp,
    WebMod in 'WebMod.pas' {WebModule1: TWebModule};

  begin
    Randomize;
    Application.Initialize;
    Application.CreateForm(TWebModule1, WebModule1);
    Application.Run;
  end.

WebMod.dfm

 object WebModule1: TWebModule1
   OldCreateOrder = False
   Actions = <
     item
       name = 'webactionitem1'
       onaction = webmodule1webactionitem1action  
     end>
   Height = 102
   Width = 175
   object PageProducer1: TPageProducer
     HTMLFile = 'WebMod.html'
     OnHTMLTag = PageProducer1HTMLTag
   end
 end

  unit WebMod;
  interface
  uses
    SysUtils, Classes, HTTPApp, HTTPProd;

  type
    TWebModule1 = class(TWebModule)
      PageProducer1: TPageProducer;
      procedure PageProducer1HTMLTag(Sender: TObject; Tag: TTag;
        const TagString: String; TagParams: TStrings;
        var ReplaceText: String);
      procedure WebModule1WebActionItem1Action(Sender: TObject;
        Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
    private
      MagicNumber, Guess, GuessCount: Integer;
    end;

  var
    WebModule1: TWebModule1;

  implementation
  {$R *.dfm}

  procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
    Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
  begin
    MagicNumber := StrToIntDef(Request.ContentFields.Values['MagicNumber'],0);
    if MagicNumber = 0 then MagicNumber := 1 + Random(100);
    GuessCount := StrToIntDef(Request.ContentFields.Values['GuessCount'],0);
    Guess := StrToIntDef(Request.ContentFields.Values['Guess'],0);
    Response.Content := PageProducer1.Content
  end;

  procedure TWebModule1.PageProducer1HTMLTag(Sender: TObject; Tag: TTag;
    const TagString: String; TagParams: TStrings; var ReplaceText: String);
  begin
    if TagString = 'MagicNumber' then
      ReplaceText := IntToStr(MagicNumber)
    else if TagString = 'GuessCount' then
      ReplaceText := IntToStr(Succ(GuessCount))
    else if TagString = 'Guess' then
      ReplaceText := IntToStr(Guess)
    else if (TagString = 'Response') and (GuessCount > 0) then
      if not (Guess in [1..100]) then
        ReplaceText := 'Guess must be between 1 and 100'
      else if Guess < MagicNumber then ReplaceText := 'Too low!'
      else if Guess > MagicNumber then ReplaceText := 'Too high!'
      else ReplaceText := '<b>Yes!</b>' +
        ' Try Again? <a href="GuessWebBroker.exe">Yes, please!</a>'
  end;

  end.

WebMod.html

  Please guess the number from 1 to 100:
  <form action="GuessWebBroker.exe" method="post">
  <input type="hidden" name="MagicNumber" value="<#MagicNumber>">
  <input type="hidden" name="GuessCount" value="<#GuessCount>">
  Guess <#GuessCount>: <input type="text" name="Guess" value="<#Guess>">
  <input type="Submit" value="Guess">
  <p><#Response></form>


You can try the WebBroker version on Windows or Linux (implemented using Kylix).


This webpage © 2002-2015 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.