1. Trang chủ
  2. » Công Nghệ Thông Tin

active server pages (asp)

102 438 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 102
Dung lượng 2,15 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

All rights reserved.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

Trang 1

 2003 Prentice Hall, Inc All rights reserved.

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

Trang 2

• 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 side scripting.

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

Trang 3

 2003 Prentice Hall, Inc All rights reserved.

• 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

Trang 4

• 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

Trang 5

 2003 Prentice Hall, Inc All rights reserved.

• Active Server Pages,cont.

– Communication with Server, cont.

• ASP document is loaded into memory

• Parses (top to bottom)

Trang 6

• 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

Trang 7

 2003 Prentice Hall, Inc All rights reserved.

• 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)

Trang 8

Object Name Description

Fig 25.1 Commonly used ASP objects

Trang 9

 2003 Prentice Hall, Inc All rights reserved.

• 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

Trang 10

5 ' A simple ASP example

20 strong { font-family: arial, sans-serif;

21 font-size: 14pt; color: blue }

Processing directive specifying scripting language Requires programmer explicitly define all variables

Trang 11

 2003 Prentice Hall, Inc All rights reserved.

vbLongDate format

Statement is short for:

<% Call Response.write( Time()) %>

Trang 12

Fig 33.2 Simple Active Server Page.

Trang 13

 2003 Prentice Hall, Inc All rights reserved.

HTML generated by clock.asp

12 strong { font-family: arial, sans-serif;

13 font-size: 14pt; color: blue }

Trang 14

HTML generated by clock.asp

Trang 15

 2003 Prentice Hall, Inc All rights reserved.

• 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

Trang 16

5 <! XHTML document that request an ASP document >

15 <p style = "font-family: arial, sans-serif">

16 Enter your name:

17 </p>

18

name.html (1 of 2)

Trang 17

 2003 Prentice Hall, Inc All rights reserved.

Trang 18

Fig 33.4 XHTML document that requests an ASP.

Trang 19

 2003 Prentice Hall, Inc All rights reserved.

5 ' Another simple ASP example

17 <style type = "text/css">

18 p { font-family: arial, sans-serif;

19 font-size: 14pt; color: navy }

20 special { font-size: 20pt; color: green }

21 </style>

22 </head>

name.asp (1 of 2)

Trang 20

Request object retrieves form data from text field

“ namebox ”

Trang 21

 2003 Prentice Hall, Inc All rights reserved.

Fig 33.5 ASP document that responds to a client request.

Trang 22

• 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

• FileSystemObject methods (Fig 33.7)

Trang 23

 2003 Prentice Hall, Inc All rights reserved.

Object type Description

Drives

disks, RAM disks—computer memory used as a substitute for hard disks to allow high-speed file operations, CD-ROMs, etc.) Drives can be local or remote

Fig 33.6 File System Objects (FSOs)

Trang 24

Methods Description

Fig 33.7 FileSystemObject methods (Part 1 of 2)

Trang 25

 2003 Prentice Hall, Inc All rights reserved.

Methods Description

Fig 33.7 FileSystemObject methods (Part 2 of 2)

Trang 26

Property/method Description

Property

Method

Fig 33.8 Some common File properties and methods

Trang 27

 2003 Prentice Hall, Inc All rights reserved.

• File System Objects (FSOs)

Trang 28

Property/method Description

Properties

Fig 33.9 Some Folder properties and methods (Part 1 of 2)

Trang 29

 2003 Prentice Hall, Inc All rights reserved.

Trang 30

• File System Objects (FSOs)

Trang 31

 2003 Prentice Hall, Inc All rights reserved.

• File System Objects (FSOs)

– Drive object (Fig 33.10)

• Gather information about drives

Trang 32

Property Description

Remote , CDRom and RamDisk represent Drive types and have the values 0–5, respectively

Fig 33.10 Drive properties

Trang 33

 2003 Prentice Hall, Inc All rights reserved.

• File System Objects (FSOs)

– TextStream object (Fig 33.11)

• Manipulate text files

Trang 34

Property/Method Description

Properties

Methods

Read String Returns a specified number of characters from the file

referenced by the TextStream object

ReadAll String Returns the entire contents of the file referenced by the

TextStream object

Fig 33.11 TextStream methods and properties (Part 1 of 2)

Trang 35

 2003 Prentice Hall, Inc All rights reserved.

Property/Method Description

Methods, cont

TextStream object

TextStream object

object

file referenced by the TextStream object

referenced by the TextStream object

Fig 33.11 TextStream methods and properties (Part 2 of 2)

Trang 37

 2003 Prentice Hall, Inc All rights reserved.

• Creating a guestbook, cont.

• Request object

– Chr function

• Generate characters from ASCII code

Trang 38

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>

23 </head>

24 <body>

guestbook.asp (1 of 5)

Trang 39

 2003 Prentice Hall, Inc All rights reserved.

37 ' Check if this request is after the user has posted the form

38 If Request( "hiddenInput" ) = "true" Then

39

40 ' Print a thank you

41 Call Response.Write( "Thanks for your entry, " & _

42 Request( "username" ) & "!" )

43 %>

44 <hr />

45 <%

46 ' build the mailtoUrl

47 mailtoUrl = Date() & " <a href = " & Chr( 34 ) _

48 & "mailto:" & Request( "email" ) & Chr( 34 ) _

49 & ">" & Request( "username" ) & "</a>: "

guestbook.asp (2 of 5)

Creating a FSO instance assigned to reference

fileObject

variable in VBScript

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 of

mailtoUrl

Displays a mailto : link

Submitted name and email are combined and assigned to string mailtoUrl

Pass value 34 into VBScript

Chr function to produce double quotes ( “” )

Request retrieves “ email ” and

“ username ”

Pass Request method

ServerVariables server key

APPL_PHYSICAL_PATH

Trang 40

54 Set textFile = _

55 fileObject.OpenTextFile( guestbook, 8, True )

56

57 ' write data to guestbook.txt

58 Call textFile.WriteLine( "<hr />" & mailtoUrl & _

66 <! write form to the client >

67 <form action = "guestbook.asp" method = "post">

68 <table>

69 <tr>

70 <td>Your Name: </td>

71 <td><input class = "font"

72 type = "text" size = "60"

73 name = "username" /></td>

74 </tr>

guestbook.asp (3 of 5)

TextStream method

WriteLine writes to

guestbook.txt Close() method closes the file

Form post action to asp page

Trang 41

 2003 Prentice Hall, Inc All rights reserved.

guestbook.asp (4 of 5)

79 type = "text" size = "60"

86 <td>Tell the world: </td>

87 <td><textarea name = "comment" rows = "3"

88 cols = "50">

89 Replace this text with the information

90 you would like to post.</textarea></td>

91 </tr>

92 </table>

93

94 <input type = "submit" value = "submit" />

95 <input type = "reset" value = "clear" />

96 <input type = "hidden" name = "hiddenInput"

Passes parameter

hiddenInput

value “ true ”

Trang 42

104

105 ' open the guestbook, "1" is for reading

106 Set textFile = fileObject.OpenTextFile( guestbook, 1 )

checks if guestbook.txt

exists.

Read entries from

guestbook.txt and write XHTML to the client

TextStream and ReadAll

methods read entire contents of

guestbook.txt Response.Write writes text to client Contains XHTML markup rendered

on client browser.

Trang 43

 2003 Prentice Hall, Inc All rights reserved.

Fig 33.12 Guestbook Active Server Page.

Trang 44

Fig 33.12 Guestbook Active Server Page.

Trang 45

 2003 Prentice Hall, Inc All rights reserved.

XHTML generated by guestbook.asp

(1 of 1)

Trang 46

Key name Description

APPL_PHYSICAL_PATH Returns the physical path

came in through SSL (Secure Sockets Layer)

REQUEST_METHOD Request method (i.e., get and post)

HTTP_USER_AGENT Returns information about the client making

the request

HTTP_COOKIE Returns cookies residing on the client

Fig 33.13 Some server variable keys

Trang 47

 2003 Prentice Hall, Inc All rights reserved.

• 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

Trang 48

– Stores message in session variable welcomeBack

• Every time user submits the form

Trang 49

 2003 Prentice Hall, Inc All rights reserved.

• Server-side include

– Commands embedded in XHTML documents – Add dynamic content

– 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

Trang 50

5 ' ASP document that posts data to process.asp

17 <style type = "text/css">

18 table { text-align: center;

Trang 51

 2003 Prentice Hall, Inc All rights reserved.

30 <! #include virtual = "/includes/header.shtml" >

31 <h2>Instant Page Content Builder</h2>

32

33 <% ' if process.asp posted an error, print the error

34 ' message

35 If Session( "errormessage" ) <> "no error" Then

36 Call Response.Write( Session( "errorMessage" ) )

37 ' otherwise, print the welcome back message, if any

43 <! a form to get the information from the user >

44 <form action = "process.asp" method = "post">

value is written to client

Else, WelcomeBack value is written to the client

First time page is executed, line 35 returns true

errorMessage never has

a value unless errors are encountered

Session object retrieves variable value

Requests process.asp when form is posted

Trang 53

 2003 Prentice Hall, Inc All rights reserved.

77 </table>

78

79 <input type = "submit" value = "submit" />

80 <input type = "reset" value = "clear" />

Server-side include

Trang 54

Fig 33.15 ASP that posts user information to process.asp.

Trang 55

 2003 Prentice Hall, Inc All rights reserved.

Fig 33.15 ASP that posts user information to process.asp.

Trang 56

Fig 33.15 ASP that posts user information to process.asp.

Trang 57

 2003 Prentice Hall, Inc All rights reserved.

header.shtml (1 of 1)

5 <hr style = "color: blue" />

Trang 58

server-side include file for the document footer

5 href = "mailto:orders">ordering information</a> -

6 <a style = "text-align: center"

7 href = "mailto:editor">contact the editor</a><br />

8 <hr style = "color: blue" />

Trang 59

 2003 Prentice Hall, Inc All rights reserved.

5 ' ASP document that creates user's ASP document

12 q = Chr( 34 ) ' assign quote character to q

13 Session( "errorMessage" ) = "no error"

14

15 ' check to make sure that they have entered a

16 ' valid filename

17 If ( LCase( Request( "filename" ) ) = "yourfilename" ) _

18 Or Request( "filename" ) = "" Then

19 message = "<p style = " & q & "color: red" & q & _

20 ">" & "Please enter a valid name or filename.</p>"

21 Session( "errorMessage" ) = message

22 Call Server.Transfer( "instantpage.asp" )

23 End If

24

25 Dim directoryPath, filePath, fileObject, fileName

process.asp (1 of 6)

If statement validates contents of text field.

If field is empty or contains default string

yourfilename , lines 19-21 assign XHTML error message to variable message.

Assign message value to session variable

errorMessage

Server method

Transfer requests

instantpage.asp

Trang 60

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 = "<p style = " & q & "color: red" & q & _

42 ">" & "The file name is in use.<br />" & _

43 "Please use a different file name.</p>"

44 Session( "errorMessage" ) = message

45 Call Server.Transfer( "instantpage.asp" )

46 End If

47

process.asp (2 of 6)

Specify server path where ASP file is written

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

requests instantpage.asp

Ngày đăng: 24/10/2014, 12:07

TỪ KHÓA LIÊN QUAN

w