ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional...

23
ASP(COMPONEN TS) Active Server Pages(cont..) 1

Transcript of ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional...

Page 1: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

1

ASP(COMPONENTS)

Active Server Pages(cont..)

Page 2: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

2

global.asa fileThe Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application.

All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used within Global.asa.The Global.asa file can contain only the following:

• Application events• Session events• <object> declarations• TypeLibrary declarations• the #include directive

Note: The Global.asa file must be stored in the root directory of the ASP application and each application can only have one Global.asa file.

Page 3: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

3

ASP : Components ASP Components are set of built-in- tools that automate common tasks such as displaying advertisements, keeping track of number of visitors etc.

ASP components are created by using CreateObject method of Server object,

ASP components are :

1. Ad Rotator is used to display banner advertisements on web pages.

2. Content Rotator – used to rotate HTML content on a page

3. Counter Component – used to store, increment or retrieve a value that persists for the lifetime of an application.

4. Page Counter – to keep track of number of visitors visited the web page

5. Permission Checker Component – used to check whether the user has permission to read or execute a particular file.

Page 4: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

4

Ad Rotator Component• Used to display a series of different banner advertisements each

time the Web page is viewed.• Specifies how frequently different advertisements should be

displayed.• Also records the number of times an advertisement has been

clicked by the user.

The different files used by the Ad Rotator component are: Rotator Schedule File – a text ASCII file containing information

about the images to be rotated. ASP File – the webpage that will display the advertisement along

with other information. Redirection File – implements redirection and enables the Ad

Rotator component to record how many users click on each advertisement.

Page 5: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

5

Rotator Schedule File is an ASCII file that keep track of the images to be displayed, how frequently to be displayed, the advertiser’s URL where the user will be redirected when he clicks on the advertisement.

This scheduler has 2 parts separated by a * sign :-– First part : it has 4 optional global parameters applicable to all

advertisements. If no parameter is mentioned, the default value will be used.– Second part : It specifies file and location information for each individual

advertisement and percentage of display time that each ad should receive.

Properties:– Redirect : Redirect URL – specifies the URL of the file that implements the

redirection.– Width : Width num – specifies the width of the ad in pixel, default value is

440– Height : Height num – specifies pixel height, 60 is the default value – Border : Border num – specifies the thickness of the border around the ad in

pixel. Default value is 1

Page 6: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

6

Second section contains 4 lines:• Line 1 – image of the file containing ad graphics• Line 2 – URL of the web page to which user will redirect when the ad is clicked by

the user• Line 3 – Text description to be displayed• Line 4 – Frequency with which the ad will be displayed.

REDIRECT redir.aspWIDTH 420HEIGHT 50BORDER 0*homeloan.gifhttp://www.paisavasool.comAll kinds of Home Loans70hawai.gifhttp://www.hotelsinhawai.comVisit All Hotels in Hawai30

Adrotator.txt

<html><head><title>Home Page</title></head><body><center><h1>Welcome to our Web City</h1></center><hr><%Set myAd=Server.CreateObject(“MSWC.AdRotator”)Response.write(myAd.GetAdvertisement(“adrotator.txt”))%></body></html>

An instance of Ad Rotator component is created using CreateObject() method of the server object and assigned to myAd variable using Set statement. GetAdvertisement() method of Ad Rotator is used to retrieve information about the ad to be displayed from the rotator schedule file sent as parameter.

ASP file is needed to display the ads stored in the above rotator schedule file using the Ad Rotator component.

Adhomepage.asp

Page 7: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

7

ASP AdRotator Component

The ASP AdRotator component creates an AdRotator object that displays a different image each time a user enters or refreshes a page. A text file includes information about the images.

Note: The AdRotator does not work with Internet Information Server 7 (IIS7).

Syntax<%set adrotator=server.createobject("MSWC.AdRotator")adrotator.GetAdvertisement("textfile.txt")%>

Page 8: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

8

ASP AdRotator Example The lines below the asterisk in the text file specifies the name of the images (ads) to be displayed, the hyperlink addresses, the alternate text (for the images), and the display rates (in percent).

The first line in the text file above specifies what to happen when a visitor clicks on one of the images. The redirection page (banners.asp) will receive a query string with the URL to redirect to.Tip: To specify the height, width, and border of the image, you can insert the following lines under REDIRECT:REDIRECT banners.aspWIDTH 468 HEIGHT 60 BORDER 0 *w3s.gif...

Assume that we have the following text file, named "ads.txt":

REDIRECT banners.asp*w3s.gifhttp://www.w3schools.comFree Tutorials from W3Schools50xmlspy.gifhttp://www.altova.comXML Editor from Altova50

Page 9: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

9

Ad Rotator Properties:• Border

AdRotator.Border = size Specifies the thickness of the border around the advertisement in pixels.

• Clickable AdRotator.Clickable = value, value is true or falseSpecifies whether the ad should function as a hyperlink.

• TargetFrameAdRotator.TargetFrame = frameSpecifies the name of frame into which the ad link should be loaded.

Page 10: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

10

Content Rotator Component

The ASP Content Rotator component creates a Content Rotator object that displays a different content string each time a visitor enters or refreshes a page.

A text file, called the Content Schedule File, includes the information about the content strings.

The content strings can contain HTML tags so you can display any type of content that HTML can represent: text, images, colors, or hyperlinks.

Syntax<%Set cr=Server.CreateObject("MSWC.ContentRotator")%>

Page 11: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

11

Methods of Content Rotator

• ChooseContent ContRot.ChooseContent(schedule-path)

Retrieves a HTML content string from the Content Schedule File randomly and displays on the current page.

• GetAllContentContRot.GetAllContent(schedule-path)

Retrieves all of the HTML content string from the Content Schedule File and write them directly to the web page as a list with <HR> tag after each entry.

Page 12: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

12

ASP Content Rotator Example

The following example displays a different content each time a visitor views the Web page.First, create a text file named "textads.txt" and place it in a subfolder called "text"."textads.txt":

%% #3 <h2>This is a great day!!</h2>

%% #3<img src="smiley.gif">

%% #4<a href="http://www.w3schools.com">Visit W3Schools.com</a>

Notice the #number at the beginning of each content string. This number is an optional parameter that indicates the relative weight of the HTML content string. In the text file above, the Content Rotator will display the first and second content string three-tenth of the time, and the third string four-tenths of the time.

Page 13: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

13

Then, create an ASP file, and insert the following code:

Example<html><body><%set cr=server.createobject("MSWC.ContentRotator")response.write(cr.ChooseContent("text/textads.txt"))%></body></html>

Page 14: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

14

ASP Source<!DOCTYPE html><html><body><p><a href="demo_textads.asp"><img src="/images/btn_view_text.gif"></a></p><%set cr=server.createobject("MSWC.ContentRotator")response.write(cr.ChooseContent("text/textads.txt"))%>

<p>NOTE: Because the content strings are changed randomly in the text file, and this page has only four content strings to choose from, sometimes the page will display the same content strings twice in a row.</p>

</body></html>

Output

This is a great day!!

NOTE: Because the content strings are changed randomly in the text file, and this page has only four content strings to choose from, sometimes the page will display the same content strings twice in a row.

Page 15: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

15

Counters Component

A counter is a persistent value that contains an integer. You can manipulate a counter with the Get, Increment, Set, and Remove methods of the Counters object. Once you create the counter, it persists until it is removed.

Counters do not automatically increment on an event like a page hit. You must manually set or increment counters using the Set and Increment methods.

Counters are not limited in scope. Once you create a counter, any page on your site can retrieve or manipulate its value.

For example, if you increment and display a counter named hits in a page called Page1.asp, and you increment hits in another page called Page2.asp, both pages will increment the same counter. If you hit Page1.asp and increment hits to 34, hitting Page2.asp will increment hits to 35. The next time you hit Page1.asp, hits will increment to 36.All counters are stored in a single text file, Counter.txt. Only create one Counters object in your site. This single Counters object can create any number of individual counters.

Page 16: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

16

Syntax: ASP Counters Component

The Counters control is registered with the ProgId of "MSWC.Counters"

Create the Counters object one time on your site by adding the following to the global.asa file:

<OBJECT RUNAT=Server SCOPE=Application ID=Counter PROGID="MSWC.Counters"> </OBJECT>

Note: ASP Counter Properties - None

Page 17: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

17

Methods Syntax Description

Set Counters.Set (CounterName, int) Where countername is the name of the counter and int specifies the new value for the countername.

Sets the value of the counter to the specified value. If the counter does not exist it creates a new counter and sets its values.

Get Counters.Get(CounterName)Where CounterName specifies the name of the counter

Takes the name of the counter as parameter and returns the current value of the counter.

Increment Counters.Increment(CounterName) Increment the current counter value by 1 and returns the new value, if does not exist, then sets the value as 1.

Remove Counters.Remove(CounterName) Removes the counter from the Counter Object.

ASP Counters Component Methods : Get, Set, Increment, Remove

Page 18: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

18

Examples: ASP Counters Component Get MethodDisplay the value of a counter with: <%= Counters.Get(CounterName) %> Assign the value of the counter to a variable with: <% countervar = Counters.Get(CounterName) %> The following script displays the vote tally from a poll about favorite colors. <% If colornumber = "1" Then Counters.Increment("greencounter") ElseIf colornumber = "2" Then Counters.Increment("bluecounter") ElseIf colornumber = "0" Then Counters.Increment("redcounter") End If %>

<P>Current vote tally: <P>red: <% = Counters.Get("redcounter") %> <P>green: <% = Counters.Get("greencounter") %> <P>blue: <% = Counters.Get("bluecounter") %>

Page 19: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

19

Examples: ASP Counters Component Increment Method

Increment the value of a counter with: <% Counters.Increment(CounterName) %>

Increment and display the value of a counter with: <%= Counters.Increment(CounterName) %>

To retrieve the value of a counter, use Counters.Get.

To set a counter to a specific value, use Counters.Set.

The following code implements a one-line page-hit counter.

<P>There have been <%= Counters.Increment("hits") %> visits to this Web page. </P>

In this example, Counters.Increment increases the counter by one each time the client requests the page from the server.

Page 20: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

20

Example: ASP Counters Component Remove MethodThe following code removes the counter hitscounter from the Counter.txt file. <% Counters.Remove(hitscounter) %>

Example: ASP Counters Component Set MethodThe following code resets the hit counter pageHits to 0: <% Counters.Set(pageHits, 0) %>

Page 21: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

21

Examples: ASP Counters Component<html><head><title>Opinion Poll</title></head><body><%vote = request.QueryString("Choice")if vote="Yes" then mycount.Increment("yesCounter")elseif vote="No" then mycount.Increment("NoCounter")elseif vote = "Cannot say" then mycount.Increment("yesNoCounter")end if%><H2> Do you like Pizza </H2><Form Name = "Voting" method = "Get" Action = "Counters.asp"><Input Type = "Radio" name="choice" checked value="yes">Yes<Input Type = "Radio" name="choice" checked value="no">No<Input Type = "Radio" name="choice" checked value="cannot say">Cannot say<br> Input Type ="SUBMIT" value= "submit vote"</Form><H3>Current Vote Tally</H3><table border =1><tr><td>Yes</td> <td><%=mycount.Get("yesCounter")%></td></tr><tr><td>No</td> <td><%=mycount.Get("NoCounter")%></td></tr><tr><td>Can not say</td><td><%=mycount.Get("yesNoCounter")%></td></tr></table><br> this page has been visited <%=mycount.Increment("pgcount")%> times</body></html>

Page 22: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

22

Page Counter ComponentCreates a counter which counts the number of times a page has been viewed, similar to Counters component, while Counters component is set manually, the page counter component is an automatic process.

Syntax : Set myVar = Server.CreateObject(“MSWC.PageCounter”)

Methods:• Hits - myVar.Hits( [pathInfo]) – if the path Info is not specified, the hit count for the

current page is displayed, i.e., number of times a specified Web page has been opened.• PageHit – myVar.PageHit() – Increments the hit count for the current Web page by 1.• Reset - myVar.Reset ( [pathInfo]) – Resets the hit count for a specified Web page to 0.

Example :<HTML><BODY><%Set myPageCounter = Server.CreateObject(“MSWC.PageCounter”)myPageCounter.PageHit()Num= myPageCounter.Hits()%><CENTER><H1>This Page is visited <FONT FACE=“Regular” SIZE = 6 COLOR = “blue”><%=Num%> </FONT></H1></CENTER> </BODY></HTML>

Page 23: ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,

23

Permission Checker Component

Can be used to check whether the user has permission to access a particular file or not, this is possible by the password authentication protocols provided by Microsoft in its propriety Web Server – Internet Information Services (IIS).

Syntax:Set myVar = Server.CreateObject(“MSWC.PermissionChecker”)

Method: HasAccess – myVar.HasAccess (Path) – checks whether a user has permission to access a specified file and returns true or false.