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

Tài liệu Module 5: Using Server- Side Scripts pdf

52 443 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

Tiêu đề Using Server-Side Scripts
Tác giả Sangeeta Nair, Vijayalakshmi Narayanaswamy, Veena Nambier, Yatinder Walia
Người hướng dẫn Reid Bannecker
Trường học NIIT
Thể loại module
Năm xuất bản 2000
Thành phố Not Specified
Định dạng
Số trang 52
Dung lượng 920,03 KB

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

Nội dung

Contents Overview 1 Introduction to Active Server Pages ASPs 2 Object Oriented Programming OOP Concepts 6 Lab 5: Processing Form Data Using... Object Oriented Programming OOP Concept

Trang 1

Contents

Overview 1

Introduction to Active Server Pages (ASPs) 2

Object Oriented Programming (OOP)

Concepts 6

Lab 5: Processing Form Data Using

Trang 2

Server-with all applicable copyright laws is the responsibility of the user No part of this document may

be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Microsoft Corporation If, however, your only means of access is electronic, permission to print one copy is hereby granted

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property

 2000 Microsoft Corporation All rights reserved

Microsoft, ActiveX, BackOffice, FrontPage 2000, Internet Information Server, InterDev, JScript, MS-DOS, Notepad, PowerPoint, Visual Basic, Windows and Windows NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries

The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted

Other product and company names mentioned herein may be the trademarks of their respective owners

Program Manager: Steve Merrill

Development Lead: Basabjit Chakrabarty (NIIT)

Instructional Designers: Sangeeta Nair, Vijayalakshmi Narayanaswamy (NIIT); Veena Nambier,

Yatinder Walia (NIIT)

Technical Contributors: Scott Swigart (3 Leaf Solutions); Gary Gumbiner (Great Barrier

Technologies, Inc.)

Graphic Artist: Scott Serna (Creative Assets)

Editing Manager: Jennifer Linn

Editor: Reid Bannecker

Production Manager: Miracle Davis

Production Coordinator: Linda Lu Cannon (The Write Stuff)

Build Coordinator: Eric Wagoner

Testing Lead: Eric Meyers

Testing: Bryan Urakawa, Chris and Edward

Lead Product Manager, Internet Services: Hilary Vandal

Manufacturing Manager: Rick Terek

Operations Coordinator: John Williams

Manufacturing Support: Laura King; Kathy Hershey

Lead Product Manager, Release Management: Bo Galford

Group Manager, Courseware Infrastructure: David Bramble

General Manager: Robert Stewart

Trang 3

Instructor Notes

This module provides students with an overview of server-side scripts, such as Active Server Pages (ASPs) It also explains the concept of object oriented programming Students will also learn about the ASP object model and will

learn to write programs using ASP

At the end of this module, students will be able to:

! Execute ASP code in Microsoft IIS

! Use ASP objects to capture form information and handle server functions

! Use ASP to process form information

! Write server-side scripts using ASP

Materials and Preparation

This section provides you with the required materials and preparation tasks that are needed to teach this module

Required Materials

To teach this module, you need the following materials:

! Microsoft PowerPoint file 1912A_05.ppt

! Module 5, “Using Server-Side Scripts”

! Lab 5, “Processing Form Data Using Server-Side Scripts”

Preparation Tasks

To prepare for this module, you should:

! Read all of the materials for this module

! Complete the practices and lab in this module

Presentation:

165 Minutes

Lab:

60 Minutes

Trang 4

Module Strategy

Use the following strategy to present this module:

! Introduction to Active Server Pages (ASPs) First, explain the working of ASPs Then, explain how to execute an ASP file using the Microsoft Internet Information Server (IIS)

! Object Oriented Programming (OOP) Concepts First, demonstrate and explain the sample code in Microsoft Internet Information Server Then, based on the demonstration, explain the concept

of properties, and methods

! ASP Objects

First, explain the Response object with the help of the example provided Then, explain the Request object with the help of the example provided Similarly, explain the Application, Session, and Server objects with the

help of the examples provided Also, ask the students to do practices based

on the various ASP objects

! Programming in ASP This section introduces the concept of programming in context of server-side scripts Explain variables, data types, conditional statements, procedures, operators, and string functions with the help of the examples provided Finally, ask the students to do practice based on the key concepts taught in this section

Trang 5

# Overview

! Introduction to Active Server Pages (ASPs)

! Object Oriented Programming (OOP) Concepts

! Using ASP Objects

! Lab 5: Processing Form Data Using Server-Side Scripts

! Programming in ASP

! Review

(View Userinfo.htm from Sampapps)

Web pages created with HTML are static in nature However, you can make your Web pages dynamic and interactive by using server-side scripts, such as Active Server Pages (ASPs) You can use ASP in your Web pages to process form information, respond to queries, and work with Microsoft ActiveX®controls

After completing this module, you will be able to:

! Define the ASP object model

! Define object oriented programming

! Execute ASP code in Microsoft IIS

! Use ASP objects to capture form information and handle server functions

! Use ASP to process form information

The sample application files for this module are located in the folder

In this module, you will learn

about server-side scripts,

such as Active Server

Pages You will also learn

the ASP object model

Delivery Tip

From Internet Explorer,

open Userinfo.htm from the

address

http://localhost/1912/Samp

apps/Ch05/Userinfo.htm

Show students what

happens when the form

submitted without filling the

name and the e-mail

address Ask students how

the process running in the

background knows that the

user has not specified the

name or the e-mail address

in the Employee

Registration Form Explain

that server-side scripts

provide this functionality

Tell students that at the end

of the module, they will be

able to create a similar

application using ASP

Note

Trang 6

# Introduction to Active Server Pages (ASPs)

! Working of ASPs

! Microsoft Internet Information Server (IIS)

! ASP Object Model

A server-side script, such as ASP, runs on a Web server and returns an HTML page to the user

ASP is an open, compile-free application environment in which you can combine HTML, scripts, and reusable ActiveX server components to create dynamic and powerful Web applications ASPs enable server-side scripting for IIS with native support for both Microsoft Visual Basic® Scripting Edition (VBScript) and JavaScript For example, with ASP, you can build Web sites that advertise your company products or you can build information related Web sites that provide up-to-date news to users Also, ASPs are used to retrieve and store form information in a text file or a database

Trang 7

Working of ASPs

! When a browser requests an ASP file from a Web server:

commands contained in the file, and generates an HTML Web page

When a browser requests an ASP file from a Web server:

! The server-side script is executed

! ASP processes the requested file, executes the script commands contained in the file, and generates a Web page

! The Web page is sent back to the browser

As the ASP script runs on the Web server, the Web server does all of the processing The Web server generates and sends back HTML-based pages to the browser

You can use Notepad to create ASP scripts Microsoft also provides a development tool called Microsoft Visual InterDev® to work with ASP scripts

Slide Objective

To explain how ASPs work

Lead-in

ASP scripts run on the Web

server The Web server

sends back HTML pages to

the browser

Tip

Trang 8

Microsoft Internet Information Server (IIS)

! IIS

IIS is built into the Microsoft Windows NT® Server operating system IIS uses the Windows NT User Manager to maintain users and groups, which saves you the trouble of maintaining multiple sets of network and Web site users IIS also utilizes the Windows NT Event Viewer and Performance Monitor to view such items as bytes sent per second and current CGI requests

In the Windows® 2000 environment, you need to use IIS to run ASP code There is a difference between previewing ASP code in Internet Explorer and previewing a normal HTML code in Internet Explorer When you preview ASP code in Internet Explorer, the Address bar of the browser window displays a

path that looks like http://localhost/… This path indicates that the ASP code is

run on IIS and not executed by the browser The term "localhost" maps to the folder \Inetpub\wwwroot The file that contains the ASP code must reside under the folder \Inetpub\wwwroot

However, when you preview normal HTML code in Internet Explorer, the path

in the Address bar looks like c:/…, which indicates that the HTML file is

executed by the browser itself

IIS comes with Active

Server Pages (ASPs) with

which you can build

dynamic Web pages

Trang 9

ASP Object Model

Object Description Response Used to generate the response that is sent back to the browser

Request Contains information sent by the browser to the Web server

Session Contains user specific information

Application Contains global information for Web applications

Server Provides common functionalities and access to COM objects

Slide Objective

To provide an overview of

the ASP object model

Lead-in

IIS implements ASP as an

OLE automation server that

has a hierarchical object

framework, called the ASP

object model

Trang 10

# Object Oriented Programming (OOP) Concepts

! Properties

! Methods

(View survey.htm and Object.htm from Sampapps)

While browsing the Web, you may have noticed pages containing various types

of control elements, such as push buttons, text boxes, radio buttons, and check boxes These controls are called objects

Object-oriented programming enables you to write code that is associated with specific objects An object is a piece of code that combines data and processes that operate on the data An object performs a set of activities that define its behavior

Objects can contain other objects To use an object, you need to use its properties, methods, or events

Consider the following code that uses the Request and Response objects

From Internet Explorer,

open Survey.htm from the

address

http://localhost/1912/Samp

apps/Ch05/Survey.htm

Show students what

happens when the form is

filled in and submitted

papps\Ch05 Briefly explain

the use of methods and

properties of the Request

and the Response object

with the help of the source

code

Trang 11

Properties

! Properties:

object

<% Response.Buffer = True %>

Properties refer to an object's data, attributes, and settings Properties do not take arguments They are usually used to describe or set the state and appearance of an object

You can set properties of an object when you add the object to a Web page at design time You can also set the properties of an object at run time

Consider the following code:

<% Response.Buffer = True %>

The Buffer property of the Response object causes the server to do all the

processing for a Web page before data is send to the browser

Trang 12

Methods

! Methods:

In the code above, the Write method of the Response object is used to write a

string variable to the current page

Slide Objective

To provide an overview of

methods

Lead-in

An object contains data

members The only way to

access the data members of

an object is by using its

methods

Trang 13

# Using ASP Objects

! Practice: Using the Session and Application Objects

Any Web-based application involves a relationship between a client and a

server through a browser interface The Application, Session, and Server

objects in the ASP object model handle the server functions, while the

Response and Request objects in the model handle the interaction with the

client through the browser interface A group of additional scripting objects is also available, which primarily handles file operations on the server

Slide Objective

To explain the role of the

predefined objects in the

ASP object model

Lead-in

ASP is based on a core

group of predefined objects

These objects handle all

aspects of a Web-based

application

Trang 14

Response Object

! Used to send data back to the client Web browser

<% Response.Write(“Hello World”)%>

The Response object is used to send data back to the client Web browser The

Write method of the Response object can be used to insert a string into the

current HTML output that is sent to the browser The following example code

shows the use of the Write method:

The Response object sends

data to the client Web

browser The Response

object sends data to the

client Web browser

Trang 15

Practice: Using the Response Object

In this exercise, you will first create a Web in Microsoft FrontPage® 2000 Then, you will create an ASP file, response.asp, which sends information back

to the browser For example, you can create response.asp such that it sends the

text Hello, Welcome to the world of ASP back to the browser using the

response object

! Create a Web in FrontPage 2000

1 Open FrontPage 2000

2 Create a one-page Web under http://localhost/1912/Practices/Mod05web1

! Create an ASP file

1 Switch to the HTML pane of the page

2 Within the <BODY> </BODY> tags, type the ASP code to send information back to the browser

Your code should resemble the following:

<%

response.write ("Hello! Welcome to the world of ASP.")

%>

! View the ASP file in the browser

1 Save the ASP file as Response.asp

2 To view the file in the browser, click the Preview in Browser icon on the

In this practice, you will use

the response object to send

data back to the browser

Delivery Tip

Allow the students send

back any information that

they want using the

Response object

Tip

Trang 16

<% first=Request.QueryString(“txtfname”)last =Request.QueryString(“txtlname”)Response.Write(“Welcome” & first & last)%>

<%first=Request.Form(“txtfname”)last =Request.Form(“txtlname”)Response.Write(“Welcome” & first & last)%>

<%first=Request.Form(“txtfname”)last =Request.Form(“txtlname”)Response.Write(“Welcome” & first & last)%>

The Request object is used to capture information in response to a request

made by a user There are two ways in which a browser can send information to

a Web server The information can be appended at the end of the URL or it can come from the <FORM> section on a page

The Request object provides the QueryString and the Form collections to

retrieve information from the form controls

The following example consists of two files, Query.htm and Details.asp

Query.htm creates a form with two text boxes, First name and Last name, and

a Submit button The form is submitted by using the Get method The code for

First name: <INPUT TYPE=”text” NAME="txtfname">

Last name: <INPUT TYPE=”text” NAME="txtlname">

<INPUT TYPE=”submit” NAME="btnsubmit">

The Request object

captures the request made

by the user

Delivery Tip

From Internet Explorer,

open Query.htm from the

address

http://localhost/1912/Samp

apps/Ch05/Query.htm and

show the results to the

students Also, from

Notepad or FrontPage, open

Details.asp from the folder

\Inetpub\wwwroot\1912\Sam

papps\Ch05 Use the source

code to explain the use of

Querystring collection

Trang 17

The file Details.asp uses the QueryString collection of the Request object to

retrieve the form input The code for Details.asp is as follows:

<HTML>

<BODY>

<%

first=Request.QueryString("txtfname") last=Request.QueryString("txtlname") Response.Write (" Welcome " & first & " " & last & "!") %>

</BODY>

</HTML>

When a form is submitted by using the Get method as the Method attribute, the

values in the text boxes are put into a query string that is appended to the URL The complete URL is visible in the browser address box The URL of the above code appears as:

http://<server-name>/details.asp?txtfname=Richard&txtlname=Hanks where Richard is the value entered as the first name field, and Hanks is the value entered as the last name field

The Form collection of the Request object can also be used to retrieve the value from the txtfname and txtlname controls, if the form is submitted using the Post method:

<FORM NAME="frmdetails" ACTION="details1.asp" METHOD="Post"> First name: <INPUT TYPE="text" NAME="txtfname">

Last name: <INPUT TYPE="text" NAME="txtlname" >

<INPUT TYPE="submit" NAME="btnsubmit">

</FORM>

The information is sent to the Web server along with the http headers The

following code uses the Form collection to retrieve form input:

<HTML>

<BODY>

<% first=Request.Form("txtfname") last=Request.Form("txtlname") Response.Write (" Welcome " & " " & first & last ) %>

</BODY>

</HTML>

Delivery Tip

Use the code on the slide to

explain the Form collection

Trang 18

Practice: Using the Request Object

In this exercise, you will use the Request object to access the data submitted by

a form You will create a simple form that accepts user details such as first name, last name, and e-mail address When the user fills in the details and submits the form, an ASP script running on the server should access the form data and send a message displaying the first name, last name, and e-mail address back to the browser as an acknowledgement To do so, you will create

an ASP file that processes the data submitted by the form

! Create a Web in FrontPage 2000

1 Open FrontPage 2000

2 Create a one-page Web under http://localhost/1912/Practices/Mod05web2

! Create a form

1 Add a form to the new page that is open On the Insert menu, point to

Form, and then click Form FrontPage inserts a form on the page, which

contains a Submit button and a Reset button

2 Add three text boxes to the form: First Name, Last Name, and E-mail To

create a text box and set its properties:

a On the Insert menu, point to Form, and click One-Line Text Box

b Double-click a text box to open its properties dialog box The Text Box

Properties dialog box appears Change the name of the text box to a

name that is suitable to the context

For example, name the first name, last name, and e-mail text boxes

fname, lname, and email, respectively

Slide Objective

To introduce the practice

Lead-in

In this practice, you will use

the Request object to

access the data submitted

by a form

Trang 19

3 Give each of the text boxes suitable labels such as First Name, Last Name, and E-mail

Your form should look like the following illustration:

4 To set the ACTION attribute of the form to an ASP file, right-click anywhere inside the form, and then click Form Properties on the short-cut menu The Form Properties dialog box appears To specify that the form

details are to be submitted to an ASP file named Request.asp:

a Select the Send to other radio button

b Click Options, type Request.asp in the Action field, and then click OK

5 Save the page as Form.htm

! Create an ASP file to process the form

1 Create a new page in the current Web by clicking the New Page icon on the

toolbar

2 Switch to the HTML pane of the new page

3 Type in the ASP code for accessing the user data submitted by the form, and send a page back to the browser that displays the user information as an acknowledgement

Your code should resemble the following:

<%

fname = request.form ("fname") lname = request.form ("lname") email = request.form ("email")

%>

<P><%response.write "Welcome "&fname&" "&lname%></P>

<P><%response.write "Your e-mail address is: "&email%></P>

4 Save the ASP file as Request.asp

! Test the ASP file for functionality

1 Open Form.htm

2 View the file in a browser, by clicking Preview in Browser

3 Test the ASP file Request.asp, by filling in the form and clicking Submit

Check to see if the results are displayed as expected

Trang 20

Session Object

! Session Object

(View session.asp from Sampapps)

Session variable remains in the memory of the server throughout the time a user visits the Web site A user cannot read or modify the Session variables created for another user

You can create and assign values to a Session variable by using the Session

object:

<%Session ("UserName") = "John Smith"%>

After creating a Session variable, you can use the Response object to display its

value:

<%Response.Write Session ("UserName")%>

The Abandon method can be used to explicitly destroy a Session variable When the Abandon method is called, the current Session object is deleted as

soon as the script commands on the current page have been processed

Slide Objective

To explain the Session

object

Lead-in

The Session object stores

information required for a

particular session

Trang 21

The following example consists of two files, Session.asp and Connect1.asp The first file, Session.asp, assigns a value to a session variable and then the variable

is destroyed using the Abandon method When the hyperlink is clicked, the file Connect1.asp does not display the value of the session variable, as the variable

is destroyed The code for Session.asp is as follows:

ELSE session("varGreeting")="Hello!"

END IF session.abandon %>

The Session variable varGreeting is assigned the value: <%=session("varGreeting")%>

<P><A HREF="Connect1.asp"><B>Click Here</B></A>

From Internet Explorer,

open Session.asp from the

address

http://localhost/1912/Samp

apps/Ch05/Session.asp

Show the results to the

students Also, from

Notepad or FrontPage, open

Session.asp from the folder

\Inetpub\wwwroot\1912\Sam

papps\Ch05 Use the source

code to explain the Session

variable

Delivery Tip

Show students how the

Session variable works after

removing the code

“session.abandon” from

Session.asp

Trang 22

Application Object

! Application Object

(View application.asp and lock.asp from Sampapps)

<% Application.LockApplication("Visit")=Application("Visit")+1 Application.Unlock %>

The Application object stores state information for use by script files and can

also be used to share information among all users of a given application

Script files can be defined as all the asp files in a virtual directory and its subdirectories

You can use Application variables to store global information Different users can modify application variables simultaneously You can create and assign

values to an Application variable by using the Application object:

<%Application ("CompanyName") = "Microsoft"%>

After creating an Application variable, you can use the Response object to

display its value:

<%Response.Write Application("CompanyName")%>

The following example consists of two files, Application.asp and Connect.asp The first file, Application.asp, assigns a value to an Application variable When the hyperlink is clicked, the file Connect.asp displays the value of the

Application variable as the Application variable retains the value

Slide Objective

To explain the Application

object

Lead-in

The Application object

stores information for use by

script files

Note

Trang 23

The code for Application.asp is as follows:

ELSE application("varWish")="Hello !"

END IF %>

The Application variable varWish is assigned the value: <%=Application("varWish" )%>

<P><A HREF="Connect.asp"><B>Click Here</B></A>

</BODY>

</HTML>

The file Connect.asp displays the value of the application variable created in

Application.asp The code for Connect.asp is as follows:

modifying the Application object The Unlock method allows other users to modify the Application object properties

Consider an example file Lock.asp, where you use the Lock and UnLock methods of the Application object The Web page that appears displays the

number of times a user has visited the page Each time a user visits the page, the

variable visit is incremented by one The Lock method prevents a user from

modifying the file while another user is visiting it

Delivery Tip

From Internet Explorer,

open Application.asp from

the address

http://localhost/1912/Samp

apps/Ch05/Application.as

p and show the results to

the students Also, from

Notepad or FrontPage, open

Application.asp from the

folder

\Inetpub\wwwroot\1912\Sam

papps\Ch05 Use the source

code to explain the

Application variable

Trang 24

The code for Lock.asp is as follows:

From Internet Explorer,

open Lock.asp from the

address

http://localhost/1912/Samp

apps/Ch05/Lock.asp and

show the results to the

students Also, from

Notepad or FrontPage, open

Lock.asp from the folder

\Inetpub\wwwroot\1912\Sam

papps\Ch05 Use the source

code to explain the Lock

and Unlock methods of the

Application object

Trang 25

Practice: Using the Session and Application Objects

In this exercise, you will create a form, and two ASP files that use Session and

Application objects to share data submitted by the form and between the ASP

files The name of the user is accepted in a form, and it is submitted to an ASP file This ASP file in turn, generates a welcome message for the user The ASP file also contains a link to another ASP page The linked ASP page displays another welcome message to the user

! Create a Web in FrontPage 2000

1 Open FrontPage 2000

2 Create a one-page Web under http://localhost/1912/Practices/Mod05web3

! Create a form

1 Add a form to the new page that is open On the Insert menu, point to

Form, and then click Form FrontPage inserts a form on the page, which

contains a Submit button and a Reset button

2 In the form, add a text box for accepting the name of the user To create a text box and set its properties:

a On the Insert menu, point to Form, and click One-Line Text Box

b Double-click the text box to open its properties dialog box The Text

Box Properties dialog box appears Change the name of the text box to

a name that is suitable to the context such as UserName

3 Give the text box a suitable label such as Name

Slide Objective

To introduce the practice

Lead-in

In this practice, you will use

the session and application

objects to handle server

functions

Delivery Tip

Allow students to use any

welcome message they

want in their code

Trang 26

4 To set the ACTION attribute of the form to an ASP file, right-click anywhere inside the form, click Form Properties The Form Properties

dialog box appears To specify that the form details are to be submitted to an ASP file named Formproc.asp:

a Select the Send to other radio button

b Click Options, type Formproc.asp in the Action field, and then click

OK

5 Save the file as Sessapp.htm

! Create the ASP file that processes the form

1 Create a new page in the current Web by clicking the New Page icon on the

toolbar

2 Switch to the HTML pane of the new page

3 Type in the ASP code to send a welcome message Hello! followed by the

name of the user For example, if the name of the user is Susan, use the

Response object to display the text Hello! Susan to the user

Hello! <% response.write request.form("UserName") %>

4 To share the name of the user with another ASP file, create a Session

variable named UserName, and assign the name of the user to it

5 To share the name of the company with another ASP file, create an

Application variable named CompanyName, and assign the value

“Microsoft” to it

<%

session("UserName") = request.form("UserName") application("CompanyName") = "Microsoft"

%>

6 Add a link to another page, Another.asp, and specify the text to use for the

link as Another Page

Your code should resemble the following:

Hello! <% response.write request.form("UserName") %>

<%

session("UserName") = request.form("UserName") application("CompanyName") = "Microsoft"

%>

<P><A HREF="Another.asp">Another Page</A></P>

7 Save the file as Formproc.asp

Ngày đăng: 11/12/2013, 14:15

TỪ KHÓA LIÊN QUAN