DS 420ssg b | ![]() ![]() |
Dr.Elliott |
The ASP framework provides five built-in objects to ease the creation of sophisticated dynamic Web applications. The objects are titled Application, Request, Response, Server, and Session. The application object is used to share information among all users. The following is a sample implementation of
The application object:
<html>
<%For Count = 1 To 7% >
<FONT SIZE =<%= Count %>>
Hello World! <BR>
</font>
<%Next%>
<%
Application.Lock
Application("NumberOfHits")=Application("NumberOfHits")+1
Application.Unlock
%>
This site has been visited
<%=Application("NumberOfHits") %>
times!
</html>
In this example the variable "NumberOfHits" is used to store the number of times
A page has been accessed. The Lock and Unlock methods ensure only the current client can modify "NumberOfHits".
The built in Request objcet retrieves the values that the client browser passed to the server during a request. As opposed to the hassle of parsing data with CGI programs, this object allows easy access to data through what are called "Collections". The five "collections" are Form, QueryString, Cookies, Client Certificate, and ServerVariables. Obviously the most common use of the Request object is to retrieve data entered by the user into an HTML form.
<FORM METHOD=POST Action=name.asp>
Name: <INPUT NAME=MyName SIZE=30>
</FORM>
This information is retrieved in an .ASP file on the server using Request object.
<%=Request.Form("MyName") %>
The response objcet is used to send the output back to the client. This can be used to store cookies on the user's machine using the Cookies collection. Because HTTP is a stateless protocol, cookies are used as the packets of data which the Web server passes to the browser. Whenever a client visits a site the sever can ask for the cookie to restore information about the client.
<%Response.Cookies("FavoriteNews") ="Sports" %>
The server then generates a special HTTP header that is sent to the client's Web browser:
Set-Cookie:FavoriteNews=Sports
The Server object provides access to methods and properties on the server.
The most common and useful being the CreateObject, which enables a program to instatiate an ActiveX component.
<% Set dbConnection = Server.CreateObject("ADODB.Connection")
This code creates an instance of the ActiveX Data Objects(ADO) component's connection object.
Lastly, the Session object is used to store information needed for a user session. Variables stored in the Session object are saved for the entire user session. The server creates a Session object automically when a new user requests a Web page from the application.
<% Application("Name") = "Anna"
Application("Age")=21
%>
This is a common use in which the session object can store information about the user.
In addition to the previously mentioned objects ASP has access to five, easily modifiable add-on components. The Ad rotator, Browser Capabilites, Database Access, Content Linking, and File Access are the components that are provided as add-on DLLs.
The Ad Rotator component is fairly intuitive, it automates the rotation of advertisement, or other images each time the user opens or reloads a web page.
An activity log in the IIS allows one to track how many users "click" on a given image, or advertisement.
<% Set ad = Server.CreateObject("MSWC.AdRotator") %>
<%=ad.GetAdvertisement("myads.txt") %>
The above mentioned GetAdvertisement method will retrieve an ad from within the specified ad rotation file.
The Browser Capabilities component allows the server to identify the client's Web browser so that Browsers that may not support technologies such as ActiveX, java applets, etc. are not presented with inappropriate content.
ADO, or ActiveX Data Object is the component that allows database access. This component is further described in the database access section.
The Content Linking component manages a list of URLs so that one can treat the pages in a Web site like the pages of a book. Utilizing this feature allows automatic updating of navigational links to changes in Web pages. This uses a special content Linking List file that contain the list of linked Web pages which is stored on the Web server. The following is a simple implementation of the Content Linking Component :
<% Set nl=Server.CreateObject("MSWC.NextLInk")%>
<% If nl.GetListIndex("nextlink.txt.")> -1 Then %>
<A HREF=" <%=nl.GetPreviousURL("nextlink.txt")%> ">
Previous Page</a><br>
<%End If%>
<A Href="<% =nl.GetNextURL("nextlink.txt")%>">
Next Page</a>
The File Access Component with Visual Basic's FileSystemObject and TextStream
Object enables ASP to retrieve and modify information stored in files on the server.