TutWebAppServer Lesson 2 - Hello Today
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.
Steps
- 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:
- uses
- SysUtils, OverbyteIcsHttpAppServer;
- uses
- type
- TUrlHandlerHelloToday = class(TUrlHandler)
- public
- procedure Execute; override;
- end;
- type
- implementation
- procedure TUrlHandlerHelloToday.Execute;
- begin
- AnswerPage(’’, ’’, 'HelloToday.html', nil, ['DateTime', DateTimeToStr(Now)]);
- Finish;
- end;
- 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.
Summary
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