Active Server Pages Tutorial

The Application Object

Unlike the Session object, the Application object stores application information shared among all users of a site. Application information lasts as long as the web server is running.

Application Object: Methods
Application.Lock Locks the variables in an application while their values are being changed.
Application.Unlock Unlocks the variables in an application.
Application Object: Collections
Application.Contents Contains items added to the Application object using script.
Application.StaticObjects Contains items added to the Application object using the HTML
Application Object: Events
Application.OnStart Specifies an event to occur when the server starts an application.
Application.OnEnd Specifies an event to occur when an application ends.

Using Application Variables

Application variables are created and accessed just like Session variables. The only difference is the statement-Application(“variable”)-and the fact that application variables can be accessed by all sessions (users).

<% Application(“Visits”)=Application(“Visits”)+1 %>

Counter: <%=Application(“Visits”)%> visits

Each time you refresh a page with the above code, the counter increases by 1. It will continue to increase as other users access the page.

The Server Object

The Server object creates objects on and provides access to the web server. For example, you can use the Server object to create ActiveX objects, create recordsets (when connected to a database), or access information such as the physical path to items in a directory on the server.

Server Object: Methods
Server.CreateObject Creates an instance of an object, such as an ActiveX object or a recordset.
Server.Execute Accesses one ASP page from another (includes the contents of the page in the calling page).
Server.GetLastError() Creates an instance of the ASPError object, which describes the error that occurred.
Server.HTMLEncode Forces a client to process HTML code as plain text.
Server.MapPath Returns the physical path of a file on the server.
Server.Transfer Transfers information created on one page to another page.
Server.URLEncode Encodes a string as a URL so it can be passed from one page to another.
Server Object: Properties
Server.ScriptTimeout Specifies the number of seconds a server will process a script before timing out.

Setting Script Timeout

You can set the timeout property for a lengthy script to prevent the server from timing out before processing is complete:

<% Server.ScriptTimeout=300 %>

Note: The timeout setting for a script cannot exceed the timeout setting for the server.

Displaying HTML Code as Text

You may occasionally need to display symbols that are ordinarily interpreted as HTML code-for example, the angle brackets (<>). To do so, you can use the Server.HTMLEncode statement:

<% Response.Write(“Use the paragraph tag ” & Server.HTMLEncode(“ “) & ” to format paragraph text.”) %>

Encoding a URL

Because a browser can misinterpret spaces and special characters in a URL (as when, for example, a query string is passed), you should use the Server.URLEncode statement to encode a URL being passed from one page to another:

<% Dim FavoriteURL FavoriteURL=Request.Form(“txtFavURL”) %> Click the link below to visit your favorite URL: ><%=FavoriteURL%>

In the code above, a user’s entry for a favorite URL is requested from a from on another page and displayed as a hyperlink on the current page. While the text for the hyperlink appears as the user entered it, the actual hyperlink is encoded to be interpreted correctly by the browser.

Mapping Server File Paths

You use the Server.MapPath statement to return the physical path for a file on the server. To return the path of the server’s root directory, include a slash (/) or a backslash (\):

<%Response.Write(Server.MapPath(“/”))%> <%Response.Write(Server.MapPath(“SendData.asp”))%>

Creating Server Objects

Much of the power of dynamic content can be found in its ability to interact with a database. From any Internet connection, users can browse, sort, update and delete records using a simple but flexible interface that is, on the developer’s side, both secure and easy to code. You use the Server.CreateObject statement to create the objects necessary for interacting with a database. The following code creates a Connection object to connect to a database:

<% Dim cnn Set cnn=Server.CreateObject(“ADODB.Connection”) cnn.ConnectionTimeout=60 cnn.Open “DSN=TestDB” cnn.Close Set cnn=Nothing %>

You can also create a Recordset object:

<% Dim cnn Dim rs Set cnn=Server.CreateObject(“ADODB.Connection”) cnn.ConnectionTimeout=60 cnn.Open “DSN=TestDB” Set rs=Server.CreateObject(“ADODB.Recordset”) rs.Open “Select * From tblTest”,cnn cnn.Close Set cnn=Nothing %>

Leave a Reply

Your email address will not be published. Required fields are marked *