Contents
Overview 1
Introduction to Active Server Pages (ASPs) 2
Object Oriented Programming (OOP)
Concepts 6
Using ASP Objects 9
Lab 5: Processing Form Data Using Server-
Side Scripts 24
Programming in ASP 28
Review 48
Module 5: Using Server-
Side Scripts
Information in this document is subject to change without notice. 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. Complying
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
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
Module 5: Using Server-Side Scripts iii 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:
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.
Module 5: Using Server-Side Scripts 1 #
##
#
Overview
!
Introduction to Active Server Pages (ASPs)
!
Object Oriented Programming (OOP) Concepts
!
The sample application files for this module are located in the folder
\Inetpub\wwwroot\1912\Sampapps\Ch05.
Slide Objective
To provide an overview of
the module topics and
objectives.
Lead-in
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 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.
Slide Objective
To provide an overview of
Active Server Pages
(ASPs).
Lead-in
ASPs are used to write
server-side scripts.
Module 5: Using Server-Side Scripts 3 Working of ASPs
!
When a browser requests an ASP file from a Web
server:
$
The server-side script gets executed
$
ASP processes the requested file, executes the script
commands contained in the file, and generates an HTML
Web page
$
The Web page is sent back to the browser When a browser requests an ASP file from a Web server:
!
The server-side script is executed.
Includes ASPs
$
Includes the Native Scripting Engine for VBScript
$
Includes the Native Scripting Engine for JScriptIIS 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.
Slide Objective
To provide an overview of
Microsoft Internet
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.
6 Module 5: Using Server-Side Scripts #
##
#
Object Oriented Programming (OOP) Concepts
Slide Objective
To provide an overview of
Object Oriented
Programming (OOP).
Lead-in
OOP is a programming
technique that enables
development of programs.
Delivery Tip
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.
Delivery Tip
From Notepad or
FrontPage, open
Results.asp from the folder
\Inetpub\wwwroot\1912\Sam
papps\Ch05. Briefly explain
the use of methods and
properties of the Request
and the Response object
with the help of the source
code.
Module 5: Using Server-Side Scripts 7
Properties are the attributes
of an object.
8 Module 5: Using Server-Side Scripts Methods
!
Methods:
$
Set of tasks that perform actions
$
Can return values and take arguments
$
Has access to the data known to the object
<% Response.Write("Hello") %>
<% Response.Write("Hello") %>Methods are sets of tasks that perform actions. Methods can return values and
take arguments. A method has access to the data known to the object.
Consider the following code:
<% Response.Write("Hello") %>
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
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.
10 Module 5: Using Server-Side Scripts Response Object
!
Used to send data back to the client Web browser
<% Response.Write(“Hello World”)%>
<% 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:
<% Response.Write("Hello World") %>
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
toolbar.
Clicking on the Preview pane works well for HTML files but not for
ASPs, as the file is not opened through Microsoft IIS. To view ASP files
from FrontPage, click the Preview in Browser icon on the toolbar. Slide Objective
To introduce the practice.
Lead-in
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
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
Query.htm is as follows:
<HTML>
<BODY>
<FORM NAME="frmdetails" METHOD="Get"
ACTION="details.asp" >
First name: <INPUT TYPE=”text” NAME="txtfname">
Last name: <INPUT TYPE=”text” NAME="txtlname">
<INPUT TYPE=”submit” NAME="btnsubmit">
</FORM>
</BODY>
</HTML>
Slide Objective
To explain the Request
object.
Lead-in
The Request object
captures the request made
by the user.
Delivery Tip
From Internet Explorer,
open Query.htm from the
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.
14 Module 5: Using Server-Side Scripts Practice: Using the Request Object
In this practice, you will use
the Request object to
access the data submitted
by a form.
Module 5: Using Server-Side Scripts 15
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:
<%
A Session object stores information needed for a particular session. The server
creates a Session object for every user who visits a Web site. A Session
variable stored in the Session object is assigned a value for each user. The
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.
Module 5: Using Server-Side Scripts 17 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
Delivery Tip
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.
18 Module 5: Using Server-Side Scripts Application Object
!
Application Object
$
Stores state information for use by script files
$
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
Module 5: Using Server-Side Scripts 19 The code for Application.asp is as follows:
<HTML>
<HEAD><title>Using the Application Object</TITLE></HEAD>
<BODY>
<%
varTime= Hour(Now())
IF varTime < 12 THEN
application("varWish")="Good Morning!"
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>
Notepad or FrontPage, open
Application.asp from the
folder
\Inetpub\wwwroot\1912\Sam
papps\Ch05. Use the source
code to explain the
Application variable.
20 Module 5: Using Server-Side Scripts The code for Lock.asp is as follows:
<HTML>
<HEAD><TITLE>Application Object</TITLE></HEAD>
<BODY>
<%
Application.Lock
Application("Visit") = Application("Visit") + 1
Application.Unlock
%>
This application page has been visited
<%= Application("Visit") %> time(s).
</BODY>
</HTML>
Delivery Tip
From Internet Explorer,
open Lock.asp from the
address
http://localhost/1912/Samp
apps/Ch05/Lock.asp and
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.