Creating an Active Server Page

The simplest way to create an ASP page is to change the extension of an existing HTML document, from HTML (or HTML) to ASP.Then place the file in a new folder under your Web server’s root folder. In this chapter, all examples will reference ASP pages in the ASP pages folder, and you’ll find these examples in the ASP pages folder on the CD that comes with this book.

The date and time are displayed from within a client-side script, which calls the functions Date() and Time(). This script is executed on the client, and as a consequence, the date and time are read from the local computer’s clock.

We’ll revise this page to display the time on the server. To do so, we’ll add statements that are executed on the server. Copy the DATETIME.HTM file to the SRVRTIME.ASP file and replace the script with the following:

The Response object is equivalent to the Document object, but the server can’t access the Document object, which is available only on the client. Instead, it must use the Response object’s Write method to send output to the client.Everything you “write” to the Response object is placed in the output stream and sent to the client as if it were an existing HTML document. The new page will produce the same output, but the date and time displayed will be the date and time on the server. The modifier RUN AT in the <SCRIPT> the tells the Active Server Pages to execute this script on the server, not on the client.

Here’s a more useful ASP page along the same lines. This page, GREET.ASP, displays a different greeting depending on the time of the day.

The statements between tile first pair of server-side tags «% and %» do not produce any output for the client. They simply assign the proper value to the . greeting« variable. The value of this variable is then sent to the client by the line <% ‘”greeting so. The expression “=variable’~ (without the quotes, of course) is replaced by the value of the specified variable when the script is executed on the server, The rest is straight HTML, which is rendered on the client as usual.

HTML and server-side statements can coexist on the same line too. The last example could have been implemented as follows:

Your server-side script can also call procedures you supply in addition to builtin procedures such as the Date() and Time() functions. You can write a function that accepts a numeric value and returns it as a string (the value 96, for example, as “ninety-six”). To call this function from within your script, enclose its name in the server-side script tags:

The actual definition of the function must appear somewhere in the same file. Since the entire procedure will be executed on the server and since it doesn’t contain any HTML’Code to be sent to the client, you can place it in a pair of <SCRIPT> tags, similar to the client-side scripts, with an added qualifier:

The procedure will be executed and return a string, which can be used in the script.

Scroll to Top