TutWebAppServer Lesson 5 - Relocation
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:
- http://127.0.0.1:20080/RelocateMe?parameter=1
- The even page is RelocateMeEven.html
- The odd page is RelocateMeOdd.html
Steps
- Write the RelocateMeEven.html static page. Just enter this in the body:
Even !
Save the file in c:\icstutorial\wwwroot\RelocateMeEven.html. - Write the RelocateMeOdd.html static page. Just enter this in the body:
Odd !
Save the file in c:\icstutorial\wwwroot\RelocateMeOdd.html. - Create a new Delphi unit "OverbyteIcsTutWebAppServerRedirectionDemo.pas". Create a new class to handle the URL. The code looks like:
- unit OverbyteIcsTutWebAppServerRedirectionDemo;
- interface
- uses
- Windows, SysUtils, Classes, OverbyteIcsHttpAppServer, OverbyteIcsHttpSrv;
- type
- TUrlHandlerRelocateMe = class(TUrlHandler)
- public
- procedure Execute; override;
- end;
- implementation
- procedure TUrlHandlerRelocateMe.Execute;
- var
- Parameter : String;
- Location : String;
- begin
- ExtractURLEncodedValue(Params, 'Parameter', Parameter);
- if (StrToIntDef(Parameter, 0) and 1) = 0 then
- Location := '/RelocateMeEven.html'
- else
- Location := '/RelocateMeOdd.html';
- AnswerString('302 moved', ’’,
- 'Location: ' + Location + #13#10 + NO_CACHE,
- '<h tml><body><a HREF="' + Location + '">Click here</a></body></html>');
- Finish;
- end;
- end.
- unit OverbyteIcsTutWebAppServerRedirectionDemo;
- 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.
- You can now point your browser to http://127.0.0.1:20080/RelocateMe?parameter=1 and test. You should get the "Odd" page since the parameter is odd. Enter http://127.0.0.1:20080/RelocateMe?parameter=2 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.
Summary
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