|
The third way to maintain state is with "Fat URLs" like the following which shows how a search on HotBot can be saved: http://hotbot.lycos.com/?MT=Corbin%27s+treehouse
You can easily implement this in Delphi, and because of its simplicity it is one of the more common ways of saving state that is easily 'bookmarkable.'
The first thing we are going to do is take a closer look at a GET request (aka, Fat URL). Typically, they will look something like:
http://www.mydomain.com/scripts/myIsapi.dll/PathInfo?name1=value1&name2=value2
The first part (http://www.mydomain.com/scripts/myIsapi.dll/PathInfo) contains the usual information; the domain name, followed by the path to the ISAPI dll (or CGI), and then the PathInfo that specifies which action to invoke. Immediately after that is a question mark that signifies the start of the query data, or Fat URL. It is then followed by Name=Value pairs separated by ampersands.
In Delphi, the way you access these values is with the Request.QueryFields.Values['Name'] object which returns the value associated with a given Name.
Something to be aware of is that the web browser has to URL encode the data sent via the GET method. If you use a form to send the data with a GET method (as which is done in my example below), then you don't have to worry about anything. If you want to construct a Fat URL in code, you have to be sure to URL encode any possible Name and Value pairs. In Delphi, with the NetMaster's TNMURL component, this is a piece of cake. Here is an example:
function Encode(const str: string): string;
begin
NMURL1.InputString := str;
Result := NMURL1.Encode;
end;
var
strFatURL: string;
begin
strFatURL := 'http://MyMachine/scripts/MyIsapi.dll/Path?' +
Encode('Query Name Is')
+ '=' +
Encode('Corbin''s Tree House')
+ '&' +
Encode('&Another Name')
+ '=' +
Encode('Here"s The Result Value');
end;
Notice the small helper function "Encode" that encodes a given string with the TNMURL component and returns the encoded string. After execution, strFatURL contains: http://MyMachine/scripts/MyIsapi.dll/Path?Query+Name+Is=
Corbin%27s+Tree+House&%26Another+Name=Here%22s+The+Result+Value which is a properly URL encoded.
Delphi Example
Now it is time to add some code to the Delphi example we have been working on. Add another action to your web module and give it a new PathInfo string, such as '/get' (since GET is the method which a Fat URL is invoked by).
procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
strVisitNum: string;
begin
// Fat URL example (using the GET request method).
// When a web browser sends a Fat URL to the web server, you have
// to access the data sent via the Request.QueryFields, instead
// of the Request.ContentFields that are filled in with the POST
// method.
// First, check to see if the URL has any data sent in the Query
if Request.QueryFields.Count <= 0 then
begin
// Set the response to an HTML form with the GET method
Response.Content :=
'<form action="' +
Request.URL + Request.PathInfo +
'" method="get">' +
'Test saving state with Fat URL''s!<br>' +
'<input type="submit" name="counter" value="1">' +
'</form></body></html>';
end
else
begin
// Inc the counter and send back the count number
try
strVisitNum := Request.QueryFields.Values['counter'];
strVisitNum := IntToStr(StrToInt(strVisitNum) + 1);
except // Eat exceptions (conversion exceptions)
strVisitNum := '1';
end;
Response.Content :=
'<form action="' +
Request.URL + Request.PathInfo +
'" method="get">' +
'Test saving state with Fat URL''s!<br>' +
'<input type="submit" name="counter" value="' +
strVisitNum + '">' +
'</form></body></html>';
end;
end;
You will probably notice that the URL becomes something like:
http://MyMachine/scripts/MyIsapi.dll/get?counter=23, which means the user can very easily change the URL and the underlying request that is sent to the Web Application when a Fat URL is sent with the GET method. Of course, it is possible to do this with a POST method too, but it is more difficult because the user has to create an HTML form that sends the custom data to a Web Application.
This is just something to keep in mind. If you try to put counter=1.123 in the URL, and didn't catch the conversion exception like I do, your application would show an exception to the end user (which isn't very pretty).
|