1 / 102

Chapter 33 - Active Server Pages (ASP)

Chapter 33 - 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)

harlow
Télécharger la présentation

Chapter 33 - Active Server Pages (ASP)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 33 - 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

  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.

  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

  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

  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)

  6. 33.3 Setup • Web Server • Need 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. 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

  8. 33.4 Active Server Page Objects

  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

  10. Scripting delimiter wraps around code executed on the server. Processing directive specifying scripting language Requires programmer explicitly define all variables clock.asp(1 of 2)

  11. Statement is short for: <% Call Response.write( Time()) %> Returns server date and time (Now) as a string formatted in vbLongDate format “<%=“ is short for Response.write clock.asp(2 of 2)

  12. 33.5 Simple ASP Examples Fig. 33.2 Simple Active Server Page.

  13. HTML generated by clock.asp(1 of 2)

  14. HTML generated by clock.asp (2 of 2)

  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

  16. name.html(1 of 2)

  17. action set to ASP file where information is posted. name.html(2 of 2)

  18. 33.5 Simple ASP Examples Fig. 33.4 XHTML document that requests an ASP.

  19. name.asp(1 of 2)

  20. Request object retrieves form data from text field “namebox” name.asp(2 of 2)

  21. 33.5 Simple ASP Examples Fig. 33.5 ASP document that responds to a client request.

  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 and TextStream • 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. 33.6 File System Objects

  24. 33.6 File System Objects

  25. 33.6 File System Objects

  26. 33.6 File System Objects

  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. 33.6 File System Objects

  29. 33.6 File System Objects

  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. 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. 33.6 File System Objects

  33. 33.6 File System Objects • File System Objects (FSOs) • TextStream object (Fig. 33.11) • Manipulate text files

  34. 33.6 File System Objects

  35. 33.6 File System Objects

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

  38. guestbook.asp(1 of 5)

  39. Set is required to establish a variable in VBScript Pass Request method ServerVariables server key APPL_PHYSICAL_PATH Request object retrieves hiddenInput value and tests it against string “true” Prints string followed by users name input in the form Date() assigns current server date to beginning ofmailtoUrl Displays a mailto: link. Concatenated with file nameguestbook.txt Submitted name and email are combined and assigned to string mailtoUrl Creating a FSO instance assigned to referencefileObject Pass value 34 into VBScript Chr function to produce double quotes (“”) Request retrieves “email” and “username” guestbook.asp(2 of 5)

  40. Form post action to .asp page Method OpenTextFile retrieves TextStream object for accessing file guestbook.txt Contstant 8 indicates append mode (writing to the end of the file.) TextStream method WriteLine writes toguestbook.txt Close() method closes the file guestbook.asp(3 of 5)

  41. Form contains two text fields and text area for user input Passes parameter hiddenInput value “true” guestbook.asp(4 of 5)

  42. FSO object FileExists checks if guestbook.txt exists. TextStream and ReadAll methods read entire contents of guestbook.txt Read entries from guestbook.txt and write XHTML to the client Response.Write writes text to client. Contains XHTML markup rendered on client browser. guestbook.asp(5 of 5)

  43. 33.6 File System Objects Fig. 33.12 Guestbook Active Server Page.

  44. 33.6 File System Objects Fig. 33.12 Guestbook Active Server Page.

  45. XHTML generated by guestbook.asp(1 of 1)

  46. 33.6 File System Objects

  47. 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. 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. 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 • <!--#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 <% %> delimiters

  50. instantpage.asp(1 of 4)

More Related