To help you create Web applications, the Active Server provides the following objects that supply the functionality that is riot necessary (as is the functionality of the builtin objects), but that is commonly used in building applications for the Web.
This object provides connectivity to any ODBC-compliant database . or OLE DB data source. It is based on the Active Data Objects and allows Web developers to easily link a database to an active Web page to access and manipulate data.
File Access This is another useful object that lets you access text files stored on the server: As with client-side scripts, server-side scripts must be safe for the host system. The file Access object is fairly safe, because it allows developers to write and read text files only in specific folders on the server.
Advertisement Rotator As people realize the potential of the Web as a business medium and more and more sites become commercial, the need to manage ads on a Web increases. Web managers can’t afford to manually place ads in their designs. Ads must not only be rotated, but selected according to user preferences. If your Web site has a search engine, it wouldn’t make sense to advertise cars to a visitor who’s looking for books or advertise magazines to people who are searching for programming tools, To simplify the handling of ads on a Web site, Active Server Pages comes with the Advertisement Rotator object. You can use this object to display ads based en preset criteria every time an ASP file is requested.
In this chapter, we’ll cover only a few of the basic components of Active Server Pages. We’ll discuss the built-in objects and a few of the basic objects, which will help you leverage your VB knowledge to the Web. We won’t get into every ASP related topic.
The Response Object
Use the Response object to send information to the client. ASP files can contain straight HTML code that’s sent directly to the client. However, if you want to control the output programmatically from within the script, you must write it to the Response object. The Response object supports the following methods and properties.
The Write Method
Everything you write to the Response object with the Write method is sent to the client. Normally, the information written with the Write method must be an HTML document, just like the Write method of the Document object. Here’s a simple, but working example of the Response object’s Write method.
The outer pair of HTML tags delimits the ASP file (they are not really required), and the pair of HTML tags written to the Response object delimit the HTML file seen by the client. If there’s one method used in nearly every ASP file, it is the Response .Write method, and you will see many more examples of it in this chapter.
The Write method of the Response object is the primary way to send data to the client. The example is quite trivial. You could use straight test and HTML tags to produce the same output, or you could place the same arguments in the Document.Write method. All three methods would produce the same output.
The Redirect Method
This method redirects the client to another URL. If you move your site to another URL, write a short ASP application such as the following to redirect the visitor automatically to the new URL.
The new URL variable is the Web site’s new URL.
.The Clear Method
nus method clears the data written to the Response object and is used only when the Buffer property is set to True. Normally, the output is buffered until the entire page has been processed, and only then is it sent to the client. To force the information collected so far in the output stream to be transmitted to the client, call the Response object’s Clear method.
The Buffer Property
By default, the Response object sends its output to the client as soon as it can, and it doesn’t wait for the entire page to complete. If you want to process the entire page before sending any output to the client, set the Buffer property of the Response object to True. Let’s say that as you process the page (with an ActiveX component or by reading data from a database), you discover that you need not send the previous data to the client or that you must redirect the client to another URL. If the Buffer property was Set to True, you can cancel the processing and clear the Response object with the Clear method of the Response object. Here’s a typical scenario:
The Query String Collection
The collection you’ll be using most often in developing ASP applications is the QueryString collection. This property gives you access to all the parameters passed along with the URL by the client. If the names of the parameters are known at the time you develop the ASP file, you can request their values by name, with expressions such as the following:
If the names of the parameters are not known when you develop the ASP file or if you want to process them all serially, you can use a loop such as the following:
It shows a simpleHTML page with a Form (the SRVRFORM.HTM page on the CD); the user can enter information by selecting options in list ~es. When done, they can click the Recalculate button to submit the information to the server, where the cost of the selected configuration will be calculated and sent back to the client as a new HTML page.
The names of the ListBox controls on the SRVRFRM page are: HardDisk, Memory, CD, Speaker, and Software. The possible values for each setting are listed in the <OPTION> tags of each <5ELECf> tag. The options of the Select Memory list box have the values 32, 64, 128, and 256. The Memory query parameter will have one of these values, depending on which option was selected on the Form.
The ASP page that processes this Form is called PARAM.ASP, and it resides in the ASP pages virtual folder on the server (its URL appears in the FORM tag). This page doesn’t do much. (It doesn’t actually recalculate the price of the configuration, but once you know the options specified by the visitor, it’s relatively easy to calculate the price of the ‘selected configuration.) The PARAM.ASP script generates another HTML page on the fly with the names and values of the parameters and sends it back to the client. The server’s response is shown.
The PARAM.ASP page that processes the data on the server uses a For … Each loop to soon all the items of the QueryString collection and display their names and values in a new table. Each parameter is stored in the-variable PValue. The default property of this variable is the name of the parameter. To access the value of the parameters, use the expression Params(PValue).
To access individual parameters, you can use the collection Request.QueryString, followed by the name of the parameter whose’ value you want. The following lines will return the-specifications Of the memory and the hard disk, as entered by the visitor on the Form in the page SRVRFRM.HTML.
The Form Collection
This collection is similar to the QueryString collection in that it contains data that the visitor entered on a Form. Whereas the QueryString collection contains all the parameters submitted to the server, regardless of whether they belong to a Form, the Form collection contains the values of the parameters that come from controls. It is possible to build a URL followed by a number of parameters on the fly, from within a client script. These parameters will be reported by the QueryString collection, but not by the Form collection.
To access the value of a specific parameter in the Form collection, use a statement such as the following: