Tải bản đầy đủ (.pdf) (52 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (920.03 KB, 52 trang )







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


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

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:

!
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
iv Module 5: Using Server-Side Scripts


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.
Module 5: Using Server-Side Scripts 1


#
##
#

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
\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 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
2 Module 5: Using Server-Side Scripts


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

!
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
4 Module 5: Using Server-Side Scripts


Microsoft Internet Information Server (IIS)
!
IIS
$

Includes ASPs
$
Includes the Native Scripting Engine for VBScript
$
Includes the Native Scripting Engine for JScript


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.
Slide Objective
To provide an overview of
Microsoft Internet

Information Server (IIS).
Lead-in
IIS comes with Active
Server Pages (ASPs) with
which you can build
dynamic Web pages.

Module 5: Using Server-Side Scripts 5


ASP Object Model
!
Response Object
!
Request Object
!
Session Object
!
Application Object
!
Server Object


ASP provides a set of objects that can be manipulated with scripting languages.
There are five predefined objects provided by ASP, which are used in the
interaction between browsers and Web servers. The table below lists and
describes the ASP objects.
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.
6 Module 5: Using Server-Side Scripts


#
##
#

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.
<HTML>
<HEAD>
<TITLE>Confirmation Page</Title>
</HEAD>
<BODY>
<% fname=Request.Form("txtfname")
lname=Request.Form("txtlname")
age=Request.Form("txtage")%>
<% Response.Buffer=true%>
<P> <%Response.Write("Hello " & fname & “ “ & lname & " .
You are " & age &" years old")%> </P>
</BODY>
</HTML>


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
!
Properties:
$
Refer to data, attributes, and settings of an object
$
Do not take any arguments
$
Used to describe or set the state and appearance of an
object
<% Response.Buffer = True %>
<% 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.
Slide Objective
To provide an overview of
properties.
Lead-in

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

An object contains data
members. The only way to
access the data members of
an object is by using its
methods.
Module 5: Using Server-Side Scripts 9


#
##
#

Using ASP Objects
!
Response Object
!
Practice: Using the Response Object
!
Request Object
!
Practice: Using the Request Object
!
Session Object
!
Application Object
!
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.
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") %>


Slide Objective
To explain the Response
object.
Lead-in
The Response object sends
data to the client Web
browser. The Response
object sends data to the
client Web browser.
Module 5: Using Server-Side Scripts 11


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

Response object.
Tip
12 Module 5: Using Server-Side Scripts


Request Object
!
Used to capture information submitted by a form
!
QueryString collection
!
Form collection
(Open query.htm from Sampapps)
<% first=Request.QueryString("txtfname")%>
<% first=Request.QueryString("txtfname")%>
<% first=Request.QueryString(“txtfname”)
last =Request.QueryString(“txtlname”)
Response.Write(“Welcome” & first & last)%>
<% 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
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

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.
Module 5: Using Server-Side Scripts 13


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.
14 Module 5: Using Server-Side Scripts


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

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.

16 Module 5: Using Server-Side Scripts


Session Object
!
Session Object
$
Stores information needed for a particular session
(View session.asp from Sampapps)
<% Session(“CompanyName”)=“Microsoft” %>
<% Response.Write Session(“CompanyName”) %>
<% Session(“CompanyName”)=“Microsoft” %>
<% Response.Write Session(“CompanyName”) %>



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

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:
<HTML>
<HEAD><TITLE>Using the Session Object</TITLE></HEAD>
<BODY>
<%
currenttime= Hour(Now())
IF currenttime < 12 THEN
session("varGreeting")="Good Morning!"
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>
</BODY>
</HTML>

The code for Connect1.asp is as follows:
<HTML>
<HEAD><TITLE>Testing the Session Variable</TITLE></HEAD>
<BODY>
The Session variable varGreeting contains the value:
<%=Session("varGreeting")%>
</BODY>
</HTML>



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
$

Lock and UnLock Methods
(View application.asp and lock.asp from Sampapps)
<% Application.(“CompanyName”)=“Microsoft”
Response.Write Application(“CompanyName”)%>
<% Application.(“CompanyName”)=“Microsoft”
Response.Write Application(“CompanyName”)%>
<% Application.Lock
Application("Visit")=Application("Visit")+1
Application.Unlock %>
<% Application.Lock
Application("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
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>

</HTML>

The file Connect.asp displays the value of the application variable created in
Application.asp. The code for Connect.asp is as follows:
<HTML>
<HEAD><TITLE>Testing the Application Variable</TITLE></HEAD>
<BODY>
The Application variable varWish contains the value:
<%=Session("varWish")%>
</BODY>
</HTML>

Because the Application object can be shared by more than one user, the Lock
and Unlock methods are used to ensure that multiple users do not try to alter a
property simultaneously. The Lock method prevents other users from
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.
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

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.
Module 5: Using Server-Side Scripts 21


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.

×