TutWebAppServer Lesson 2 - Hello Today
Let's add a dynamic webpage to our website. To make it simple, we will just display the current server date and time.
- Create the template for the dynamic web page: Menu / File / New / Others / web document / HTML Page.
- Write the text: "Server time is"
- 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.
- Save the template as "c:\icstutorial\templates\HelloToday.html"
- 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)]);
- 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);
- Hit CTRL-SHIFT-S and F9 to save all, compile and run your application.
- Enter this URL into your browser: http://127.0.0.1:20080/HelloToday.html. 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.