Академический Документы
Профессиональный Документы
Культура Документы
.asp file extension Can contain XHTML tags Scripting written with VBScript
JavaScript also used Others (Independent Software Vendors)
Response
Sends inforamtion to client XHTML, text
Server
Access to server methods or properties
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
</head>
<body>
Response.write
Time
<p><strong>A Simple ASP Example</strong></p> <table border = "6"> <tr> <td> <% =FormatDateTime( Now, vbLongDate ) %> </td>
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
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>
11
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.
<!-- 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>
13
Outline
Name.html Program Output
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>
15
Outline
Name.asp
Program Output
16
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
18
19
20
21
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)
22
23
24
25
26
FileSystem FreeSpace IsReady Path RootFolder SerialNumber TotalSize VolumeName Fig. 25.10
27
28
29
30
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.
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
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
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>
hiddenInput
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.
35
Outline
Program Output
36
Outline
Program Output
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
38
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
40
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>
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>
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>
44
Outline
Program Output
45
Outline
Program Output
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
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
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"
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 _
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>"
' 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">
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
<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
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™ Live-Code™ 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
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
54
55
56
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
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" -->
<%
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
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>
%>
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
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
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
63
Outline
Program Output
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.)
If true, line writes forms loginData( "password" ) Then loginID value as cookie named
' 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
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
66
Outline
Program Output
67
68
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)
70
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
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>
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
<%
If browser.JavaScript = True Then %> <script language = "JavaScript">Passes server variable key alert( "Client browser supports JavaScript!" ); to HTTP_USER_AGENT </script> ServerVariables, obtains
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 )
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
<p style = "color: blue; font-size: 12pt"> Increments number of hits %> This page has been visited <% =counter.Hits() by one times!</p> </body> </html>
74
Outline
Program Output
75
Outline
Program Output
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
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" ) ) %>