Server-Side ActiveX Components
Server-side script functionality is extended with server-side ActiveX components— ActiveX controls that typically reside on the Web server and do not have a graphical user interface. These components make features accessible to the ASP author. Figure 25.27 summarizes some of the ActiveX components included with Internet Information Servic-es (IIS).
for more information about Web server technologies.
Many Web sites sell advertising space—especially Web sites with large numbers of hits. The code in Fig. 25.28 demonstrates the AdRotator ActiveX component for rotating advertisements on a Web page. Each time a client requests this Active Server Page, the AdRotator component randomly displays one of several advertisements—in this example, one of five flag images. When the user clicks a country’s flag image, the country’s corre-sponding Central Intelligence Agency (CIA) Fact book Web page is displayed. [Note: The example presented here is IIS specific. PWS users should use the version in the Chapter 25 examples directory (on the CD-ROM that accompanies this book). Separate files are included on the CD for users running Personal Web Server.]
Line 29 creates an instance of an AdRotator component and assigns it to reference rotator. Server-side ActiveX components are instantiated by passing the name of the component as a string to the Server object’s method CreateObject.
Lines 32–33 call the Response object’s Write method to send the advertisement as HTML to the client. Method GetAdvertisement is called using reference rotator to get the advertisements from the file config.txt (Fig. 25.29).
<% @LANGUAGE = VBScript %>
' Fig. 25.28 : component.asp
' Demonstrating Server-side ActiveX Components
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns = "http://www.w3.org/1999/xhtml">
<title>ActiveX Component Example</title>
<strong style = "font-family: arial, sans-serif">
Server-side ActiveX Components
Dim rotator, browser, information, counter
' create an AdRotator object
Set rotator = Server.CreateObject( "MSWC.AdRotator" )
' use config.txt to send an advertisement to the client
Call Response.Write( _
rotator.GetAdvertisement( "config.txt" ) )
' create a BrowserType object
Set browser = Server.CreateObject( "MSWC.BrowserType" )
If browser.VBScript = True Then
<script language = "VBScript">
Call Msgbox( "Client browser supports VBScript!" )
' get client's browser information
information = "<p>Your browser information is:<br />" & _
Request.ServerVariables( "HTTP_USER_AGENT" ) & _
"<br />Browser: " & browser.Browser & " Version: " & _
browser.Version & " Minor version: " & _
browser.MinorVer & "<br />Cookies are "
If browser.Cookies Then
information = information & "enabled</p><br />"
information = information & "disabled</p><br />"
Call Response.Write( information )
' create Page Counter Object
Set counter = Server.CreateObject( "MSWC.PageCounter" )
Call counter.PageHit() ' page has been "hit"
<p style = "color: blue; font-size: 12pt">
This page has been visited <% =counter.Hits() %>
Fig. 25.28 Demonstrating server-side ActiveX components
United States Information
Fig. 25.29 File config.txt that describes the advertisements.
The file’s header (lines 1–4) includes the URL of the REDIRECT file, redi-rect.asp (Fig. 25.30), the image height, image width and image border width. The asterisk (line 5) separates the header from the advertisements. Lines 6–9 describe the first advertisement by providing the image’s URL (the image’s location), the destination URL for redirection upon clicking the ad, a value for the alt tag (browsers that cannot dis-play graphics display the specified text) and a number (between 0 and 1000) representing the ratio of time this particular image appears. The ratios must be numbers between 0 and 10,000. For example, if four ads have the ratios 6, 9, 12 and 3, then the time ratios are cal-culated as 20% (6/30), 30% (9/30), 40% (12/30) and 10% (3/30), respectively. Lines 10– 25 list the other four advertisements. [Note: If you are executing this example, copy config.txt to the Deitel virtual directory you created in Section 25.3.]
File redirect.asp (Fig. 25.30) redirects the user to the country page when the ad is clicked. Each time the ad is clicked, the document redirect.asp is requested and a query string is sent with the request. The query string contains an attribute url that is equal to the destination URL found in config.txt for this ad. Because we are redirecting the user to a different page on the client rather than the server, we call Response method Redirect to redirect the user to the country page. For example, click the U.S. flag. The resulting behavior is equivalent to typing
in the browser’s Address field.
We arbitrarily chose the names config.txt and redirect.asp. You may choose any name you prefer. The redirect file loads (into the browser) the page referenced by the ad’s URL. These files can be placed anywhere in the publishing directory (i.e., they do not have to be under the same directory as rotate.asp). For example, if you put config.txt under directory X in the publishing directory, then Fig 25.28 lines 32–33 would read
<% @LANGUAGE = VBScript %>
' Fig. 25.30 : redirect.asp
' Redirection Page for AdRotator Component
Call Response.Redirect( Request( "url" ) )
Fig. 25.30 Code listing for redirect.asp.
Call Response.Write( _
flagChanger.GetAdvertisement( "/X/config.txt" ) )
Note that GetAdvertisement is passed a URL, not a physical disk path. Hence the use of the forward slash. Also note that /X/config.txt is short for http://local-host/X/config.txt (the server is localhost and the publishing directory is C:\Inetpub\Wwwroot). You can replace localhost with the IP address 127.0.0.1, which also refers to the local machine.
Line 56 passes the server variable key HTTP_USER_AGENT to ServerVariables to obtain a string containing the client’s information. The BrowserType object’s Browser, Version and MinorVer properties (lines 57–59) may also be used to obtain similar client information. Line 61 tests the Cookies property’s value to determine if the browser supports cookies.
Many popular Web sites display a “hit” counter that shows how many visitors the site has had. IIS provides the PageCounter ActiveX component for storing the number of “hits.” Method PageHit (line 71) increments the number of “hits” by one, and method Hits (line 76) returns the number of “hits.”
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.