TutWebAppServer Lesson 2 - Hello Today

From Overbyte
Jump to navigation Jump to search

Let's add a dynamic webpage to our website. To make it simple, we will just display the current server date and time.


  1. Create the template for the dynamic web page: Menu / File / New / Others / web document / HTML Page.
  2. Write the text: "Server time is"
  3. Switch to the HTML code editor (ALT-PgUP or click "code" tab at bottom of the editor) to enter the special tag "<#DateTime>" where you want to have the date and time shown, that is just after "Server time is". You'll see that Delphi HTML editor add an ending tag we don't need. Just delete "</#DateTime>" Delphi has inserted.
  4. Save the template as "c:\icstutorial\templates\HelloToday.html"
  5. Now we must associate the processing required to build our dynamic webpage. This is done by creating a new class deriving from TUrlHandler. Menu / File / New / Other / Delphi Project / Delphi Files / Unit. Save this new unit as "OverbyteIcsTutWebAppServerHelloToday.pas". Enter this code into the unit:
    SysUtils, OverbyteIcsHttpAppServer;

    TUrlHandlerHelloToday = class(TUrlHandler)
    procedure Execute; override;


    procedure TUrlHandlerHelloToday.Execute;
    AnswerPage(’’, ’’, 'HelloToday.html', nil, ['DateTime', DateTimeToStr(Now)]);
  6. Switch back to OverbyteIcsTutWebAppServerMain and add "OverbyteIcsTutWebAppServerHelloToday" to the uses clause, add the following line in front of the FormShow event handler:
    HttpAppSrv1.AddGetHandler('/HelloToday.html', TUrlHandlerHelloToday);
  7. Hit CTRL-SHIFT-S and F9 to save all, compile and run your application.
  8. Enter this URL into your browser: You should see the web page as the template your designed with the tag "<#DateTime>" replaced by the date and time. Hit the refresh button several times to see the dynamic page in action: the time is the real time.


In this lesson you learned how to write a template for a dynamic page, link the dynamic page URL to a Delphi class, write a Delphi class implementing the processing required to get data and feed it to the template.

Next lesson: Simple data entry and processing
Previous lesson: Hello World
Tutorial presentation: TutWebAppServer