Server-Side Scripting:
Active Server Pages (ASP)

OBJECTIVES

Learn how an ASP page is processed by a Web server

Identify the differences between client-side scripts and server-side scripts

Learn about the built-in objects in the ASP Object Model

Create Active Server Pages

Create server-side include files

Introduction to ASP

An Active Server Page (ASP) is a Web page that contains server-side script. ASP pages may also contain HTML and client-side script, but only the server-side scripts are executed on the server.

ASP pages carry the file extension .asp, as opposed to .htm or .html. When the Web server recognizes the .asp file extension, it sends the request to the ASP engine. The ASP engine is actually a dynamic-link library called asp.dll, which is installed on the Web server.

When a browser recognizes a server-side script on a file, it sends it to the ASP engine. The ASP engine first executes the scripts in the global Application file (global.asa). The Global Application file contains scripts that are executed when the Web application starts and ends.

The ASP then includes any server-side include (SSI) file indicated in the ASP page. The server-side scripts in the ASP page are then interpreted line by line (not compiled); although JavaScript block statements a re executed first, then inline script statements (<%%>), and then VBScript block statements.

ASP Object Model


There are several built-in objects that can be used within an ASP page to facilitate Web application development. Together, they are called ASP object model. There are six built-in objects as discussed below.

Request Object: Used to access information that is passed from the client’s browser to the Web server. For example, the Request object is used to retrieve information that a user entered in a form.

Response Object: Used to send information from the Web server to the client’s browser. For example, the Response object is used to redirect the user to another Web page.

Session Object:: Used to share information between a single client and the Web Server. The session object is used to store data during the course of a session. The session begins when the browser makes its first request for an ASP page. For example, the name of the visitor can be shared as a session variable.

Application Object: Allows a developer to treat a sequence of Web pages as a single application, which means that information, such as the name of the application or the total number of visitors on the Web site, can be shared among all the users of an ASP application.

Server Object: Used to access the Web server’s properties and methods. For example, the CreateObject method instantiates new COM objects on the server. COM objects are objects that are installed on the server and conform to the Component Object Model.

ASPError Object:: Used to retrieve information about the last error that occurred. The GetLastError method of the server object provides access to the ASPError object.

Each of these objects have methods and properties that server scripts can access. Some of them are explained below.

Request Object

Property/Method/Event / Name / Description
Property / TotalBytes / The size of the request (in bytes)
Property / ClientCertificate / A special security object that contains the user’s electronic signature; used in implementing security in ASP
Property / Cookies / A collection of cookies
Property / Form / Values from the form elements sent by the browser
Property / ServerVariables / Values of the Web server’s environment
Method / BinaryRead / A method that is used to retrieve data sent to the server as a part of the user’s POST request

Response Object

Property/Method/Event / Name / Description
Property / Cookies / Contents of the cookie collection to be sent to the browser
Property / buffer / A property that indicates whether to buffer the contents of the response until complete
Property / status / The status of the HTTP request, as returned by the server
Method / Appendtolog / A method that adds text to a Web server log
Method / Binarywrite / A method that sends text to the browser without any text conversions; as a result, the object will not add HTML tags to the data before sending it
Method / clear / A method that removes any buffered output
Method / end / A method that stops processing the page and returns the results to the browser
Method / flush / A method that sends buffered output immediately
Method / Redirect / A method that redirects a browser to a different URL
Method / write / A method that writes text or variables to the current page as a string

Application Object

Property/Method/Event / Name / Description
Property / Contents / A collection of all items (such as variables and objects) created during the processing of ASP scripts
Property / StaticObjects / A collection of all items added through the <OBJECT> tag
Method / lock / A method that prevents any other client from modifying the Web application’s properties
Method / unlock / A method that allows other clients to modify the Web application’s properties
Event / onstart / An event that fires when an application starts
Event / onend / An event that fires when an application exits

Session Object

Property/Method/Event / Name / Description
Property / Contents / A collection of all items created during the processing of ASP scripts
Property / StaticObjects / A collection of all items added through the <OBJECT> tag
Property / timeout / A property that sets the timeout property for the session state in the application (in minutes)
Method / Abandon / A method that ends a session
Event / onstart / An event that fires when a new client accesses a Web application
Event / onend / This event fires when a client ends the session

Creating Basic Active Server Pages

Any text editor or Visual editor, such as Notepad, FrontPage, Visual Studio, or Microsoft Script Editor can be used to create Active Server Pages.

To view an ASP page, you must view the page via a Web server that supports ASP.

Block Scripts

Server-side scripts are written inside block script tags or inline script tags. Block script tags use the same script tag as client-side script. However, to distinguish a client-side script from a server-side script, you must use the runat attribute with the assigned value server. Below is the syntax for adding a sever-side script to an ASP page.

<script language=”vbscript” runat = “server”>

action and control statements

</script>

Inline Scripts

Inline script tags are used to delimit inline scripts. The inline script tags <% %>, which will be run before block script tags, are used to indicate inline script. Below is the syntax form adding an inline server-side script to an ASP page.

<%

action and control statements

%>

or

<% action or control statement %>

For example, the response object is used to send output from the Web server to the browser. Below is the syntax for the write method.

<%response.write(expression)%>

All the VBScript techniques that we have learned so far can be used in server-side scripts.

An Example using Response Object:

<% @language = "vbscript" %>

<html>

<head>

<title>Server side script example</title>

</head>

<body<h1>

<%dim strHello = "Hello World! "

respsonse.write strHello

%>

</h1>

</body>

</html>

Syntax of Code / Explanation
<% @language = “vbscript" %> / Specifying the language at the top of the Web page using the @language attribute is a good idea. VBScript is the default language.
<%dim strHello = "Hello World!" / The beginning of the server-side script. A variable strHello is given an initial value.
respsonse.write strHello / This line sends output to the browser
%> / The ending server-side script tag

Important Note:

The equal sign (=) is a shortcut key that can be used instead of typing out response.write.

<% = strHello %>

However, you can only use the shortcut key for one statement. If the script contains more than one statement, you cannot use the shortcut key.

The same ASP page above could be written using block script tags.

<% @language = "vbscript" %>

<html>

<head>

<title>Server side script example</title>

</head>

<body>

<script language ="vbscript" runat = "server">

response.write"<h1>Hello World!</h1>"

</body>

</html>

Server-side Programming

Server-side scripting is similar to client-side scripting. You can use any valid VBScript or JavaScript statement within a server-side script delimiter.

The following sample code uses server-side script to display the current weekday name, month name, day, and year. Remember since this is server-side script it uses the server’s date and time.

<%response.write WeekdayName(Weekday(Date)) & ", " & _

MonthName(Month(Date)) & " " & _

Day(date) & ", " & _

Year(Now())

%>

Syntax of Code / Explanation
<%response.write WeekdayName(Weekday(Date)) & ", " & / The Weekday function is used to obtain the index number of the day of the week.
The WeekdayName function is used to convert the index number to the name.
MonthName(Month(Date)) & " " & _ / The Month function is used to obtain the index number to a name.
The MonthName function is used to convert the index number to the month.
Day(date) & ", " & _ / The Day function retrieves the day of the month.
Year(Now()) / The year is obtained using the Now function instead of the Date function because when the Year function is applied to the Date function it returns the number “1899” when the actual year is “2000”.
%> / The ending server-side script tag
Note: / The underscore character is used so that the statement can extend over multiple lines to increase readability.
Most programming languages have some sort of tag that allows this process. Remember, because of this you cannot use the underscore character within a string.

You can also use control statements on the Web server to specify which HTML is sent to the browser.

<% if intA > 0 then %>

<font color=”ff0000”>

<h1>The variable is not equal to zero so we print in RED</h1>

<% else %>

<font color=”00FF00”>

<h1>The variable is equal to zero so we print in Green</h1>

<% end if %>

If the variable intA is not equal to zero only the red print will appear, and if the variable is equal to zero then just the green print will appear.

Programming with ASP Built-In Objects

In addition to all of the scripting language objects, properties, and methods, server-side scripts have access to all of the built in ASP objects, properties, and methods.

You can access built-in ASP objects in much the same way that you access objects in the Document Object Model. Objects and their methods and properties are referred to using dot notation.

response.write “ …. “

Example of Request Object:

The following example uses the request object and the response object. This example demonstrates how to combine basic scripting techniques with ASP built-in objects.

<html<head>

<title>Using Built-in ASP Objects</title>

</head>

<body>

<%

if request.form("txtName") > "admin" or _

request.form("txtPass") > "pass" then

%>

<h1 align="center>Login Form</h1>

<from method ="post action ="builtinObjects.asp">

<input type="text" name="txtName" size="20"> Name

<br<br>

<input type="password" name="txtPass" size="20"> Password

<br<Br>

<input type="submit" value="Login" name="btnSubmit">

</form>

<% else %>

<h3 align="center">Welcome Back!</h3>

<% end if%>

</body</html>

Syntax of Code / Explanation
if request.form("txtName" > "admin" or _
request.form("txtPass") > "pass" then / This line of code checks the login name and password from the browser. If it is not equal to the correct name and password then the form is shown again.
<% else %>
<h3 align="center">Welcome Back!</h3>
<% end if%> / If the correct password and login name are sent from the browser then Welcome Back will be displayed.

View this file
Example of Request and Session Object:

The following example shows how to use the request object along with another built-in ASP object called the session object.

<%

Session.Timeout = 30

Session.Contents("sStart") = Now()

dim strPathInfo = Request.ServerVariables("PATH_INFO")

strPhysicalPath = Server.MapPath (strPathInfo)

%>

<html<head>

<title>Using more Built-in ASP Objects</title>

</head>

<body>

<div align="center">

<h1>Built in Objects</h1>

<h3>Path Information</h3>

The virtual path to this file is: <br>

<% = strPathInfo %>

<br<br>

The physical path to this file is: <br>

<% = strPhysicalPath %>

<br<Br>

<h3>Session Information</h3>

The session timeout has been set to: <br>

<% = session.timeout %> minutes

<br>

Your session started at: <br>

<% = Session.Contents("sStart") %>

<br>

Your session ID is: <br>

<% = Session.SessionID %>

</div>

</body</html>

Syntax of Code / Explanation
Session.Timeout = 30 / This changes the timeout property to 30 minutes from the default 20 minutes. When the timeout limit is reached, the session will end, and all session-related information will be lost.
Session.Contents("sStart") = Now() / This assigns the current date and time to a session variable called sStart.
strPathInfo = Request.ServerVariables("PATH_INFO") / This virtual path returned from the server is returned from the PATH_INFO server variable and stored in the strPathInfo variable.
strPhysicalPath = Server.MapPath (strPathInfo) / The MapPath method is applied to the virtual path stored in the strPathInfo variable to retrieve the physical path.
The virtual path to this file is: <br>
<% = strPathInfo %> / Displays the virtual path to this file.
The physical path to this file is: <br>
<% = strPhysicalPath %> / Displays the physical path to this file.
The session timeout has been set to: <br>
<% = session.timeout %> minutes / Displays the new timeout property.
Your session started at: <br>
<% = Session.Contents("sStart") %> / Displays the time the session was started.
Your session ID is: <br>
<% = Session.SessionID %> / This displays the session ID number. The SessionID property of the session is one way to identify the client to the Web server.

Server-side Include Files

Many languages contain a method to include the contents of one file in another file. Server-side include files (SSI) are often used to insert code that can be reused for other Web pages, for example headers and footers.

The server-side include file may contain header or footer information, style information, logos and images, constants, other expressions, formulas, or meta tag information. This content may be composed of HTML tags, text, or client- or server-side scripts.

A server-side include file containing information that is displayed on the top of the Web page is often named “head.inc” or “header.inc”.

Note: / The SSI file will be included before the ASP engine interprets the page. Therefore, you cannot use ASP code to decide which server-side include file to include within a Web page. Furthermore, all statements in the server-side include file will be included.

Example of SSI file:

The sample include file below will write out a title to the Web page, and the date.

<div align="center">

<h2>Garden Supply Store</h2>

<i>555 Main Street<br>

Chicago, IL60011</i>

<br<br>

<font face="Verdana" size="2">

Today is <% = now() %>

<br<br>

<HR width="75%" size="4" color="#008000">

</div>

The example is a simple mix of HTML and inline script. It should be saved with an .inc extension and usually SSI files will be in their own directory named, /inc.

To instruct the Web server to include a server-side include file, use an include statement. Below is the syntax for including a server-side include file in an ASP page.

<!--#include = path & filename -->

The include file above can be included in an ASP page with the following code.

<html<head>

<title>Using Include Files</title>

</head>

<body>

<p&nbsp;</p>

<!--#include file ="filename.inc"-->

</body>

</html>

The <!--#include file ="filename.inc"--> line tells the Web server to include the filename.inc file.

Discussion

Discuss the advantages of using server-side scripts over client-side scripts

Discuss the order in which scripts are interpreted by a the scripting engines

Discuss the built-in objects in the ASP Object Model

Use of SSI (server-side include files)

Discuss some of the links to ASP-related Internet resources

1