Chapter: Internet & World Wide Web HOW TO PROGRAM - Active Server Pages (ASP)

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

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.

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).


Visit iisref/html/psdk/asp/comp275c.asp


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

          Option Explicit



          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"



          <html xmlns = "">



          <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!" )



          End If


          If browser.JavaScript = True Then


          <script language = "JavaScript">

          alert( "Client browser supports JavaScript!" );



          End If


          ' 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 />"

          End If


          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




    REDIRECT redirect.asp

    width 54

    height 36

    border 1



    United States Information



      France Information



      Germany Information



      Italy Information



      Spain 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

     Option Explicit

     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, which also refers to the local machine.


Because Web servers respond to a variety of clients, an ASP document often needs to determine who the client is and what features it supports. Line 36 (Fig. 25.28) creates a BrowserType object to obtain information about the client’s browser. Line 38 checks property VBScript’s value to determine if it is True. If so, the block (lines 48–50) are written to the client. Lines 46–52 test the JavaScript property.


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.”

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Copyright © 2018-2020; All Rights Reserved. Developed by Therithal info, Chennai.