TutWebAppServer Lesson 5 - Relocation

From Overbyte
Jump to navigation Jump to search

In this lesson you will learn how to redirect the client to another URL. This is the relocation process. Under some conditions, you may determine that the requested page is not the correct one. Instead of telling the user where the page is, you may instruct his browser to go directly to that page. In the next lesson, we will see a good place for a relocation: when a user has not logged on yet and can't be grant access to a page. He is then relocated to the login page. For now, we will do something simpler to demonstrate how to do the relocation: we will provide an URL taking one parameter. If that parameter is even, then the user is redirected to a given page and if the parameter is odd, the user is redirected to another page. We will redirect to static pages but there is defenitely no difference relocating to a dynamic page. It's just a matter of the redirection URL.

Let assume the URL is:


  1. Write the RelocateMeEven.html static page. Just enter this in the body: Even !
    Save the file in c:\icstutorial\wwwroot\RelocateMeEven.html.
  2. Write the RelocateMeOdd.html static page. Just enter this in the body: Odd !
    Save the file in c:\icstutorial\wwwroot\RelocateMeOdd.html.
  3. Create a new Delphi unit "OverbyteIcsTutWebAppServerRedirectionDemo.pas". Create a new class to handle the URL. The code looks like:
    unit OverbyteIcsTutWebAppServerRedirectionDemo;


    Windows, SysUtils, Classes, OverbyteIcsHttpAppServer, OverbyteIcsHttpSrv;

    TUrlHandlerRelocateMe = class(TUrlHandler)
    procedure Execute; override;


    procedure TUrlHandlerRelocateMe.Execute;
    Parameter : String;
    Location : String;
    ExtractURLEncodedValue(Params, 'Parameter', Parameter);
    if (StrToIntDef(Parameter, 0) and 1) = 0 then
    Location := '/RelocateMeEven.html'
    Location := '/RelocateMeOdd.html';
    AnswerString('302 moved', ’’,
    'Location: ' + Location + #13#10 + NO_CACHE,
    '<h tml><body><a HREF="' + Location + '">Click here</a></body></html>');

    As you can see in the code, a relocation is done by sending anything to the client with status "302 moved" and a "location" header line with the new URL. Here we send a simple HTML document without even bothering with a HTML file. The HTML document is given by a string. Of course we could have used an HTML file. Why do we return a document ? Because it may happend that the URL is accessed using something that do not automatically follow relocation. The document provide a simple link the user will have to click in case his browser is configured to ignore relocation.
  4. Now map the URL handler class to the actual URL. Add a single line in TTutWebAppServerMainForm.FormShow: HttpAppSrv1.AddGetHandler('/RelocateMe', TUrlHandlerRelocateMe); Don't forget to add the unit name to the uses clause so that the compiler understand the class name.
  5. You can now point your browser to and test. You should get the "Odd" page since the parameter is odd. Enter and you should get the "Even" page. Please pay attention to the URL displayed in your browser: it is the Odd or Even page's URL. This is typical of a relocation: the browser shows the url of the destination page.


During this lesson, you learned how to instruct the client browser to display the page from another location. You know that this process is known as "relocation".

Next lesson: Simple login and session data
Previous lesson: Building tables
Tutorial presentation: TutWebAppServer