| RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To |
| ISAPI tutorial for Delphi developers | ||||||||||||||||
| Page 6: A close look at the TWebRequest object | ||||||||||||||||
So, up to this point we know how to create a simple ISAPI application, insert the code needed and run it. In this chapter I am going to examine the TWebRequest object, covering it's most important methods and properties.
The TWebRequest contains the information that is made by the client request and passed on to the web server application. It has three descendants, depending on the application you are working with:
1. TISAPIRequest (if you are creating an ISAPI/NSAPI application).
2. TCGIRequest (if you are creating a CGI console application).
3. TWinCGIRequest (if you are creating a WinCGI application).
We will be focusing on the TISAPIRequest object since the examples in this tutorial are mainly based on ISAPI's. However, if you list the properties, methods of each one you can see that they are basically the same.
I am not going to cover each and every method and property in this chapter, only the ones I see as relevant for this tutorial. If I did, it would basically be a copy of the Delphi help files, and since we all have those, I'll leave it at that.
Properties http://localhost/scripts/chapter6.dll /action?target=10
Referer RemoteAddr UserAgent Let's pause for a minute here. Taking into account the information we have up to now, see how easy it would be to make a Statistics application for your web-site. With the Referer you could see what page is being visited. With RemoteAddr you could see the user's IP address and therefor find out what domain it corresponds to, thus extracting the country of origin. Finally with the UserAgent property you could see what browser and operating system they are using. The first ISAPI application I ever made (and had to, so it wan't by choice) was an auditing system that did what I have just described. It additionaly inserted this information into a database and made graphics for you the customer to see a report on the hits to his/her website. If you think about it, it's actually very easy to do.
The next two properties we are going to examine are the most basic and important ones. They let us interact and exchange information with the client. I am referring to the ContentFields and QueryFields properties.
ContentFields Well, when submitting information to a DLL (or CGI) application, you are using the HTTP Post method. Below is an example of what a form looks like:
<FORM METHOD="post" ACTION="http://localhost/scripts/chapter6.dll/form">
Name:<br><input type="text" name="NAME_FIELD"><br>
SurName:<br><input type="text" name="SURNAME_FIELD"><br>
<input type="submit" name="Submit" value="Submit"><br>
<input type="reset" name="reset" value="Reset">
</FORM>
(Note that I have taken tags like Font Face, Paragraph, etc out do make it simple).
First, let's take a look at the form tag. It indicates that the information is going to be submitted via POST method. It then tells what action it's going to call (this is basically the same actions that your application consists of). In this case, it's going to call the action "form" of our "chapter6.dll".
Next we have the fields that the form contains. The first one is of type TEXT and is called NAME_FIELD. The next one is called SURNAME_FIELD.
Lastly, there are two buttons. The first one is the one that actually submits the information contained in the form to the action specified. The second one just resets the form.
IMPORTANT: Only one Submit is performed per form. Note that the actual action isn't associated with the SUBMIT button, but with the FORM. You can never make a form submit to two different places with just plain HTML. There are ways to do this with JavaScript and it's should be quite simply (I'm not going to get into that right now though).
So, what has all this got to do with the ContentFields property we were talking about? Well, the ContentFields property actually contains all the information that is in the form. Let's take a look at our next example program. This example makes use of the ContentFields property to extract the name and surname and create a response that contains the phrase: "Hello Name Surname, how are you doing today?".
Let's look at the code:
Response.Content := 'Hello '+ Request.ContentFields.Values['NAME_FIELD'] + ' ' +
Request.ContentFields.Values['SURNAME_FIELD'] + ' how are you today?';
Well, that looks easy. As you can see, to retrieve the values of an HTML form field, all you need to do is access the stringlist of values of the ContentFields, indicating the name you have the field in the HTML form.
Of course, this works with TEXT fields. What about things like a CHECKBOX. Again, this is quite simple. When you post a form that contains a checkbox to your application, if the box is ticked (checked), the value will be in the ContentFields. If it is not checked, then the field won't even be contained in the ContentFields. For example, if you have a CHECKBOX named Tick1:
to actually see if the form was posted with the checkbox ticked, you could do something like:
if Request.ContentFields.IndexOfName('Tick1') <> -1 .....
We will examine the ContentFields with more detail in later chapters when we need to make use of the properties. This is just a small sample to show you how to access the fields.
QueryFields Let's take a look at the code for this example.
We'll examine the SendRedirect method in the next chapter. The idea here is to see how to access the QueryFields values. Again as with the ContentFields you can use the Values property indicating the name of the field. All this action does is re-direct the call to the URI specified.To run the example, open your browser and type different URI's. Make sure you have the DLL copied to the correct location.
We will examine more properties of the TWebRequest object in later chapter, as we need them.
Methods GetFieldByName ReadClient The basic methods and properties needed to start doing some serious applications are covered now. In the next chapter I'll examine the TWebResponse object.
Next page > Responding to a request > Page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
|
||||||||||||||||
All graphics (if any) in this feature created by Zarko Gajic.
| More Delphi |
|
· Learn another routine every day - RTL Quick Reference. · Download free source code applications and components. · Talk about Delphi Programming, real time. · Link to the Delphi Programming site from your Web pages. · Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC. |
|
· NEXT ARTICLE:
ADO Cursors - DB/10. Chapter ten of the free Delphi Database Course for beginners. How ADO uses cursors as a storage and access mechanism, and what you should do to choose the best cursor for your Delphi ADO application. |
| Stay informed with all new and interesting things about Delphi (for free). |
|
|
| Got some code to share? Got a question? Need some help? |

