Вы находитесь на странице: 1из 30

ASP Applications

What is an ASP Applications?


An ASP application can be defined as a set
of inter-related Active Server Pages.
In ASP, each virtual directory on a Web
Server is an application, and all the files and
folders in that virtual directory form a part
of the application.

Properties of an ASP Application


It allows the sharing of data among the
pages in the application.
It triggers various application scripts based
on an event. For example, the start of an
application is an event.
It allows the instances of objects to be
shared among all the pages of an
application.

Continue
A web site can have different applications,
each having individual properties.
When an application ends, it does not affect
the other applications.
Each application can be run in its own
memory space, resulting in more security fo
information.

An Application Object
An application object is used to store the
information that is common to an ASP
application and is shared by all the users of
the application.
ASP applications are used to retain
information that may be used by other web
pages. This is also called maintaining the
state in ASP.

Maintaining state
ASP applications are used to retain
information that may be used by other web
pages. This is also called maintaining state
in ASP.
An ASP application maintains two types
states:
The application state retains the information
of the application that is available to all the
users of the application.

The session state retains the information


that is required by a specific user in a
session. A session starts when a user
requests for a page from a web site and ends
when the user leaves the web site.

Application Object
An Application object is used to initialize
an application, automatically start processes
that are required for an application, and
allow the declaration of variables that can
be used by all the users and across all the
pages.

Application Object Methods


Lock: it prevents other clients from
modifying the properties of the Application
object.
Application.Lock
Unlock: it allows the clients to modify the
properties of the Application object.
Application.Unlock

Application object Events


Application_onStart: it marks the beginning
of an Application event.
Application_OnEnd: it marks the end of an
application event.

Controlling the Start and End of


an Application Using the
Global.asa file
The Global.asa file resides in the same folder as
the application. An application generates certain
events such as when it starts for the first time or
when it ends.
Global.asa file contains scripts for the start and
end events of an application or a session.
Each application can have only one global.asa file
attached to it.

<script language=vbscript runat=Server>


Sub application_onStart
Application.lock
Application(custname)=Jennie
Application.unlock
End sub
Sub Application_onEnd
..
End sub

Application Variables
Application variable is used to store data
that can be accessed throughout all the
pages of the application and by all the users
of the application.
An application can contain any type of data,
including arrays and objects.

Some of the purposes of


application variables are to:
Display momentary information on every web
page, for example, to display the daily news
updates
Count the number of times a user has accessed the
site
Hold the data retrieved from the database and
display the details on multiple web pages
Enable communication among users, for example,
to create applications that help users to chat.

Session Object
The session object is used to store the
information required for a particular session
by a specific user.
The variables stored in the Session object
are available to users throughout the
application for a particular session.

The web server automatically creates a


Session object for a user who requests for a
page of an application if the user does not
already have a session.
The web server destroys the Session object
when the session is terminated or timed out.

A Session Object Method


Abandon: it prevents other clients from
modifying the Application object properties
Session.Abandon

A Session Object Property


TimeOut: it has the timeout period for the
session state of the current application, in
minutes.
Session.TimeOut=minutes
SessionID: it returns the session ID for the
user of the current session.
Session.SessionID

Maintaining the Client State


The information provided by a user can be
stored using various methods. This process
of storing information is called maintaining
the client state.

Cookies
Cookies store information about a user and are
used by a particular Web server that the user has
last visited. They are used to identify each user
who visits a Web site.
Netscape communications corporation was the
first to introduce cookies in its first browser. The
World Wide Web Consortium (W3C) has accepted
cookies as a standard that is now being adapted by
many of the browsers.

Continue
Cookies are browser-dependent. The browsers
may or may not have the capability of the cookies.
Browsers that support cookies maintain one or
more special files that store information from the
web.
The Response and Request objects have the
Cookies collection. The Cookies collection of the
Request object is used to retrieve the values of the
cookies sent in the HTTP request and the Cookies
collection of the Response object is used to set the
value of a cookie.

Creating a Cookie
A cookie is created using the Cookies
collection of the Response object. A cookie
can be of two types, a single value cookie
or a cookie dictionary, which contains
multiple name and value pairs.
If a specified cookie does not exist, it is
created.
Otherwise, it takes the new value and
discards the old value.

The syntax for creating a cookie is as


follows:
Response.Cookies (cookies)
[(key)|.attribute]=value
The parameters are:
cookies it is the name of the cookie.
Key it is an optional parameter. If key is
specified, the cookie is a dictionary and the
value is assigned to the key.
Attribute it provides information about the
cookie itself.

Attribute
Name
Domain

Type
Write-only

If specified, the cookie is sent only to the


requests to this domain

Expires

Write-only

It specifies the date on which the cookie


expires.

HasKeys

Read-only

It specifies whether the cookie contains keys.

Name
Path

type
Write-only

It specified, the cookie is sent only to requests


to this path. If the path is not set, the cookie
uses the application path.

Secure

Write only

It specifies whether the cookie is secure.

Value- specifies the value to assign to a key


or attribute

Example
<%
Response.Cookies(loginName)=Jenie
%>
<%
Response.Cookies(loginName)=Jenie
Response.Cookies(password)=rose133
%>

Cookie dictionary
A Cookie dictionary is a single cookie with
multiple name and value pairs.
<%
Response.Cookies(userLogin)
(Name)=Jenie
Response.Cookies(userLogin)
(password)=rose123
%>

Passing Cookies through the


HTTP Header
<%
Response.Cookies(UserLogin)=Jenie
Response.Cookies(UserLogin).Expires=Jan 26 2005
Response.Cookies(UserLogin).Path=/AspCode
Response.Cookies(UserLogin).Domain=.asp.com
Response.Cookies(UserLogin).Secure=True
%>

Retrieve the value of cookie


To retrieve the value of a cookie, you use the
Cookies collection of the Request object.
Syntax
Request.Cookies(cookies) [(key)|.attribute]=value
cookies contains the name of the cookie whose
value is to be retrieved.
Key An optional parameter that is used to
retrieve the subkey values of the cookie dictionary
Attribute- provides information about the cookie
itself, the attribute that can be used being given
below:

Name
HasKeys

Type
Read-Only

Specifies whether the cookie contains keys


<%
=Request.Cookies(UserLogin)
%>
<%
For each cookieitem in Request.Cookies
Response.write cookieitem & = &
Request.Cookies(userLogin) & <br>
Next
%>

Вам также может понравиться