4.2 ASP Basics
-
Upload
chidambaram -
Category
Documents
-
view
218 -
download
0
Transcript of 4.2 ASP Basics
-
7/28/2019 4.2 ASP Basics
1/102
2004 Prentice Hall, Inc. All rights reserved.
1
Active Server Pages (ASP)
Outline
33.1 Introduction
33.2 How Active Server Pages Work
33.3 Setup
33.4 Active Server Page Objects
33.5 Simple ASP Examples
33.6 File System Objects
33.7 Session Tracking and Cookies
33.8 ActiveX Data Objects (ADO)
33.9 Accessing a Database from an Active Server Page
33.10 Server-Side ActiveX Components
33.11 Web Resources
-
7/28/2019 4.2 ASP Basics
2/102
2004 Prentice Hall, Inc. All rights reserved.
2
Objectives
In this tutorial, you will learn: To program Active Server Pages using VBScript.
To understand how Active Server Pages work.
To understand the differences between client-side scripting
and server-side scripting. To be able to pass data between Web pages.
To be able to use server-side include statements.
To be able to use server-side ActiveX components.
To be able to create sessions. To be able to use cookies.
To be able to use ActiveX Data Objects (ADO) to access a
database.
-
7/28/2019 4.2 ASP Basics
3/102
2004 Prentice Hall, Inc. All rights reserved.
3
33.1 Introduction
Server-side technologies Dynamically creates Web pages
Use client information, server information and information
from the Internet
Active Server Pages (ASP)
Microsoft Server-side technology
Dynamically build documents in response to client requests
Deliver dynamic Web content
XHTML, DHTML, ActiveX controls, client-side
scripts and Java applets
-
7/28/2019 4.2 ASP Basics
4/102
2004 Prentice Hall, Inc. All rights reserved.
4
33.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
-
7/28/2019 4.2 ASP Basics
5/102
2004 Prentice Hall, Inc. All rights reserved.
5
33.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)
-
7/28/2019 4.2 ASP Basics
6/102
2004 Prentice Hall, Inc. All rights reserved.
6
33.3 Setup
Web ServerNeed to run Web server to use Active Server Pages
IIS 5.0 (Internet Information Services) or higher
Create a virtual directory
Copy files to c:\InetPub\Wwwroot
-
7/28/2019 4.2 ASP Basics
7/102 2004 Prentice Hall, Inc. All rights reserved.
7
33.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 information to client
XHTML, text
Server Access to server methods or properties
-
7/28/2019 4.2 ASP Basics
8/102 2004 Prentice Hall, Inc. All rights reserved.
8
33.4 Active Server Page Objects
Object Name DescriptionRequest Used to access information passed by an HTTP request.
Response Used to control the information sent to the client.
Server Used to access methods and properties on the server.
Fig. 25.1Commonly used ASP objects.
-
7/28/2019 4.2 ASP Basics
9/102 2004 Prentice Hall, Inc. All rights reserved.
9
33.5 Simple ASP Examples
ASP Scripts Scripting delimiters
@LANGUAGE directive
Option Explicit
As in VBScript, forces all variables to be declared in advance
FormatDateTime Time to display
Now
Format to display in
1 LANGUAGE VBS i
-
7/28/2019 4.2 ASP Basics
10/102
2004 Prentice Hall, Inc.
All rights reserved.
Outline10
1
2
3
8
9
11
12
13
14 15 A Simple ASP Example
16
17
18 td { background-color:black;
19 color:yellow }
20 strong { font-family:arial, sans-serif;
21 font-size:14pt; color:blue }
22 p { font-size:14pt }
23
24
25
clock.asp
(1 of 2)
Scripting delimiter
wraps around code
executed on the server.
Processing directive
specifying scripting
languageRequires programmer explicitly
define all variables
26
-
7/28/2019 4.2 ASP Basics
11/102
2004 Prentice Hall, Inc.
All rights reserved.
Outline11
26
27
28
29
A Simple ASP Example
30
31
32
33
34
35
36
37
38
39 40
41
42
43
clock.asp
(2 of 2)
Returns server date and time
(Now) as a string formatted in
vbLongDate format
-
7/28/2019 4.2 ASP Basics
12/102 2004 Prentice Hall, Inc. All rights reserved.
12
33.5 Simple ASP Examples
Fig. 33.2 Simple Active Server Page.
1
-
7/28/2019 4.2 ASP Basics
13/102
2004 Prentice Hall, Inc.
All rights reserved.
Outline13
HTML generated by
clock.asp
(1 of 2)
1
3
4
5
6
7A Simple ASP Example
8
9
10 td { background-color: black;
11 color:yellow }
12 strong { font-family:arial, sans-serif;
13 font-size:14pt; color:blue }
14 p { font-size:14pt }15
16
17
18
19
20
21
A Simple ASP Example
-
7/28/2019 4.2 ASP Basics
14/102
2004 Prentice Hall, Inc.
All rights reserved.
Outline14
HTML generated by
clock.asp
(2 of 2)
21
A Simple ASP Example
22
23
24
25 Thursday, May 24, 2001
26
27
28
29 2:22:58 PM
30
31
32
33
3435
-
7/28/2019 4.2 ASP Basics
15/102 2004 Prentice Hall, Inc. All rights reserved.
15
33.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
161
-
7/28/2019 4.2 ASP Basics
16/102
2004 Prentice Hall, Inc.
All rights reserved.
Outline16
1
3
4
5
6
7
8
9
10 Name Request
11
12
13
1415
16 Enter your name:
17
18
name.html
(1 of 2)
1719
-
7/28/2019 4.2 ASP Basics
17/102
2004 Prentice Hall, Inc.
All rights reserved.
Outline17
19
20
21
22
24
25
26
27
28
action set to ASP file
where information is
posted.
name.html
(2 of 2)
-
7/28/2019 4.2 ASP Basics
18/102 2004 Prentice Hall, Inc. All rights reserved.
18
33.5 Simple ASP Examples
Fig. 33.4 XHTML document that requests an ASP.
191
-
7/28/2019 4.2 ASP Basics
19/102
2004 Prentice Hall, Inc.All rights reserved.
Outline19
2
3
8
9
11
12
13
14 15 Name Information
16
17
18 p { font-family:arial, sans-serif;
19 font-size:14pt; color:navy }
20 .special { font-size:20pt; color:green }
21
22
name.asp
(1 of 2)
2023
-
7/28/2019 4.2 ASP Basics
20/102
2004 Prentice Hall, Inc.All rights reserved.
Outline20
24
25
26
27
Hi ,
28
Welcome to ASP!
29
30
31
32
name.asp
(2 of 2)Request object retrieves
form data from text field
namebox
21
-
7/28/2019 4.2 ASP Basics
21/102 2004 Prentice Hall, Inc. All rights reserved.
21
33.5 Simple ASP Examples
Fig. 33.5 ASP document that responds to a client request.
22
-
7/28/2019 4.2 ASP Basics
22/102
2004 Prentice Hall, Inc. All rights reserved.
22
33.6 File System Objects
File System Objects (FSOs) Allow programmer to manipulate files, directories and drives
Read and write text
Microsoft Scripting Runtime Library (Fig 33.6)
FileSystemObject, File, Folder, Drive andTextStream
Use to create directories, move files, determine whether a
Drive exists
FileSystemObjectmethods (Fig. 33.7)
File object Allows programmer to gather info about files, manipulate
files, open files
File properties and methods (Fig. 33.8)
23
-
7/28/2019 4.2 ASP Basics
23/102
2004 Prentice Hall, Inc. All rights reserved.
23
33.6 File System Objects
Object type Description
FileSystemObject Allows the programmer to interact with Files, Folders and
Drives.
File Allows the programmer to manipulate Files of any type.
Folder Allows the programmer to manipulate Folders (i.e., directories).
Drive Allows the programmer to gather information about Drives (harddisks, RAM diskscomputer memory used as a substitute for harddisks to allow high-speed file operations, CD-ROMs, etc.). Drivescan be local or remote.
TextStream Allows the programmer to read and write text files.
Fig. 33.6 File System Objects (FSOs).
24
-
7/28/2019 4.2 ASP Basics
24/102
2004 Prentice Hall, Inc. All rights reserved.
24
33.6 File System Objects
Methods Description
CopyFileCopies an existing File.
CopyFolder Copies an existing Folder.
CreateFolder Creates and returns a Folder.
CreateTextFile Creates and returns a text File.
DeleteFile Deletes a File.
DeleteFolder Deletes a Folder.
DriveExists Tests whether or not a Drive exists. Returns a boolean.
FileExists Tests whether or not a File exists. Returns a boolean.
FolderExists Tests whether or not a Folder exists. Returns a boolean.
GetAbsolutePathName Returns the absolute path as a string.
Fig. 33.7 FileSystemObjectmethods. (Part 1 of 2)
25
-
7/28/2019 4.2 ASP Basics
25/102
2004 Prentice Hall, Inc. All rights reserved.
25
33.6 File System Objects
Methods Description
GetDriveReturns the specified Drive.
GetDriveName Returns the Drive drive name.
GetFile Returns the specified File.
GetFileName Returns the File file name.
GetFolder Returns the specified File.
GetParentFolderName Returns a string representing the parent folder name.
GetTempName Creates and returns a string representing a file name.MoveFile Moves a File.
MoveFolder Moves a Folder.
OpenTextFile Opens an existing text File. Returns a TextStream.
Fig. 33.7 FileSystemObjectmethods. (Part 2 of 2)
26
-
7/28/2019 4.2 ASP Basics
26/102
2004 Prentice Hall, Inc. All rights reserved.
26
33.6 File System Objects
Property/method Description
Property
DateCreated Date. The date the File was created.
DateLastAccessed Date. The date the File was last accessed.
DateLastModified Date. The date the File was last modified.
Drive Drive. The Drive where the file is located.
Name String. The File name.
ParentFolder String. The Files parent folder name.
Path String. The Files path.
ShortName String. The Files name expressed as a short name.
Size Variant. The size of the File in bytes.
Method
Copy Copy theFile
. Same asCopyFile
ofFileSystemObject
.
Delete Delete the File. Same as DeleteFile ofFileSystemObject.
Move Move the File. Same as MoveFile ofFileSystemObject.
OpenAsTextStream Opens an existing File as a text File. Returns TextStream.
Fig. 33.8 Some commonFile properties and methods.
27
-
7/28/2019 4.2 ASP Basics
27/102
2004 Prentice Hall, Inc. All rights reserved.
27
33.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. 33.9)
28
-
7/28/2019 4.2 ASP Basics
28/102
2004 Prentice Hall, Inc. All rights reserved.
28
33.6 File System Objects
Property/method Description
Properties
Attributes Integer. Value indicating Folders attributes (read only, hidden, etc.).
DateCreated Date. The date the folder was created.
DateLastAccessed Date. The date the folder was last accessed.
DateLastModified Date. The date the folder was last modified.
Drive Drive. The Drive where the folder is located.
IsRootFolder Boolean. Indicates whether or not a Folder is a root folder.
Name String. The Folders name.
ParentFolder Folder. The Folders parent folder.
Path String. The Folders path.
ShortName String. The Folders name expressed as a short name.
ShortPath String. The Folders path expressed as a short path.
Size Variant. The total size in bytes of all subfolders and files.
Type String. The Folder type.
Fig. 33.9 SomeFolderproperties and methods. (Part 1 of 2)
29
-
7/28/2019 4.2 ASP Basics
29/102
2004 Prentice Hall, Inc. All rights reserved.
29
33.6 File System Objects
Property/method Description
MethodsDelete Delete the Folder. Same as DeleteFolder of
FileSystemObject.
Move Move the Folder. Same as MoveFolder of
FileSystemObject.
Copy Copy the Folder. Same as CopyFolder of
FileSystemObject.
Fig. 33.9 Some Folder properties and methods. (Part 2 of 2)
30
-
7/28/2019 4.2 ASP Basics
30/102
2004 Prentice Hall, Inc. All rights reserved.
30
33.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)
31
-
7/28/2019 4.2 ASP Basics
31/102
2004 Prentice Hall, Inc. All rights reserved.
31
33.6 File System Objects
File System Objects (FSOs) Drive object (Fig. 33.10) Gather information about drives
Property DriveLetter
Contains drive letter
Property SerialNumber
Contains drive serial number
Property FreeSpace
Contains number of available bytes
32
-
7/28/2019 4.2 ASP Basics
32/102
2004 Prentice Hall, Inc. All rights reserved.
32
33.6 File System Objects
Property Description
AvailableSpace Variant. The amount of available Drivespace in bytes.
DriveLetter String. The letter assigned to the Drive(e.g., C).
DriveType Integer. The Drivetype. Constants Unknown, Removable, Fixed,
Remote, CDRom and RamDisk represent Drive types and have thevalues 05, respectively.
FileSystem
String. The file system Drivedescription (FAT, FAT32, NTFS, etc.).FreeSpace Variant. Same as AvailableSpace.
IsReady Boolean. Indicates whether or not a Driveis ready for use.
Path String. The Drives path.
RootFolder Folder object. The Drives root Folder.
SerialNumber Long. The Driveserial number.
TotalSize Variant. The total Drivesize in bytes.VolumeName String. The Drivevolume name.
Fig. 33.10 Drive properties.
33
-
7/28/2019 4.2 ASP Basics
33/102
2004 Prentice Hall, Inc. All rights reserved.
33
33.6 File System Objects
File System Objects (FSOs) TextStream object (Fig. 33.11) Manipulate text files
34
-
7/28/2019 4.2 ASP Basics
34/102
2004 Prentice Hall, Inc. All rights reserved.
34
33.6 File System Objects
Property/Method Description
Properties
AtEndOfLine Boolean. Indicates whether the end of a line has been encountered.
AtEndOfStream Boolean. Indicates whether the end of file has been encountered.
Column Integer. Returns the characters position in a line.
Line Integer. Returns the current line number.
Methods
Read String. Returns a specified number of characters from the file
referenced by the TextStreamobject.
ReadAll String. Returns the entire contents of the file referenced by the
TextStreamobject.
Fig. 33.11 TextStream methods and properties. (Part 1 of 2)
35
-
7/28/2019 4.2 ASP Basics
35/102
2004 Prentice Hall, Inc. All rights reserved.
35
33.6 File System Objects
Property/Method Description
Methods, cont.
ReadLine String. Returns one line from the file referenced by the
TextStreamobject.
Write String. Writes text to the file referenced by the TextStreamobject.
WriteBlankLines String. Writes newline characters to the file referenced by the
TextStreamobject.WriteLine String. Writes one line to the file referenced by the TextStream
object.
Skip Variant. Skips a specified number of characters while reading from thefile referenced by the TextStreamobject.
SkipLine Variant. Skips a line of characters while reading from the file
referenced by the TextStreamobject.
Close Close the file referenced by the TextStreamobject.
Fig. 33.11 TextStream methods and properties. (Part 2 of 2)
36
-
7/28/2019 4.2 ASP Basics
36/102
2004 Prentice Hall, Inc. All rights reserved.
36
33.6 File System Objects
Creating a guestbook XHTML Form hidden field
Determine if page loaded by form submission
Write data to text file ServerVariables APPL_PHYSICAL_PATH
OpenTextFile
Append mode
8
37
-
7/28/2019 4.2 ASP Basics
37/102
2004 Prentice Hall, Inc. All rights reserved.
37
33.6 File System Objects
Creating a guestbook, cont.Name returned as mailto: URL
Request object
Chr function
Generate characters from ASCII code
Always Close files
O tli381
2
-
7/28/2019 4.2 ASP Basics
38/102
2004 Prentice Hall, Inc.All rights reserved.
Outline23
7
8
10
11
12
13
14 GuestBook Example15
16
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
23
24
guestbook.asp
(1 of 5)
O tline3925
-
7/28/2019 4.2 ASP Basics
39/102
2004 Prentice Hall, Inc.All rights reserved.
Outline26 Dim fileObject, textFile, guestBook, mailtoUrl27
28 ' get physical path for this ASP page and
29 ' concatenate guestbook.txt to it
30 guestbook = Request.ServerVariables( "APPL_PHYSICAL_PATH" ) _
31 & "\guestbook.txt"
32
33 ' instantiate a FileSystemObject
34 Set fileObject = Server.CreateObject( _
35 "Scripting.FileSystemObject" )
36
37 ' Check if this request is after the user has posted the form
38If Request( "hiddenInput" ) = "true"Then
39
40 ' Print a thank you
41 Call Response.Write( "Thanks for your entry, " & _
42 Request( "username" ) & "!" )
43 %>
44
45
-
7/28/2019 4.2 ASP Basics
40/102
2004 Prentice Hall, Inc.All rights reserved.
Outline5152 ' open the guestbook, 8 is for appending
53 ' create the guestbook if it does not exist
54 Set textFile = _
55 fileObject.OpenTextFile( guestbook, 8, True )
56
57 ' write data to guestbook.txt
58 Call textFile.WriteLine( "" & mailtoUrl & _
59 Request( "comment" ) )
60 Call textFile.Close()
61 End If
62 %>
63
64
Please leave a message in our guestbook.
65
66
67
68
69
70 Your Name: 71
74
guestbook.asp
(3 of 5)
Method OpenTextFileretrieves TextStream object
for accessing fileguestbook.txt
Contstant 8 indicates
append mode (writing to
the end of the file.)
TextStream method
WriteLine writes to
guestbook.txt
Close() method closes the file
Form post action to .asp page
Outline4175
76
-
7/28/2019 4.2 ASP Basics
41/102
2004 Prentice Hall, Inc.All rights reserved.
Outline
guestbook.asp
(4 of 5)
76
77 Your email address:
78
82
83
84
85
86 Tell the world:
87
89 Replace this text with the information
90 you would like to post.
91
92
93
94
95 96
98
99
Form contains two
text fields and text
area for user input
Passes parameter
hiddenInput
value true
Outline42100
-
7/28/2019 4.2 ASP Basics
42/102
2004 Prentice Hall, Inc.All rights reserved.
Outline101 check if the file exists102 If fileObject.FileExists( guestBook ) = TrueThen
103
104
105 ' open the guestbook, "1" is for reading
106 Set textFile = fileObject.OpenTextFile( guestbook, 1 )
107
108 ' read the entries from the file and write them to
109 ' the client.
110 Call Response.Write( "Guestbook Entries:
" & _111 textFile.ReadAll() )
112 Call textFile.Close()
113
114 End If
115 %>
116
117
118
119
guestbook.asp
(5 of 5)
FSO object FileExists
checks ifguestbook.txt
exists.
Read entries from
guestbook.txt and write
XHTML to the client
TextStream and ReadAll
methods read entire
contents ofguestbook.txtResponse.Write writes
text to client. Contains
XHTML markup rendered
on client browser.
43
-
7/28/2019 4.2 ASP Basics
43/102
2004 Prentice Hall, Inc. All rights reserved.
43
33.6 File System Objects
Fig. 33.12 Guestbook Active Server Page.
44
-
7/28/2019 4.2 ASP Basics
44/102
2004 Prentice Hall, Inc. All rights reserved.
33.6 File System Objects
Fig. 33.12 Guestbook Active Server Page.
Outline451 5/24/2001 tem: ASP is a great tool
for server-side development.
-
7/28/2019 4.2 ASP Basics
45/102
2004 Prentice Hall, Inc.All rights reserved.
Outline
XHTML generated by
guestbook.asp
(1 of 1)
for server side development.
2 5/24/2001 dan: ASP is my
preferred server-side development tool.
46
-
7/28/2019 4.2 ASP Basics
46/102
2004 Prentice Hall, Inc. All rights reserved.
33.6 File System Objects
Key name DescriptionAPPL_PHYSICAL_PATHReturns the physical path.HTTPS Boolean. Determines whether the request
came in through SSL (Secure Sockets Layer).
REMOTE_ADDR Clients DNS name or IP address.REQUEST_METHOD Request method (i.e., get and post).SERVER_NAME Servers hostname (DNS or IP address).HTTP_USER_AGENT
Returns information about the client makingthe request.
HTTP_COOKIE Returns cookies residing on the client.Fig. 33.13 Some server variable keys.
47
-
7/28/2019 4.2 ASP Basics
47/102
2004 Prentice Hall, Inc. All rights reserved.
33.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
48
-
7/28/2019 4.2 ASP Basics
48/102
2004 Prentice Hall, Inc. All rights reserved.
33.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
49
-
7/28/2019 4.2 ASP Basics
49/102
2004 Prentice Hall, Inc. All rights reserved.
33.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
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 tag or delimiters
Outline501
2
-
7/28/2019 4.2 ASP Basics
50/102
2004 Prentice Hall, Inc.All rights reserved.
Outline3
8
9
11
12
13
14
15 Instant Page Content Builder
16
17
18 table { text-align:center;
19 font-size:12pt;
20 color:blue;
21 font-size:12pt;22 font-family:arial, sans-serif }
23
24
25
instantpage.asp
(1 of 4)
Outline5126
27 Server-side include
-
7/28/2019 4.2 ASP Basics
51/102
2004 Prentice Hall, Inc.All rights reserved.
Outliney28
29
30
31 Instant Page Content Builder
32
33
-
7/28/2019 4.2 ASP Basics
52/102
2004 Prentice Hall, Inc.All rights reserved.
Outline51
52
53
54 Enter the Filename:
55
56
59
60
61
62 Enter the Title:
63
64
67
68
69 70 Enter the content:
71
72
74 Replace this text with the
-
7/28/2019 4.2 ASP Basics
53/102
2004 Prentice Hall, Inc.All rights reserved.
Outline75 information you would like to post.
76
77
78
79
80
81
82
83
84
85
instantpage.asp
(4 of 4)Server-side include
54
-
7/28/2019 4.2 ASP Basics
54/102
2004 Prentice Hall, Inc. All rights reserved.
33.7 Session Tracking and Cookies
Fig. 33.15 ASP that posts user information to process.asp.
55
-
7/28/2019 4.2 ASP Basics
55/102
2004 Prentice Hall, Inc. All rights reserved.
33.7 Session Tracking and Cookies
Fig. 33.15 ASP that posts user information to process.asp.
56
-
7/28/2019 4.2 ASP Basics
56/102
2004 Prentice Hall, Inc. All rights reserved.
33.7 Session Tracking and Cookies
Fig. 33.15 ASP that posts user information to process.asp.
Outline571
2
-
7/28/2019 4.2 ASP Basics
57/102
2004 Prentice Hall, Inc.All rights reserved.
header.shtml
(1 of 1)
3
4
5
Outline581
2
-
7/28/2019 4.2 ASP Basics
58/102
2004 Prentice Hall, Inc.All rights reserved.
footer.shtml
server-side include file
for the document footer
3
4 ordering information -
6 contact the editor
8
Outline591
2
-
7/28/2019 4.2 ASP Basics
59/102
2004 Prentice Hall, Inc.All rights reserved.
3
-
7/28/2019 4.2 ASP Basics
60/102
2004 Prentice Hall, Inc.All rights reserved.
28 Set fileObject = Server.CreateObject( _
29 "Scripting.FileSystemObject" )
30
31 directoryPath = _
32 Request.ServerVariables( "APPL_PHYSICAL_PATH" )
33
34 fileName = Request( "filename" ) & ".asp"
35
36 ' build path for text file
37 filePath = directoryPath & "\" & fileName
38
39 ' check if the file already exists
40 If fileObject.FileExists( filePath ) Then
41 message = "
" & "The file name is in use.
" & _43 "Please use a different file name.
"44 Session( "errorMessage" ) = message
45 Call Server.Transfer( "instantpage.asp" )
46 End If47
process.asp
(2 of 6)
name is entered and assigned
reference fileObject
Specify server path whereASP file is written
Request.ServerVariables
retrieves physical path Builds fileName by
concatenating
filename to the .asp
file extension
filePath passed FileExists
method. Determines if file exists.
If file exists, variable
errorMessage value is set to
XHTML containing error message
Server.Transfermethod
requests instantpage.asp
Outline6148 ' save XHTML for the welcome back message
49 ' in a session variable
-
7/28/2019 4.2 ASP Basics
61/102
2004 Prentice Hall, Inc.All rights reserved.
50 message = "
" & "Welcome Back, " & Request( "username" ) & _
52 "
"53 Session( "welcomeBack" ) = message
54
55 Dim header, footer, textFile, openMark, closeMark
56 openMark = ""
58
59 ' build the header.
60 ' vbCrLf inserts a carriage return/linefeed into the text
61 ' string which makes the XHTML code more readable
62 header = openMark & " @LANGUAGE = VBScript " & closeMark _
63 & vbCrLf & openMark & " ' " & fileName _
64 & " " & closeMark & vbCrLf & vbCrLf _
65 & "" & vbCrLf & _69 "" & vbCrLf & "" & vbCrLf _
71 & "" & vbCrLf _
73 & "" & vbCrLf _
75 & "" & Request( "doctitle" ) & "" _
76
-
7/28/2019 4.2 ASP Basics
62/102
2004 Prentice Hall, Inc.All rights reserved.
process.asp
(4 of 6)
76 & vbCrLf & "" & vbCrLf & "" & vbCrLf _
77 & "" _
79 & vbCrLf & "" & Request( "doctitle" ) & "" & _
81 vbCrLf & "
" & vbCrLf82
83 ' build the footer using a different style for
84 ' building the string
85 footer = vbCrLf & "
" & vbCrLf & _86 "You have requested this page on " & _
87 openMark & " =Date() " & closeMark & "," & _
88 vbCrLf & "at " & openMark & " =Time() " & _
89 closeMark & "." & vbCrLf & _
90 "" _
92 & vbCrLf & vbCrLf & "" & vbCrLf & ""
93
Form values retrieved
using Request object
footer variable assigned
to XHTML footer contents
Outline6394 ' create the ASP file
95 Set textFile = fileObject.CreateTextFile( filePath, False )
96 i i
-
7/28/2019 4.2 ASP Basics
63/102
2004 Prentice Hall, Inc.All rights reserved.
process.asp
(5 of 6)
96 With textFile
97 Call .WriteLine( header & Request( "content" ) & _
98 footer )
99 Call .Close()
100 EndWith
101 %>
102
103
105
106
107
108
109
110 File Generated:
111
112
113 h2 { font-family:arial, sans-serif;
114 text-align:center }115
116
117
118
Lines 103-129 send XHTML to
client that contains link to
created page
Outline64119
120
121 h2 Fil % fil N %
-
7/28/2019 4.2 ASP Basics
64/102
2004 Prentice Hall, Inc.All rights reserved.
process.asp
(6 of 6)
121 File
122 was created successfully.
123
124
125
126 View your file
127
128
129
Outline65
1
2
-
7/28/2019 4.2 ASP Basics
65/102
2004 Prentice Hall, Inc.All rights reserved.
aboutme.asp
(1 of 2)
3
4
6
7
8
9
10 About Me
11
12
13
14
15
16
17
18 About Me
19
20 This page contains lots of exciting and interesting information about me!
21
22 You have requested this page on 10/15/2003,23 at 11:45:47 AM.
24
25
Outline66
26
27 ordering information
-
7/28/2019 4.2 ASP Basics
66/102
2004 Prentice Hall, Inc.
All rights reserved.
aboutme.asp
(2 of 2)
28 href = "mailto:orders">ordering information -
29 contact the editor
31
32
33
34
67
33 7 S i T ki d C ki
-
7/28/2019 4.2 ASP Basics
67/102
2004 Prentice Hall, Inc. All rights reserved.
33.7 Session Tracking and Cookies
Fig. 33.20 Welcome back message displayed by instantpage.asp.
68
33 7 Session Tracking and Cookies
-
7/28/2019 4.2 ASP Basics
68/102
2004 Prentice Hall, Inc. All rights reserved.
33.7 Session Tracking and Cookies
Fig. 33.21 Error message generated by instantpage.asp.
69
33 8 ActiveX Data Objects (ADO)
-
7/28/2019 4.2 ASP Basics
69/102
2004 Prentice Hall, Inc. All rights reserved.
33.8 ActiveX Data Objects (ADO)
ADO Enables database connectivity
Part of Universal Data Access architecture
OLE DB provides low-level access
ODBC allows C programs to access databases
ADO allows web applications to access databases
Provides pre-built objects for use in ASP and VBScript
Collections
70
33 8 ActiveX Data Objects (ADO)
-
7/28/2019 4.2 ASP Basics
70/102
2004 Prentice Hall, Inc. All rights reserved.
33.8 ActiveX Data Objects (ADO)
ADO
OLE DB
Legacy dataRelational data sources
Application or Browser
Non-relational data sources
ODBC
Fig. 33.22 Microsofts UDA architecture.
71
33 8 ActiveX Data Objects (ADO)
-
7/28/2019 4.2 ASP Basics
71/102
2004 Prentice Hall, Inc. All rights reserved.
33.8 ActiveX Data Objects (ADO)
Fig. 33.23 Portion of the ADO object model.
Field
Parameter
Fields
Error
Parameters
RecordSet
Errors
Command
Connection
72
33 8 ActiveX Data Objects (ADO)
-
7/28/2019 4.2 ASP Basics
72/102
2004 Prentice Hall, Inc. All rights reserved.
33.8 ActiveX Data Objects (ADO)
Object/Collection DescriptionConnection object Connects to the data source.Command object Contains the query that interacts with the database (the data
source) to manipulate data.
Parameter object Contains information needed by a Command object to querythe data source.
Parameterscollection Contains one or more Parameter objects.Error object Created when an error occurs while accessing data.Errors collection Contains one or more Error objects.Recordset object Contains zero or more records that match the database query.
Collectively, this group of records is called a recordset.
Field object Contains the value (and other attributes) of one data sourcefield.
Fields collection Contains one or more Field objects.Property
object Contains a characteristic of an ADO object.Propertiescollection Contains one or more Property objects.Record object Contains a single row of a Recordset.Stream object Contains a stream of binary data.Fig. 33.24 Some ADO object and collection types.
7333.9 Accessing a Database from an Active
-
7/28/2019 4.2 ASP Basics
73/102
2004 Prentice Hall, Inc. All rights reserved.
g
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
7433.9 Accessing a Database from an Active
-
7/28/2019 4.2 ASP Basics
74/102
2004 Prentice Hall, Inc. All rights reserved.
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 Webpages
Restrict access
Password protection
Query Access database
Outline751
2
3
-
7/28/2019 4.2 ASP Basics
75/102
2004 Prentice Hall, Inc.All rights reserved.
-
7/28/2019 4.2 ASP Basics
76/102
2004 Prentice Hall, Inc.All rights reserved.
g
28
29 errorString = Session( "errorString" )
30 errorString = errorString & "
Error (" _
32 & Err.Number & ") in " & Err.Source & "
" & _33 Err.Description & "
"34 Session( "errorString" ) = errorString
35 End If
36 End Sub
37 %>
database.asp
(2 of 2)
Err object Number property
contains VBScript error
number. Tests if error has
occurred.
variable is assigned
XHTML text containing
error number and messageError number and message
concatenated to variableerrorString
7733.9 Accessing a Database from an Active
-
7/28/2019 4.2 ASP Basics
77/102
2004 Prentice Hall, Inc. All rights reserved.
Server PageFig. 33.26 Error messages sent to login.asp by database.asp.
Outline781
2
3
-
7/28/2019 4.2 ASP Basics
78/102
2004 Prentice Hall, Inc.All rights reserved.
4 ' Fig. 33.27 : login.asp
5 ' ASP document to login to instantpage.asp
6 Option Explicit
7
8 ' create the SQL query
9 Session( "query" ) = "SELECT loginID FROM Users"
10 Call Server.Execute( "database.asp" )
11 %>
12
13
15
16
17
18
19 Login Page
20
21 22 table { text-align:center;
23 font-size:12pt;
24 color:blue;
25 font-size:12pt;
login.asp
(1 of 7)
Assigns SQL query
to session variable
queryExecutes database.asp to retrievelogin IDs from the database
Outline79
26font-family:arial, sans-serif }
27 .error { color:red }
28
-
7/28/2019 4.2 ASP Basics
79/102
2004 Prentice Hall, Inc.All rights reserved.
login.asp
(2 of 7)
29
30
31
32
33
34
35
40
Login attempt failed,
41 please try again
42
-
7/28/2019 4.2 ASP Basics
80/102
2004 Prentice Hall, Inc.All rights reserved.
login.asp
(3 of 7)
51
52
53 Name:
54
55 56
57
58 Select your name
59
60
67
68
69
70
select structure builds
drop-down list ofloginIDs
Requests loginID cookie
Selects the returning users
login ID option
Build loginID options
Outline81
71
72 Password:
73
-
7/28/2019 4.2 ASP Basics
81/102
2004 Prentice Hall, Inc.All rights reserved.
74 name = "password" />
75
76
77
78 79
80
81
82
83
84
85
86
87
92
93
94
login.asp
(4 of 7)
If false, print error
message to user
Outline82
95
-
7/28/2019 4.2 ASP Basics
82/102
2004 Prentice Hall, Inc.
All rights reserved.
login.asp
(5 of 7)
98 Sub BuildReturning()
99 Dim found, loginData
100
101 Set loginData = Session( "loginData" )
102
103 ' pull user names from the record set to populate the
104 ' dropdown list
105 found = False
106
107 While Not loginData.EOF
108 ' create this record's dropdown entry
109 %>
-
7/28/2019 4.2 ASP Basics
83/102
2004 Prentice Hall, Inc.
All rights reserved.
login.asp
(6 of 7)
123 Chr( 34 ) & "selected" & Chr( 34 ) )
124 found = True
125 End If
126 End If
127 %> value = "">128
129
-
7/28/2019 4.2 ASP Basics
84/102
2004 Prentice Hall, Inc.
All rights reserved.
login.asp
(7 of 7)
136 Dim loginData
137
138 Set loginData = Session( "loginData" )
139
140 ' pull user names from the record set to populate the141 ' dropdown list
142 While Not loginData.EOF
143 ' create this record's dropdown entry
144 %>
145
146
while loop (lines 107-130) iterates
through loginDatas records Sets optionvalue to
current loginID
Writes optiondisplay as
current login ID
Increments the record set pointerto next record
8533.9 Accessing a Database from an Active
S P
-
7/28/2019 4.2 ASP Basics
85/102
2004 Prentice Hall, Inc. All rights reserved.
Server PageFig. 33.27 ASP document that allows the user to log in to a site.
8633.9 Accessing a Database from an Active
S P
-
7/28/2019 4.2 ASP Basics
86/102
2004 Prentice Hall, Inc. All rights reserved.
Server PageFig. 33.27 ASP document that allows the user to log in to a site.
Outline87
1
2
3
-
7/28/2019 4.2 ASP Basics
87/102
2004 Prentice Hall, Inc.
All rights reserved.
4 ' ASP document to check user's username and password
5 Option Explicit
6
7 ' test if a user name and a password were
8' entered. If not, transfer back to the login page.
9 If Request( "password" ) = ""Or _
10 Request( "loginID" ) = "noSelection"Then
11 Session( "loginFailure" ) = True
12 Call Server.Transfer( "login.asp" )
13 End If
14
15 Dim connection, loginData16
17 ' create the SQL query
18 Session( "query" ) = _
19 "SELECT * FROM Users WHERE loginID = '" & _
20 Request( "loginID" ) & "'"
21
22 Call Server.Execute( "database.asp" )
23 Set loginData = Session( "loginData" )
24
submitlogin.asp
(1 of 2)
field is empty or id
loginID field contains
default value
WHERE specifies a
condition on which records
are selected
Executes database.asp to
query database
Sets reference loginData to
session variable loginData
(contains records matching
query.)
Checks password against the
password in recordset
If so, variable
loginFailureset to
true, client redirected
back to login.asp
Outline88
25
If Request( "password" ) = loginData( "password" ) Then26
27 ' password is OK, adjust loginFailure
If true line writes forms loginID
Sets session variableSets cookies expiration date to
current date plus 3 days
-
7/28/2019 4.2 ASP Basics
88/102
2004 Prentice Hall, Inc.
All rights reserved.
28 Session( "loginFailure" ) = False
29
30 ' write a cookie to recognize them the next time they
31 ' go to login.asp
32 Response.Cookies( "loginID" ) = Request( "loginID" )33
34 ' give it three days to expire
35 Response.Cookies( "loginID" ).Expires = Date() + 3
36
37 ' send them to instantpage.asp
38 Call Server.Transfer( "instantpage.asp" )
39 Else
40 Session( "loginFailure" ) = True
41 Call Server.Transfer( "login.asp" )
42 End If
43 %>
submitlogin.asp
(2 of 2)
If true, line writes form s loginID
value as cookie named loginIDloginFailurevalue
to False
current date plus 3 days
Calls Server method Transfer to
redirect client to instantpage.asp
Otherwise loginFailure set to True
and client is redirected to login.asp
8933.9 Accessing a Database from an Active
S P
-
7/28/2019 4.2 ASP Basics
89/102
2004 Prentice Hall, Inc. All rights reserved.
Server PageFig. 33.28 ASP document that validates user login.
9033.9 Accessing a Database from an Active
S P
-
7/28/2019 4.2 ASP Basics
90/102
2004 Prentice Hall, Inc. All rights reserved.
Server PageFig. 33.28 ASP document that validates user login.
9133.9 Accessing a Database from an Active
S P
-
7/28/2019 4.2 ASP Basics
91/102
2004 Prentice Hall, Inc. All rights reserved.
Server PageFig. 33.29 Cookies folder before and after cookie creation.
92
33.10 Server-Side ActiveX Components
-
7/28/2019 4.2 ASP Basics
92/102
2004 Prentice Hall, Inc. All rights reserved.
p
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
93
33.10 Server-Side ActiveX Components
-
7/28/2019 4.2 ASP Basics
93/102
2004 Prentice Hall, Inc. All rights reserved.
p
Component name DescriptionMSWC.BrowserType ActiveX component for gathering information about the
clients browser (e.g., type, version).MSWC.AdRotator ActiveX component for rotating advertisements on a
Web page.MSWC.NextLink ActiveX component for linking Web pages together.MSWC.ContentRotator ActiveX component for rotating HTML content on a
Web page.MSWC.PageCounter ActiveX component for storing the number of times a
Web page has been requested.MSWC.Counters ActiveX components that provide general-purpose
persistent counters.MSWC.MyInfo ActiveX component that provides information about a
Web site (e.g., owner name, owner address).Scripting.FileSystemObject
ActiveX component that provides an object library for
accessing files on the server or on the servers network.ActiveX Data Objects (ADO)Data Access Components ActiveX components that provide an object libraryfor accessing databases.Fig. 33.30 Some server-side ActiveX components.
Outline94
1
2
3
-
7/28/2019 4.2 ASP Basics
94/102
2004 Prentice Hall, Inc.
All rights reserved.
4 ' Fig. 33.31 : component.asp
5 ' Demonstrating Server-side ActiveX Components
6 Option Explicit
7 %>
8
9
11
12
13
14
15 ActiveX Component Example
16
17
18
19
20
21Server-side ActiveX Components
22
23
24
component.asp
(1 of 4)
Outline95
25
-
7/28/2019 4.2 ASP Basics
95/102
2004 Prentice Hall, Inc.
All rights reserved.
28 ' create an AdRotator object
29 Set rotator = Server.CreateObject( "MSWC.AdRotator" )
30
31 ' use config.txt to send an advertisement to the client
32 Call Response.Write( _33 rotator.GetAdvertisement( "config.txt" ) )
34
35 ' create a BrowserType object
36 Set browser = Server.CreateObject( "MSWC.BrowserType" )
37
38 If browser.VBScript = True Then
39 %>
40
41 Call Msgbox( "Client browser supports VBScript!" )
42
43
48
49 alert( "Client browser supports JavaScript!" );
50
component.asp
(2 of 4)
Creates AdRotator component instance,
assigns it reference rotator
GetAdvertisement called using referencerotator.
Retrieves advertisements from config.txt
Obtains information about users browser
Check property VBScript value
Test JavaScript property
Outline96
51
-
7/28/2019 4.2 ASP Basics
96/102
2004 Prentice Hall, Inc.
All rights reserved.
54 get client s browser information
55 information = "
Your browser information is:
" & _56 Request.ServerVariables( "HTTP_USER_AGENT" ) & _
57 "
Browser: " & browser.Browser & " Version: " & _58 browser.Version & " Minor version: " & _59 browser.MinorVer & "
Cookies are "60
61 If browser.Cookies Then
62 information = information & "enabled
"63 Else
64 information = information & "disabled
"65 End If
66
67 Call Response.Write( information )
68
69 ' create Page Counter Object
70 Set counter = Server.CreateObject( "MSWC.PageCounter" )
71 Call counter.PageHit() ' page has been "hit"
72 %>
component.asp
(3 of 4)
can obtain similar client information
Tests Cookies property to determine ifbrowser supports cookies
Increments number of hits by one
Passes server variable key
HTTP_USER_AGENT to
ServerVariables, obtains string
containing user information
Outline97
73
74
75
76 This page has been visited
Returns number of hits
-
7/28/2019 4.2 ASP Basics
97/102
2004 Prentice Hall, Inc.
All rights reserved.
component.asp
(4 of 4)
76 This page has been visited
77 times!
78
79
98
33.10 Server-Side ActiveX Components
-
7/28/2019 4.2 ASP Basics
98/102
2004 Prentice Hall, Inc. All rights reserved.
Fig. 33.31 Demonstrating server-side ActiveX components.
9933.10 Server-Side ActiveX Components
-
7/28/2019 4.2 ASP Basics
99/102
2004 Prentice Hall, Inc. All rights reserved.
Fig. 33.31 Demonstrating server-side ActiveX components.
10033.10 Server-Side ActiveX Components
-
7/28/2019 4.2 ASP Basics
100/102
2004 Prentice Hall, Inc. All rights reserved.
Fig. 33.31 Demonstrating server-side ActiveX components.
Outline101
1 REDIRECT redirect.asp
2 width 54
3 height 36
4 border 1
Header contains
REDIRECT file URL
Advertisement dimensions
Image URL, destination URL,
-
7/28/2019 4.2 ASP Basics
101/102
2004 Prentice Hall, Inc.
All rights reserved.
4 border 1
5 *
6 /images/us.gif
7 http://www.odci.gov/cia/publications/factbook/geos/us.html
8 United States Information9 20
10 /images/france.gif
11 http://www.odci.gov/cia/publications/factbook/geos/fr.html
12 France Information
13 20
14 /images/germany.gif
15 http://www.odci.gov/cia/publications/factbook/geos/gm.html
16 Germany Information
17 20
18 /images/italy.gif
19 http://www.odci.gov/cia/publications/factbook/geos/it.html
20 Italy Information
21 20
22 /images/spain.gif
23 http://www.odci.gov/cia/publications/factbook/geos/sp.html
24 Spain Information
25 20
config.txt
(1 of 1)
Asterisk separates header
from advertisementsalt value, image display ratio
Outline102
1
2
3
-
7/28/2019 4.2 ASP Basics
102/102
redirect.asp
(1 of 1)
Fig. 33.33 : redirect.asp
5 ' Redirection Page for AdRotator Component
6 Option Explicit
7
8Call Response.Redirect( Request( "url" ) )
9 %>