ASP Core Objects

ASP includes several built-in objects that function at the processing tier. The core ASP objects are the:

  1. Request - used to retrieve information that a user entered in a form
  2. Response - used to redirect the user to another Web page
  3. Session - used to share information between a single client and the Web server
  4. Application - allows developers to treat a sequence of Web pages as a single application
  5. Server - used to access the Web server's properties and methods. For example, the CreateObject method instantiates new COM objects on the server.

We use each of the objects to access specific types of information in the processing tier and to store state information on the server.

Request Object

The ASP Request object represents the current URL request from a client. ASP creates a new Request object each time a client requests a URL. When a user clicks a link in their browser, ASP creates a Request object. ASP also creates a Request object when client-side JavaScript uses the document.location or history.go() methods. The Request object has the shortest lifetime of all the ASP built-in objects, since it exists only until the current request is fulfilled.

The Response object contains several collections that contain information about the client request.

Request Object Collections
Collection Contains
ClientCertificate Field values in the client certificate sent with the request
Cookies Cookies sent with the request
Form The value of named form elements in the document displayed in the browser
QueryString The name=value pairs appended to the URL in a query string
ServerVariables Environment variables

The form collection of ASP's Request object contains variables representing form elements from the requesting Web page. ASP takes all of the named elements in a form on the user's browser and adds them as variables to the Form collection of the Request object. Recall that when you click a form's submit button, each field on the form is submitted to the server as a name=value pair. The name portion of the name=value pair becomes a variable name in the Form collection, and the value portion is assigned as the variable's value.

We refer to each form variable in the Request object's Form collection by using:

Request.Form("variable1_name")
Request.Form("variable2_name")
Request.Form("variable3_name")

When name=value pairs are attached to a URL as a query string, they are assigned as variables to the Request object's QueryString collection. Consider the following code, which appends a query string to a URL.

<a href="http://www.halharris.com/pg.asp?fName=Hal&Phone=520-5950">Link Text</a>

After the user clicks the link, pg.asp opens. Any ASP code thereafter can refer to fName and Phone as variables in the Request object's QueryString collection as follows:

Request.QueryString("fName")
Request.QueryString("Phone")

Response Object

The Response object sends output and information back to the client. The Response object is unique to ASP. We can use the Response object's Write() method to send text back to the client. The Response object includes several other methods and properties that are useful in constructing a response to return to the client.

The Response object's redirect() method sends a client to a different Web page, in the same manner as the href property of client-side JavaScript's location object.

Session Object

A new Request object is instantiated each time a client requests an ASP URL, then destroyed once the URL is delivered to the client. An ASP application may be composed of multiple documents. Because the Response object is destroyed one the URL is delivered to the client, you cannot use the same Request object with different pages in an application. If you want to preserve the client information across multiple pages in an ASP application, you must use the Session object.

The Session object temporarily stores specific client information and makes that information available to all the pages in an ASP application. The ASP Session object is instantiated the first time a client accesses a URL in a given application.

We use the Contents collection of the Session object to store information about the user in your application. For example, you may store the values from the Request object's Form collection in the Session object's Contents collection in order to make the form information available across all the pages in an ASP application.

You cannot directly assign the values from one collection to another collection using statements similar to Session.Contents("name") = Request.Form("name"); because ASP will attempt to assign the entire Request.Form("name") object to the name variable of the Session object's Contents collection. In order to force ASP to assign just the value of a collection variable, you must convert the variable to a string literal using the String() object. The syntax for converting a collection variable to a string literal is String(object.collection("variable"));. The following code shows an example of an ASP application that assigns the values of a form's fields to variables of the Session object's Contents collecion.

<%
Session.Contents("name") = String(Request.Form("name"));
Session.Contents("address") = String(Request.Form("address"));
Session.Contents("city") = String(Request.Form("city"));
Session.Contents("state") = String(Request.Form("state"));
Session.Contents("zip") = String(Request.Form("zip"));
Session.Contents("email") = String(Request.Form("email"));
%>

The Session object's Timeout property determines the lifespan of a Session object. The default lifespan of the Session object is 10 minutes.

Application Object

The Application object is used for storing global application information that can be shared by all clients accessing the application. An ASP application automatically starts the first time a client requests one of the application's pages. ASP applications run until the server is shut down.

You can create a variable in the Application object's Contents collection that counts the number of visitors, and create a JavaScript function that updates the counter variable as follows:

<%
Application.Lock();
Counter=Application.Contents("NumberOfVisits");
Counter=++Counter;
Application.Contents("NumberOfVisits")=Counter;
Session.Contents("NbrHits")=Counter;
Application.Unlock();
%>

The Application object's Lock() and Unlock() methods were used to prevent one client from accessing a variable of the Application object before another client was finished with it.

Server Object

The Server object is 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.