Вы находитесь на странице: 1из 77

1

Chapter 25 - Active Server Pages (ASP)


Outline
25.1 25.2 25.3 25.4 25.5 25.6 25.7 25.8 25.9 25.10 Introduction How Active Server Pages Work Setup Active Server Page Objects Simple ASP Examples File System Objects Session Tracking and Cookies Accessing a Database from an Active Server Page Server-Side ActiveX Components Internet and World Wide Web Resources

2001 Prentice Hall, Inc. All rights reserved.

25.1 Introduction Server-side technologies


Dynamically creates Web pages
Uses client information, server information and information from the Internet

Active Server Pages (ASP)


Microsoft Server-side technology Dynamically builds documents in response to client requests Delivers dynamic Web content XHTML, DHTML, ActiveX controls, client-side scripts and Java applets

2001 Prentice Hall, Inc. All rights reserved.

25.2 How Active Server Pages Work Active Server Pages


Processed by scripting engine
Server-side ActiveX control

.asp file extension Can contain XHTML tags Scripting written with VBScript
JavaScript also used Others (Independent Software Vendors)

Communication with Server


Client HTTP request to server Active server page processes request and returns results

2001 Prentice Hall, Inc. All rights reserved.

25.2 How Active Server Pages Work Active Server Pages,cont.


Communication with Server, cont.
ASP document is loaded into memory asp.dll scripting engine on server
Parses (top to bottom)

2001 Prentice Hall, Inc. All rights reserved.

25.3 Setup Web Server


Need to run Web server to use Active Server Pages
IIS (Internet Information Services) or PWS 4.0 (Personal Web Server 4.0)

Create a virtual directory


Copy files to c:\InetPub\Wwwroot or c:\Webshare\Wwwroot

2001 Prentice Hall, Inc. All rights reserved.

25.4 Active Server Page Objects


Built-in objects
Communicate with a Web browser Gather data sent by HTTP request Distinguish between users Request
Get or post information Data provided by the user in an XHTML form Access to information stored on client machine Cookies File upload (binary)

Response
Sends inforamtion to client XHTML, text

Server
Access to server methods or properties
2001 Prentice Hall, Inc. All rights reserved.

25.4 Active Server Page Objects


Object Na me Request Response Server Description Used to access information passed by an HTTP request. Used to control the information sent to the client. Used to access methods and properties on the server.

Fig. 25.1 Co m m o nly used ASP o b jec ts.

2001 Prentice Hall, Inc. All rights reserved.

8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <% @LANGUAGE = VBScript %>

Outline
Scripting delimeter wraps around code executed on the server.
Clock.asp
sends Web servers date and time to the client as XHTML markup <% %> scripting delimeter @LANGUAGE processing directive

<%
' Fig. 25.2 : clock.asp ' A simple ASP example Option Explicit %>

Processing directive <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" specifying scripting Requires programmer "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> language define all variables explicitly
<html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>A Simple ASP Example</title> <style type = "text/css"> td { background-color: black; color: yellow } strong { font-family: arial, sans-serif; font-size: 14pt; color: blue } Returns server date and time p { font-size: 14pt } (Now) as a string formatted in </style>

Option Explicit
FormatDateTime Now vbLongDate format

vbLongDate format <%= is short for Response.write

</head>

<body>

Response.write
Time

<p><strong>A Simple ASP Example</strong></p> <table border = "6"> <tr> <td> <% =FormatDateTime( Now, vbLongDate ) %> </td>

2001 Prentice Hall, Inc.


All rights reserved.

9
36 <td> 37 <% =Time() %> 38 </td> 39 </tr> 40 </table> Statement is short for: 41 </body> 42 <% Call Response.write( Time()) 43 </html>

Outline
Clock.asp %> Time

Program Output

2001 Prentice Hall, Inc.


All rights reserved.

10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>A Simple ASP Example</title> <style type = "text/css"> td { background-color: black; color: yellow } strong { font-family: arial, sans-serif; font-size: 14pt; color: blue } p { font-size: 14pt } </style> </head>

Outline
Fig. 25.3
XHTML generated by clock.asp.

<body>
<p><strong>A Simple ASP Example</strong></p> <table border = "6"> <tr> <td> Thursday, May 24, 2001 </td> <td> 2:22:58 PM </td> </tr> </table> </body>

</html>

2001 Prentice Hall, Inc.


All rights reserved.

11

25.5 Simple ASP Examples ASP processes input


Form information sent by client E-commerce Web site
Use to verify customer information

Server responds to process request Form


Using post method action attribute indicates the .asp file to which the form information is posted

request object retrieves form data

2001 Prentice Hall, Inc. All rights reserved.

12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- Fig. 25.4 : name.html --> <!-- XHTML document that request an ASP document --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Name Request</title> </head> <body> <p style = "font-family: arial, Enter your name: </p>

Outline
Name.html
passes information into an ASP document using the post method.

action set to ASP file where information sans-serif"> is posted.

<!-- request name.asp when posted --> <form action = "name.asp" method = "post"> <input type = "text" name = "namebox" size = "20" /> <input type = "submit" name = "submitButton" value = "Enter" /> </form>
</body> </html>

2001 Prentice Hall, Inc.


All rights reserved.

13

Outline
Name.html Program Output

2001 Prentice Hall, Inc.


All rights reserved.

14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <% @LANGUAGE = VBScript %>

Outline
Name.asp
Uses the Request method to retrieve the data posted in Name.html. Places this data within XHTML tags for output.

<%
' Fig. 25.5 : name.asp ' Another simple ASP example Option Explicit %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Name Information</title> <style type = "text/css"> Request object retrieves p { font-family: arial, sans-serif; font-size: 14pt; color: navy } form data from textfield .special { font-size: 20pt; color: green } namebox </style> </head> <body> <!-- retrieve and display namebox's value --> <p>Hi <% =Request( "namebox" ) %>, </p><br /> <p class = "special">Welcome to ASP!</p> </body> </html>

2001 Prentice Hall, Inc.


All rights reserved.

15

Outline
Name.asp

Program Output

2001 Prentice Hall, Inc.


All rights reserved.

16

25.6 File System Objects File System Objects (FSOs)


Allow programmer to manipulate files, directories and drives Read and write text Microsoft Sctipting Runtime Library (Fig 25.6)
FileSystemObject, File, Folder, Drive and TextStream

Use to create directories, move files, determine whether a Drive exists


FileSystemObject methods (Fig. 25.7)

File object
Allows programmer to gather info about files, manipulate files, open files File properties and methods (Fig. 25.8)
2001 Prentice Hall, Inc. All rights reserved.

17

25.6 File System Objects


Object type FileSystemObject File Folder Drive Description Allows the programmer to interact with Files, Folders and Drives. Allows the programmer to manipulate Files of any type. Allows the programmer to manipulate Folders (i.e., directories). Allows the programmer to gather information about Drives (hard disks, RAM diskscomputer memory used as a substitute for hard disks to allow high-speed file operations, CD-ROMs, etc.). Drives can be local or remote. Allows the programmer to read and write text files.

TextStream Fig. 25.6

File System Ob je c ts (FSO s).

2001 Prentice Hall, Inc. All rights reserved.

18

25.6 File System Objects


Methods CopyFile CopyFolder CreateFolder CreateTextFile DeleteFile DeleteFolder DriveExists FileExists FolderExists GetAbsolutePathName Fig. 25.7 Description Copies an existing File. Copies an existing Folder. Creates and returns a Folder. Creates and returns a text File. Deletes a File. Deletes a Folder. Tests whether or not a Drive exists. Returns a boolean. Tests whether or not a File exists. Returns a boolean. Tests whether or not a Folder exists. Returns a boolean. Returns the absolute path as a string.

FileSystemObject m etho d s. (Pa rt 1 o f 2)

2001 Prentice Hall, Inc. All rights reserved.

19

25.6 File System Objects


Methods GetDrive GetDriveName GetFile GetFileName GetFolder GetParentFolderName GetTempName MoveFile MoveFolder OpenTextFile Fig. 25.7 Description Returns the specified Drive. Returns the Drive drive name. Returns the specified File. Returns the File file name. Returns the specified Folder. Returns a string representing the parent folder name. Creates and returns a string representing a file name. Moves a File. Moves a Folder. Opens an existing text File. Returns a TextStream.

FileSystemObject m etho d s. (Pa rt 2 o f 2)

2001 Prentice Hall, Inc. All rights reserved.

20

25.6 File System Objects


Property/ method Properties DateCreated DateLastAccessed DateLastModified Drive Name ParentFolder Path ShortName Size Methods Copy Delete Move OpenAsTextStream Fig. 25.8 Date. The date the File was created. Date. The date the File was last accessed. Date. The date the File was last modified. Drive. The Drive where the file is located. String. The File name. String. The Files parent folder name. String. The Files path. String. The Files name expressed as a short name. Variant. The size of the File in bytes. Copy the File. Same as CopyFile of FileSystemObject. Delete the File. Same as DeleteFile of FileSystemObject. Move the File. Same as MoveFile of FileSystemObject. Opens an existing File as a text File. Returns TextStream. So m e c o m m o n File p ro p erties a nd m etho d s. Description

2001 Prentice Hall, Inc. All rights reserved.

21

25.6 File System Objects File System Objects (FSOs)


Path property
Contains File path in long name format

ShortName property
Contains File path in short name format

Folder object
Allows programmer to manipulate and gather info about directories Folder properties (Fig. 25.9)

2001 Prentice Hall, Inc. All rights reserved.

22

25.6 File System Objects


Property/ method Properties Attributes DateCreated DateLastAccessed DateLastModified Drive IsRootFolder Name ParentFolder Path ShortName ShortPath Size Type Fig. 25.9 Integer. Value indicating Folders attributes (read only, hidden, etc.). Date. The date the folder was created. Date. The date the folder was last accessed. Date. The date the folder was last modified. Drive. The Drive where the folder is located. Boolean. Indicates whether or not a Folder is a root folder. String. The Folders name. Folder. The Folders parent folder. String. The Folders path. String. The Folders name expressed as a short name. String. The Folders path expressed as a short path. Variant. The total size in bytes of all subfolders and files. String. The Folder type. So m e Folder p ro p erties a nd m etho d s. (Pa rt 1 o f 2) Description

2001 Prentice Hall, Inc. All rights reserved.

23

25.6 File System Objects


Property/ method Methods Delete Move Copy Fig. 25.9 Delete the Folder. Same as DeleteFolder of FileSystemObject. Move the Folder. Same as MoveFolder of FileSystemObject. Copy the Folder. Same as CopyFolder of FileSystemObject. So m e Folder p ro p erties a nd m e tho d s. (Pa rt 2 o f 2) Description

2001 Prentice Hall, Inc. All rights reserved.

24

25.6 File System Objects File System Objects (FSOs)


IsRootFolder property
Indicates whether folder is the root folder for the Drive If not: Method ParentFolder
Retrieves parent folder Method Size Returns the total bytes the folder contains (includes subfolders)

2001 Prentice Hall, Inc. All rights reserved.

25

25.6 File System Objects File System Objects (FSOs)


Drive object (Fig. 25.10)
Gather information about drives Property DriveLetter Contains drive letter Property SerialNumber Contains drive serial number Property FreeSpace Contains number of available bytes

2001 Prentice Hall, Inc. All rights reserved.

26

25.6 File System Objects


Property AvailableSpace DriveLetter DriveType Description Variant. The amount of available Drive space in bytes. String. The letter assigned to the Drive (e.g., C). Integer. The Drive type. Constants Unknown, Removable, Fixed, Remote, CDRom and RamDisk represent Drive types and have the values 05, respectively. String. The file system Drive description (FAT, FAT32, NTFS, etc.). Variant. Same as AvailableSpace. Boolean. Indicates whether or not a Drive is ready for use. String. The Drives path. Folder object. The Drives root Folder. Long. The Drive serial number. Variant. The total Drive size in bytes. String. The Drive volume name. Drive p ro p ertie s.

FileSystem FreeSpace IsReady Path RootFolder SerialNumber TotalSize VolumeName Fig. 25.10

2001 Prentice Hall, Inc. All rights reserved.

27

25.6 File System Objects File System Objects (FSOs)


TextStream object (Fig. 25.11)
Manipulate text files

2001 Prentice Hall, Inc. All rights reserved.

28

25.6 File System Objects


Property/ Method Properties AtEndOfLine AtEndOfStream Column Line Methods Read ReadAll Fig. 25.11 Boolean. Indicates whether the end of a line has been encountered. Boolean. Indicates whether the end of file has been encountered. Integer. Returns the characters position in a line. Integer. Returns the current line number. String. Returns a specified number of characters from the file referenced by the TextStream object. String. Returns the entire contents of the file referenced by the TextStream object. TextStream m etho d s a nd p ro p e rties. (Pa rt 1 o f 2) Description

2001 Prentice Hall, Inc. All rights reserved.

29

25.6 File System Objects


Property/ Method Methods, cont. ReadLine Write WriteBlankLines WriteLine Skip SkipLine Close Fig. 25.11 String. Returns one line from the file referenced by the TextStream object. String. Writes text to the file referenced by the TextStream object. String. Writes newline characters to the file referenced by the TextStream object. String. Writes one line to the file referenced by the TextStream object. Variant. Skips a specified number of characters while reading from the file referenced by the TextStream object. Variant. Skips a line of characters while reading from the file referenced by the TextStream object. Close the file referenced by the TextStream object. TextStream m etho d s a nd p ro p e rties. (Pa rt 2 o f 2) Description

2001 Prentice Hall, Inc. All rights reserved.

30

25.6 File System Objects


Key Na me APPL_PHYSICAL_PATH HTTPS Description Returns the physical path. Boolean. Determines if the request came in through SSL (Secure Sockets Layer). Clients DNS name or IP address. Request method (i.e., get and post). Servers hostname (DNS or IP address). Returns information about the client making the request. Returns cookies residing on the client.

REMOTE_ADDR REQUEST_METHOD SERVER_NAME HTTP_USER_AGENT HTTP_COOKIE Fig. 25.13 So m e serve r va ria b le ke ys.

2001 Prentice Hall, Inc. All rights reserved.

31
1 <% @LANGUAGE = VBScript %> 2 3 <% ' Fig. 25.12 : guestbook.asp 4 ' Demonstrating File System Objects 5 Option Explicit 6 %> 7 8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 9 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 10 11 <html xmlns = "http://www.w3.org/1999/xhtml"> 12 13 <head> 14 <title>GuestBook Example</title> 15 16 <style type = "text/css"> 17 hr { size: 1; color: blue } 18 table { text-align: center } 19 td { font-size: 12pt } 20 p { font-size: 14pt; color: blue } 21 .font { font-family: arial, sans-serif } 22 </style> Set is required to establish a Pass Request method 23 </head> variable in VBScript ServerVariables server 24 <body> 25 <% key APPL_PHYSICAL_PATH 26 Dim fileObject, textFile, guestBook, mailtoUrl Concatenated with file 27 name guestbook.txt 28 ' get physical path for this ASP page and Creating a FSO instance 29 ' concatenate guestbook.txt to it 30 guestbook = Request.ServerVariables( "APPL_PHYSICAL_PATH" ) _ assigned to reference 31 & "\guestbook.txt" fileObject 32 33 ' instantiate a FileSystemObject 34 Set fileObject = Server.CreateObject( _ 35 "Scripting.FileSystemObject" )

Outline
Guestbook.asp
Users enter name, email address and comments. FSOs write this data to a server file.

Request ServerVariables
APPL_PHYSICAL_PATH

fileObject

2001 Prentice Hall, Inc.


All rights reserved.

32
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 ' Check if this request is after the user has posted the form If Request( "hiddenInput" ) = "true" Then ' Print a thank you Call Response.Write( "Thanks for your entry, " & _ Request( "username" ) & "!" ) %> <% <hr />

Outline
Guestbook.asp
Users enter name, email address and comments. FSOs write this data to a server file.

%>

Request object retrieves hiddenInput value and ' build the mailtoUrl tests it against string true mailtoUrl = Date() & " <a href = " followed by _ Prints string & Chr( 34 ) & "mailto:" & Request( "email" ) & Chr( 34 ) _ Request users name input in the form & ">" & Request( "username" ) & "</a>: " assigns current execute Date() Lines 41-60 server date mailto: link. loaded when pages is Displays a to beginning of mailtoUrl Sumbitted name and email mailtoUrl with post request. ' open the guestbook, 8 is for appending are combined and assigned to ' create the guestbook if it does not exist Request retrieves emailDate() Set textFile = _ sring Pass value 34 mailtoUrl fileObject.OpenTextFile( guestbook,and username into VBScript 8, True ) Chr function to produce double Method OpenTextFile 34 ' write data to guestbook.txt quotes () retrieves TextStream Call textFile.WriteLine( "<hr />" Contstant 8 indicates & mailtoUrl & _ Request( "comment" ) ) object for accessing file WriteLine append mode (writing to Call textFile.Close() guestbook.txt End If the end ofmethod closes the file Close() the file.) Close() TextStream method WriteLine <p>Please leave a message in our guestbook.</p> writes to Append Mode guestbook.txt

2001 Prentice Hall, Inc.


All rights reserved.

33
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 <!-- write form to the client --> <form action = "guestbook.asp" method = "post"> <table> <tr> <td>Your Name: </td> Form post action <td><input class = "font" type = "text" size = "60" name = "username" /></td> </tr>

Outline
Guestbook.asp to .asp page
Users enter name, email address and comments. FSOs write this data to a server file.

<tr> <td>Your email address:</td> Form contains two <td><input class = "font" text fields and text type = "text" size = "60" area for user input name = "email" value = "user@isp.com" /> </td> </tr>
<tr> <td>Tell the world: </td> <td><textarea name = "comment" rows = "3" Passes parameter cols = "50"> hiddenInput Replace this text with the information you would like to post.</textarea></td> value true </tr> </table> <input type = "submit" value = "submit" /> <input type = "reset" value = "clear" /> <input type = "hidden" name = "hiddenInput" value = "true" /> </form>

<form> post action

hiddenInput

2001 Prentice Hall, Inc.


All rights reserved.

34
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 <% ' check if the file exists If fileObject.FileExists( guestBook ) = True Then

Outline

Guestbook.asp FSO object ' open the guestbook, "1" is for reading FileExists checks if Set textFile = fileObject.OpenTextFile( guestbook, 1 ) Users enter name, email guestbook.txt address and comments. ' read the entries from the file and exists. them to write FSOs write this data to a ' the client. Lines 100-115 file. server execute Call Response.Write( "Guestbook Entries:<br />" & _ every time the client textFile.ReadAll() ) requests FileExists() this ASP page Call textFile.Close() TextStream and
End If %> </body> </html>

ReadAll methods read TextStream() Read entries from contents of entire guestbook.txt and Response.Write writes guestbook.txt ReadAll() write XHTMLclient. client text to to the Contains XHTML markup rendered Response.Write on client browser.

2001 Prentice Hall, Inc.


All rights reserved.

35

Outline
Program Output

2001 Prentice Hall, Inc.


All rights reserved.

36

Outline
Program Output

2001 Prentice Hall, Inc.


All rights reserved.

37
1 <hr />5/24/2001 <a href = "mailto:tem@deitel.com">tem</a>: ASP is a great tool for server-side development. 2 <hr />5/24/2001 <a href = "mailto:dan@bushycat.com">dan</a>: ASP is my preferred server-side development tool.

Outline
XHTML generated by guestbook.asp

2001 Prentice Hall, Inc.


All rights reserved.

38

25.7 Session Tracking and Cookies Session Tracking and Cookies


Helps server to distinguish between clients Provide custom content
Shopping cart Marketing/advertising

SessionID
Assigned by server to each unique session Compared with sessionIDs stored in server

Session object
Timeout property Length of session before it expires Abandon property Terminates individual session
2001 Prentice Hall, Inc. All rights reserved.

39

25.7 Session Tracking and Cookies ASP application


Multiple ASP pages linked with session variables Example
instantpage.asp Form, requests information from the user Posted to process.asp process.asp Redirects user to instantpage.asp if errors exist Otherwise creates users ASP page Stores message in session variable welcomeBack Every time user submits the form

2001 Prentice Hall, Inc. All rights reserved.

40

25.7 Session Tracking and Cookies Server-side include


Commands embedded in XHTML documents Add dynamic content Places a .shtml include file within another file
Physical or virtual path
<!--#include file = includes\file.shtml -->

Not all Web servers support


Written as comment

Performed before scripting code is interpreted.


ASP page cannot determine which includes to use

Can contain scripting code


Must use <script> tag or <% %> delimeters

2001 Prentice Hall, Inc. All rights reserved.

41
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <% @LANGUAGE = VBScript %>

Outline
Instantpage.asp
Recieves error message from process.asp Uses server side include to create dynamic content (embedded file)

<%
' Fig. 25.15 : instantpage.asp ' ASP document that posts data to process.asp Option Explicit %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Instant Page Content Builder</title> <style type = "text/css"> table { text-align: center; font-size: 12pt; color: blue; font-size: 12pt; font-family: arial, sans-serif } </style> </head>

Server-side include

<body>
<!-- include the header --> <!-- #include virtual = "/includes/header.shtml" --> <h2>Instant Page Content Builder</h2>

2001 Prentice Hall, Inc.


All rights reserved.

42
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 <% ' if process.asp posted an error, print the error ' message. If Session( "errormessage" ) <> "no error" Then Call Response.Write( Session( "errorMessage" ) ) ' otherwise, print the welcome back message, if any Else Session object First time Call Response.Write( Session( "welcomeBack" ) )page is executed, End If retrieves variable value line 35 returns true %>

Outline
Instantpage.asp
session variable with no explicit value contains an empty string

If true, errorMessage errorMessage welcomeBackexecuted When is variable tosession value line 36 is client written sessionuser --> used for <!-- a form to get the information from the variable displays never andstatement tests errorMessage has errorMessage error <form action = "process.asp" method = If messages "post"> Else, WelcomeBack errors is welcomeline 36 message no value, back does not has a value unless value <table> errorMessage written encountered the client. to returningno error. <tr> print anything to are to the client equality to users <td>Your Name: </td> Return True or False. Requests process.asp <td><input type = "text" size = "60" name = "username" /></td> is posted when form
</tr>
<tr> <td>Enter the Filename:</td> <td><input type = "text" size = "60" name = "filename" value = "yourfilename" /></td> </tr> <tr> <td>Enter the Title:</td> <td><input type = "text" size = "60" name = "doctitle" value = "document title" /></td> </tr>

2001 Prentice Hall, Inc.


All rights reserved.

43
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 <tr> <td>Enter the content:</td> <td><textarea name = "content" rows = "3" cols = "50"> Replace this text with the information you would like to post.</textarea></td> </tr> </table>

Outline
Instantpage.asp

Server-side include
<input type = "submit" value = "submit" /> <input type = "reset" value = "clear" /> </form> <!-- #include virtual = "/includes/footer.shtml" --> </body> </html>

2001 Prentice Hall, Inc.


All rights reserved.

44

Outline
Program Output

2001 Prentice Hall, Inc.


All rights reserved.

45

Outline
Program Output

2001 Prentice Hall, Inc.


All rights reserved.

46
1 2 3 4 5 <!-- Fig. 25.16: header.shtml --> <!-- Server-side include file containing XHTML --> <hr style = "color: blue" /> <img height = "100" src = "/images/bug2bug.gif" alt = "Bug" /> <hr style = "color: blue" />

Outline
Header.shtml
server-side include file for the document header

2001 Prentice Hall, Inc.


All rights reserved.

47
1 2 3 4 5 6 7 8 <!-- Fig. 25.17: footer.shtml --> <!-- Server-side include file containing XHTML --> <hr style = "color: blue" /> <a style = "text-align: center" href = "mailto:orders">ordering information</a> <a style = "text-align: center" href = "mailto:editor">contact the editor</a><br /> <hr style = "color: blue" />

Outline
Footer.shtml
server-side include file for the document footer

2001 Prentice Hall, Inc.


All rights reserved.

48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <% @LANGUAGE = VBScript %>

Outline
Process.asp
Creates the users ASP document and presents a link to the users page Requested by instantpage.asp

<%
' Fig. 25.18 : process.asp ' ASP document that creates user's ASP document Option Explicit %> <% Dim message, q q = Chr( 34 ) ' assign quote character Session( "errorMessage" ) = "no error"

If field is empty or contains default string yourfilename, lines 19-21 assign validates If statement XHTML error message to variable tocontents of text field. q message.

' check to make sure that they have entered a Assign message value ' valid filename to session ) _ If ( LCase( Request( "filename" ) ) = "yourfilename"variable Or Request( "filename" ) = "" Then errorMessage message = "<p style = " & q & "color: red" & q & _ ">" & "Please enter a valid name or filename.</p>"created if FSO object is Session( "errorMessage" ) = message valid user name is entered Call Server.Transfer( "instantpage.asp" ) and assigned reference End If

Request.ServerVariables fileObject retrieves physical path Dim directoryPath, filePath, fileObject, fileName method path where Server Specify server Transfer requests Builds fileName by ASP file is written ' Create a FileSystem Object instantpage.asp concatenating Set fileObject = Server.CreateObject( _ "Scripting.FileSystemObject" ) filename to the .asp file extension directoryPath = _
Request.ServerVariables( "APPL_PHYSICAL_PATH" ) fileName = Request( "filename" ) & ".asp"

2001 Prentice Hall, Inc.


All rights reserved.

49
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 ' build path for text file filePath = directoryPath & "\" & fileName ' check if the file already exists If fileObject.FileExists( filePath ) Then message = "<p style = " & q & "color: red" & q & _ ">" & "The file name is in use.<br />" & _ Builds file path by concatenating "Please use a different file name.</p>" file filePath passed FileExists Session( "errorMessage" ) = messagename to directory path. Call Server.Transfer( "instantpage.asp" ) variablefile exists. Assigned to method. Determines if filePath. End If

Outline
Process.asp
Creates the users ASP document and presents a link to the users page Requested by instantpage.asp

If file exists, variable ' save XHTML for the welcome back message errorMessage value is set to Server Transfer method requests ' in a session variable XHTML containing message = "<p style = " & q & "color: blue" & q & _ error message instantpage.asp
">" & "Welcome Back, " & Request( "username" ) & _ "</p><br />" Session( "welcomeBack" ) = message Dim header, footer, textFile, openMark, closeMark openMark = "<" & "%" Assigns XHTML forfor header Construct XHTML welcome closeMark = "%" & ">" ' build the header. ' vbCrLf inserts a carriage return/linefeed into the text Assign ASP scripting ' string which makes the XHTML code more readable delimeters & closeMark _ header = openMark & " @LANGUAGE = VBScript " to string variables & vbCrLf & openMark & " ' " & fileName _ openMark and closeMark & " " & closeMark & vbCrLf & vbCrLf _ & "<!DOC" & "TYPE html PUBLIC " & q & _ "-//W3C//DTD XHTML 1.0 Transitional//EN" & q & _ vbCrLf & q & "http://www.w3.org/TR/xhtml1/" & _ "DTD/xhtml1-transitional.dtd" & q & ">" & vbCrLf & _ "<html xmlns = " & q & "http://www.w3.org/1999/xhtml" & _ q & ">" & vbCrLf & "<head>" & vbCrLf _

back message to welcomeBack session variable

2001 Prentice Hall, Inc.


All rights reserved.

50
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 & "<meta name = " & q & "author" & q & " content = " _ & q & Request( "username" ) & q & " />" & vbCrLf _ & "<meta name = " & q & "pubdate" & q & " content = " _ & q & Date() & q & " />" & vbCrLf _ & "<title>" & Request( "doctitle" ) & "</title>" _ & vbCrLf & "</head>" & vbCrLf & "<body>" & vbCrLf _ & "<!-- #" & "include " & "virtual = " & _ "/includes/header.shtml -->" _ footer variable assigned & vbCrLf & "<h2 style = " & q & "text-align: center" & _ q & "><em>" & Request( "doctitle" ) & to XHTML footer contents "</em></h2>" & _ vbCrLf & "<br />" & vbCrLf

Outline
Process.asp
Creates the users ASP document and presents a link to the users page Requested by instantpage.asp

' build the footer using a different ' building the string footer = vbCrLf & "<br /><br /><br />" & vbCrLf & _ "You have requested this page on " & _ openMark & " =Date() " & closeMark & "," & _ vbCrLf & "at " & openMark & " =Time() Write header, text area " & _ closeMark & "." & vbCrLf & _ contents text and footer "<!-- #" & "include " & "virtual = " & _ to text file "/includes/footer.shtml -->" _ & vbCrLf & vbCrLf & "</body>" & vbCrLf & "</html>"

Form values retrieved style for Request object using

' create the ASP file Set textFile = fileObject.CreateTextFile( filePath, False ) With textFile Call .WriteLine( header & Request( "content" ) & _ send XHTML to Lines 103-129 footer ) client that contains link to Call .Close() created page End With %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2001 Prentice Hall, Inc.


All rights reserved.

51
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 <html xmlns = "http://www.w3.org/1999/xhtml">

Outline
Process.asp

<head> <!-- use the title given by the user --> <title>File Generated: <% =fileName %></title>
<style type = "text/css"> h2 { font-family: arial, sans-serif; text-align: center } </style> </head>

Creates the users ASP document and presents a Lines 103-129 send XHTML to users page link to the

client that contains link to created page Requested by

<body> <!-- #include virtual = "/includes/header.shtml" --> <h2><em>File <% =fileName %> was created successfully.</em> </h2><br /> <!-- provide a link to the generated page --> <a href = "<% =fileName %>">View your file</a> <!-- #include virtual = "/includes/footer.shtml" --> </body> </html>

instantpage.asp

2001 Prentice Hall, Inc.


All rights reserved.

52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <% @LANGUAGE = VBScript %> <% ' test.asp %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta name = "author" content = "tem" /> <meta name = "pubdate" content = "2/27/2001" /> <title>XML How to Program</title> </head> <body> <!-- Fig. 25.16: header.shtml --> <!-- Server-side include file containing XHTML --> <hr style = "color: blue" /> <img height = "100" src = "/images/bug2bug.gif" alt = "Bug" /> <hr style = "color: blue" /> <h2 style = "text-align: center"><em>XML How to Program</em></h2> <br /> The authoritative Deitel&#8482; Live-Code&#8482; introduction to XML-based systems development. ISBN 0-13-028417-3 <br /><br /><br /> You have requested this page on 2/27/2001, at 10:14:44 PM. <!-- Fig. 25.17: footer.shtml --> <!-- Server-side include file containing XHTML --> <hr style = "color: blue" /> <a style = "text-align: center" href = "mailto:orders">ordering information</a> -

Outline
Test.asp
XHTML file created by process.asp

2001 Prentice Hall, Inc.


All rights reserved.

53
33 34 35 36 37 38 <a style = "text-align: center" href = "mailto:editor">contact the editor</a><br /> <hr style = "color: blue" /> </body> </html>

Outline
Test.asp
XHTML file created by process.asp

Program Output

Fig. 25.20 Welcome back message displayed by instantpage.asp.

2001 Prentice Hall, Inc.


All rights reserved.

54

25.7 Session Tracking and Cookies

Fig. 25.21 Error message generated by instantpage.asp.


2001 Prentice Hall, Inc. All rights reserved.

55

25.8 Accessing a Database from an Active Server Page Web applications


Communicating with databases
Use ActiveX Data Objects (ADO) Provides uniform way for programs to connect with databases

Three-tier distributed applications


User interface Created with XHTML, DHTML or XML Contains ActiveX controls, client-side scripts, Java applets Communicate directly with business logic Business logic Database access May reside on separate computers
2001 Prentice Hall, Inc. All rights reserved.

56

25.8 Accessing a Database from an Active Server Page


Web applications, cont.
Three-tier distributed applications, cont.
Web servers build middle tier Provide business logic Manipulates databases Communicates with client Web browsers

ASP communications with databases


Use SQL-based queries ADO handled specifics Through OLE DB Databases provide data source for dynamic content Allows users who are not familiar with Web languages to create Web pages Restrict access Password protection Query Access database
2001 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

<% @LANGUAGE = VBScript %> <% ' Fig. 25.22 : database.asp Ignores errors until ' ASP document for interacting with the database Server method Option Explicit end of script Dim connection, loginData

57

Outline
Database.asp
connects to, and queries an Access database CreateObject ADODB.Connection contains functionality necessary to connect to database

CreateObject creates ADODB.Connection ' provide error handling code Method session DeclaresOpen opens On Error Resume Next When Open finishes variable specified by database loginData reference is set Session( "errorString" ) = "" executing, points to first errorString string OpenerrorHandlerLog method is passed ODBC System DSN to an ADODB.recordset Set connection = Server.CreateObject( "ADODB.Connection"EOF if no record or ) processes query (login) containing SQL errors and object Call connection.Open( "login" ) errorHandlerLog records were found Call errorHandlerLog() ADODB.Connection object called again ' create the record set Err object Set loginData = Server.CreateObject( "ADODB.Recordset" ) Number Call loginData.Open( Session( "query" ), connection ) contains VBScript property Set Session( "loginData" ) = loginData Lines 25-36 define error number. Tests if error If true, errorSrting Call errorHandlerLog() errorHandlerLog has occurred. Error number and message variable is assigned Sub errorHandlerLog() concatenated containing XHTML text to variable If Err.Number <> 0 Then errorString message Dim errorString error number and
errorString = Session( "errorString" ) Sets session & _ errorString = errorString & "<p class = " variable loginData to Chr( 34 ) & "error" & Chr ( 34 ) & loginData which references variable ">Error (" _ & Err.Number & ") in " & Err.Source & "<br />" & _ Err.Description & "</p><br the ADODB.Recordset containing />" Session( "errorString" ) = errorString matching SQL query all records End If End Sub %>

errorHandlerLog

2001 Prentice Hall, Inc.


All rights reserved.

58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <% @LANGUAGE = VBScript %>

Outline
Login.asp
Identifies users by prompting them for login name and password. Data is stored in Access database login.mdb

<%
' Fig. 25.23 : login.asp ' ASP document to login to instantpage.asp Option Explicit ' create the SQL query Session( "query" ) = "SELECT loginID FROM Users" Call Server.Execute( "database.asp" )

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" Assigns SQL "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> query <html xmlns =

to session variable submitlogin.asp Executes database.asp to "http://www.w3.org/1999/xhtml"> query retrieve login IDs from the databasevalidates the users login

<head> <title>Login Page</title> <style type = "text/css"> table { text-align: center; font-size: 12pt; color: blue; font-size: 12pt; font-family: arial, sans-serif } .error { color: red } </style> </head> <body> <!-- #include virtual="/includes/header.shtml" -->

<%

2001 Prentice Hall, Inc.


All rights reserved.

59
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 If Session( "errorString" ) = "" Then ' if this is a return after a failed attempt, ' print an error If Session( "loginFailure" ) = True Then %> <p class = "error">Login attempt failed, please try again</p> Tests if session <% End If ' begin the form %> <p>Please select your name and enter your password to login:</p><br />

Outline
Login.asp
Prompts user for login name and password. Information is stored in

variable errorString value is empty string

If false, line 89 prints Access database opened in error message database.asp <form action = "submitlogin.asp" method = "post"> to user Lines 39-41 test is session variable <!-- format the form using a table --> is True loginFailure <table border = "0"> If true, login failure select structure builds <tr> message prints of loginIDs drop-down list to user and <td>Name:</td> prompts new login
<td> <select name = "loginID"> Requests <option value = "noSelection"> Select your name</option>

loginID cookie

<%

Selects the returning users If Request.Cookies( "loginID" ) <> "" Then login ID option Call BuildReturning() Else Build loginID options Call BuildNewUser() End If
</select> </td> </tr>

%>

2001 Prentice Hall, Inc.


All rights reserved.

60
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 <tr> <td>Password:</td> <td><input type = "password" name = "password" /></td> </tr> <tr> <td></td> <td align = "left"> <input type = "submit" value = "Log Me In" /> </td> </tr> </table> </form> <!-- #include virtual="/includes/footer.shtml" --> <% Else Call Response.Write( Session( "errorString" ) ) End If %> </body> </html> <% ' builds the option items for loginIDs and writes ' selected for the loginID of the returning user Sub BuildReturning() Dim found, loginData Set loginData = Session( "loginData" ) ' pull user names from the record set to populate the ' dropdown list

Outline
Login.asp
Prompts user for login name and password. Information is stored in Access database opened in database.asp

2001 Prentice Hall, Inc.


All rights reserved.

61
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 found = False

Outline
Login.asp
Prompts user for login name and password.

%> <%

Tests for EOF While Not loginData.EOF ' create this record's dropdown entry <option ' if we did not write selected for found set to false any option ' before before If statement writes loop If ( Not found ) Then

' Information is stored in ' Access database opened in ' database.asp ' ' ' for an option by setting found to True. If Request.Cookies( "loginID" ) _ = loginData( "loginID" ) Then Increments the record set pointer Call Response.Write( "selected = " & _ to next record Chr( 34 ) & "selected" & Chr( 34 ) ) found = True End If End If If true, lines 122-124 writeis Once selected %> value = "<% =loginData( "loginID" )selected and set found %>"> written for an option, <% =loginData( "loginID" ) %></option> to truefound set whether current <% Call loginData.MoveNext() Determines to true Wend records loginID field is End Sub ' builds the option items for loginIDs ' selected for any loginID Sub BuildNewUser() Dim loginData

selected for an if the current record's loginID is equal to option the loginID cookie, then it is the loginID of If statement tests whether the returning user, and thus we need to write option we also selected for this option; in this case needs to be need to signal that we have written selected selected

whileequal (lines 107-130) iterates loop to loginID cookie through loginDatas as without writing display records Writes option current loginID Sets option value to current loginID

Set loginData = Session( "loginData" )

2001 Prentice Hall, Inc.


All rights reserved.

62
140 141 142 143 144 145 146 147 148 149 ' pull user names from the record set to populate the ' dropdown list While Not loginData.EOF ' create this record's dropdown entry %> <option value = "<% =loginData( "loginID" ) %>"> <% =loginData( "loginID" ) %></option> <% Call loginData.MoveNext() Wend End Sub %>

Outline
Login.asp

Program Output

2001 Prentice Hall, Inc.


All rights reserved.

63

Outline

Program Output

2001 Prentice Hall, Inc.


All rights reserved.

64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <% @LANGUAGE = VBScript %>

<% ' Fig. 25.24 : submitlogin.asp ' ASP document to check user's username Option Explicit

Lines 9-13 check whether password field is empty or id loginID field and password contains default value

Outline
Submitlogin.asp
Takes values passed by login.asp and checks values against Users table in database

' test if a user name and a password were ' entered. If not, transfer back to the login page. If so, variable If Request( "password" ) = "" Or _ loginFailure set to Request( "loginID" ) = "noSelection" Then Session( "loginFailure" ) = True true, client redirected Call Server.Transfer( "login.asp" ) back to login.asp End If

If match is found, user is WHERE specifies a redirected to instantpage.asp. If Dim connection, loginData condition on the Sets session variable Checks password againstwhich records not, are selected loginData to user is redirected to password in recordset Sets reference ' create the loginFailure SQL query Executes database.asp login.asp.
Session( "query" ) =False session variable loginData value to _ to query database "SELECT * FROM Users WHERE loginID = '" & _ (contains records matching Request( "loginID" ) & "'"

query.)

Sets cookies expiration date to If Request( 3 days current date plus"password" ) =

Call Server.Execute( "database.asp" ) Set loginData = Session( "loginData" )

If true, line writes forms loginData( "password" ) Then loginID value as cookie named

' password is OK, adjust loginFailure loginID Session( "loginFailure" ) = False

' write a cookie to recognize them the next time they ' go to login.asp Response.Cookies( "loginID" ) = Request( "loginID" ) ' give it three days to expire Response.Cookies( "loginID" ).Expires = Date() + 3

2001 Prentice Hall, Inc.


All rights reserved.

65
36 37 38 39 40 41 42 43

Otherwise loginFailure set to True Calls Server method Transfer to ' send them to instantpage.asp and client is redirected to login.asp client to instantpage.asp redirect Call Server.Transfer( "instantpage.asp" )
Else Session( "loginFailure" ) = True Call Server.Transfer( "login.asp" ) End If %>

Outline
Submitlogin.asp

Program Output

2001 Prentice Hall, Inc.


All rights reserved.

66

Outline

Program Output

2001 Prentice Hall, Inc.


All rights reserved.

67

25.8 Accessing a Database from an Active Server Page

Fig. 25.25 Cookies folder before and after cookie creation.


2001 Prentice Hall, Inc. All rights reserved.

68

25.8 Accessing a Database from an Active Server Page

Fig. 25.26 Error messages sent to login.asp by database.asp.

2001 Prentice Hall, Inc. All rights reserved.

69

25.9 Server-Side ActiveX Components ActiveX controls on the server with no GUI
Make features available in ASP AdRotator ActiveX component
Rotates advertisements on a Web page Randomly displays one of several advertisements Minimizes space on a Web page committed to advertisements Client does not have to support ActiveX technologies (on the server)

PageCounter ActiveX component


Page hit counter

2001 Prentice Hall, Inc. All rights reserved.

70

25.9 Server-Side ActiveX Components


C o m p o ne nt Na m e De sc rip tio n

MSWC.BrowserType MSWC.AdRotator MSWC.NextLink MSWC.ContentRotator MSWC.PageCounter MSWC.Counters MSWC.MyInfo Scripting.FileSystemObject


ActiveX Data Objects (ADO) Data Access Components

ActiveX component for gathering information about the clients browser (e.g., type, version, etc.). ActiveX component for rotating advertisements on a Web page. ActiveX component for linking Web pages together. ActiveX component for rotating HTML content on a Web page. ActiveX component for storing the number of times a Web page has been requested. ActiveX component that provide general-purpose persistent counters. ActiveX component that provides information about a Web site (e.g., owner name, owner address, etc.). ActiveX component that provides an object library for accessing files on the server or on the servers network. ActiveX components that provide an object library for accessing databases.

Fig. 25.27

S m e se rve r-sid e Ac tive X c o m p o ne nts. o

2001 Prentice Hall, Inc. All rights reserved.

71
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <% @LANGUAGE = VBScript %>

Outline
Component.asp
Uses AdRotator ActiveX component to rotate one of five flag images. When user clicks flag image, countrys corresponding CIA Fact Book Web page displays.

<%
' Fig. 25.28 : component.asp ' Demonstrating Server-side ActiveX Components Option Explicit %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>ActiveX Component Example</title> </head> <body> <strong style = "font-family: arial, sans-serif"> assigns it reference rotator Sends advertisement as HTML to client. Method Server-side ActiveX Components GetAdvertisement called using reference rotator. </strong>

Creates AdRotator component instance,

Retrieves advertisements from config.txt


<p> <% 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" ) )

2001 Prentice Hall, Inc.


All rights reserved.

72
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 ' create a BrowserType object Set browser = Server.CreateObject( "MSWC.BrowserType" ) If browser.VBScript = True Then %> <script language Obtains information about users browser = "VBScript"> Call Msgbox( "Client browser supports VBScript!" ) </script> End If

Outline
Component.asp

<%

Check property VBScript value If true, lines 48-50 written to client

If browser.JavaScript = True Then %> <script language = "JavaScript">Passes server variable key alert( "Client browser supports JavaScript!" ); to HTTP_USER_AGENT </script> ServerVariables, obtains

<%BrowserType objects Browser End If Version and MinorVer properties

string containing user information

canget client's browser information obtain similar client information '


information = "<p>Your browser information is:<br />" & _ Lines 46-52 test JavaScript Request.ServerVariables( "HTTP_USER_AGENT" ) & _ "<br />Browser: " & browser.Browser & " Version: " & _ property browser.Version & " Minor version: " & _ browser.MinorVer & "<br />Cookies are "

If browser.Cookies Then information = information & "enabled</p><br />" Else information = information & "disabled</p><br />" Tests Cookies property to determine End If
Call Response.Write( information )

if browser supports cookies


2001 Prentice Hall, Inc.
All rights reserved.

73
69 70 71 72 73 74 75 76 77 78 79 ' create Page Counter Object Set counter = Server.CreateObject( "MSWC.PageCounter" ) Call counter.PageHit() ' page has been "hit" %> </p>

Outline
Component.asp

Returns number of hits

<p style = "color: blue; font-size: 12pt"> Increments number of hits %> This page has been visited <% =counter.Hits() by one times!</p> </body> </html>

2001 Prentice Hall, Inc.


All rights reserved.

74

Outline
Program Output

2001 Prentice Hall, Inc.


All rights reserved.

75

Outline
Program Output

2001 Prentice Hall, Inc.


All rights reserved.

76
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 REDIRECT redirect.asp Header contains width 54 Image URL, URL REDIRECT file destination URL, height 36 Advertisement dimensions alt value, image display ratio border 1 Asterisk separates header * from advertisements /images/us.gif http://www.odci.gov/cia/publications/factbook/geos/us.html United States Information 20 /images/france.gif http://www.odci.gov/cia/publications/factbook/geos/fr.html France Information 20 /images/germany.gif http://www.odci.gov/cia/publications/factbook/geos/gm.html Germany Information 20 /images/italy.gif http://www.odci.gov/cia/publications/factbook/geos/it.html Italy Information 20 /images/spain.gif http://www.odci.gov/cia/publications/factbook/geos/sp.html Spain Information 20

Outline
Config.txt
Describes the advertisements

2001 Prentice Hall, Inc.


All rights reserved.

77
1 2 3 4 5 6 7 8 9 <% @LANGUAGE = VBScript %>

Outline
Redirect.asp
Redirects user to countrys CIA page when ad is clicked

<%
' Fig. 25.30 : redirect.asp ' Redirection Page for AdRotator Component Option Explicit Call Response.Redirect( Request( "url" ) ) %>

2001 Prentice Hall, Inc.


All rights reserved.

Вам также может понравиться