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

Jatin Soni : 9904181899 : Hi Friends.

Part – II Working with Object


A popular buzzword in programming is “Object-Oriented
Programming”. OOP can help you in your ASP programming.

The Building Block of Objects


Programming objects are composed of the things that describe
them and the things that can be done with them. An object is
something that typically has methods, properties or
collections.
 Properties
The things you can do with an object are called properties.
Properties describe an object. An object’s properties can be read
or set to specify the state of the object.
 Methods
An object’s methods determine the things that one can do with
the object. Method associated with programming objects may also
need information. They receive it in much the same way as the
function and subroutines receive arguments. Methods often affect
the values of the properties methods are also often used to
retrieve the value of properties. Methods can also tell you the
value of properties.
 Collections
An object’s collection is of Key and value pairs. Each page
number (the Key) has a corresponding page of text (the value).
 Instances of Objects
Jatin Soni : 9904181899 : Hi Friends.
Jatin Soni : 9904181899 : Hi Friends.

One important thing to understand is the difference between an


instance of an object and the object itself.

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

The Fundamental Built-in ASP Objects

An ActiveX Object
An ActiveX object (component) is similar to an Active Server Page
built-in object. However, there is an important difference between
the two. An instance of the object must be explicitly instantiated
before it can be used.

I. Active Server Pages Objects – At a Glance


Active server Pages include several built-in objects and
installable ActiveX Components. These objects and components
can be used to extend the power of your Active Server Page
scripts. ASP derives their rich functionality from the user of six
inherent objects that do have to be instantiated. These objects
are special because they are built into Active Server Pages. These
objects have their own predefined functionality and their own
respective properties, methods, and events. The built-in Active
Server Page objects provide control over the session and Web-
Server applications. These objects form the foundation of ASP
development.
 Application Object
 Server Object
 ObjectContext Object
 Session Object
 Request Object
 Response Object
Jatin Soni : 9904181899 : Hi Friends.
Jatin Soni : 9904181899 : Hi Friends.

 Error (Err) Object

The Application Object


The Application object is used to manage all information in the
ASP application. The Application object is used to share
information among several clients visiting the same group of
pages. In ASP, the term application refers to all the .asp pages
in a directory and its subdirectories. Only one instance of the
Application object is created per application. It is shared among
all the clients accessing that application. The information can be
accessed and passed between different users in the application.
Because multiple people can try to simultaneously change
application variables, the Application object enables locking
and unlocking of its variables. When a variable is locked other
users cannot modify the properties of an Application object.

The Server Object


The Server object is responsible for the administrative
functionality of the server. The Server object provides a few basic
properties and methods. The most important of these is the
CreateObject method. One of the most exciting aspects of the
Server object is the capability to create an instance of a server
component via its CreateObject method. This capability to tap
into existing ActiveX servers transforms the Web server from a
simple file manager into a data translator. The MapPath method of
the Server object maps a physical or virtual path to a directory on
Jatin Soni : 9904181899 : Hi Friends.
Jatin Soni : 9904181899 : Hi Friends.

the server. The ScriptTimeOut property determines the amount


of time a script can run before the script process is terminated.
The HTMLEncode methods enables HTML encoding of a string.

 Tip:- Components are packages of related objects that you

can use in your pages. They make common ASP tasks easier,
and add a great deal of power to your pages.

The ObjectContext Object


The ObjectContext object is used by ASP to control transaction
processing using the Microsoft Transaction Server (MTS). This
direct processing within the MTS environment enables ASP to
explicitly control, commit, and rollback features of objects
managed by MTS.

The Session Object


The Session object is responsible for managing information for a
specific user session. The Session object refers to a single client
accessing an application. Therefore, a new instance of the
Session object is created of each session. The Session object is
important to carrying information as a client travels between
pages because Session variables persist for the entire session.
One page can store data into a Session variable, and that data
can be accessed from other pages in the session. These variables
are not accessible by other user sessions, but they can be passed
from page to page within the ASP application. The SessionID
property uniquely identifies the particular user session. The

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

Timeout property sets the time in minutes that the SessionID is


valid. Once the SessionID is invalid, the application destroys the
user’s variables and the user must enter the application from the
beginning. If the Timeout property is not reached, executing the
Abandon method will destroy the specific user session.

The Request Object


The Request object is primarily responsible for retrieving
information/data from the client browser, and it uses five
collections to provide for communication between the client
browser and Web server.

 Tip:- A collection is commonly referred to as a grouping of

common objects.

When the client’s Web browser makes a request for a particular


page, it sends some of information along to the server. That data
is packaged together in the Request object. The Request object
allows the page to retrieve what it needs – cookie information,
information from a form, query string data, and more.

 Tip:- Query string data is the extra stuff sometimes


attached at the end of a URL. It might look like
“?firstname=John&lastname=Smith”.

The ClientCertificate collection retrieves certification fields


from Web browsers. The Cookies collection enables the server to
collect information from client-side cookies. The Form collection
Jatin Soni : 9904181899 : Hi Friends.
Jatin Soni : 9904181899 : Hi Friends.

retrieves values posted by a client form. Finally, the QueryString


collection enables values to be parsed from the HTTP URL string.
The Request object can also retrieve server environmental
variables.

The Response Object


The Response object is used to send output. The Response object
is responsible for sending output from the server to the
requesting client. The three most common Response object
methods are the Write, Redirect, and Response methods. The
Response object uses the Write method to send HTTP output to
the user’s Web browser. The Response object can also control how
and when data is sent and write cookies to store information. The
Redirect method provides the capability to redirect the client
browser to a different URL. The Response object enables a Cookie
collection to set cookie values on the client’s browser.

Trapping errors with the Err object


Active Server Pages use the error handling found in VBScript to
process and manage runtime errors. VBScript handles error
trapping with the Err object. The Err object has several
properties and two methods. The Err object’s properties and
methods can be accessed using the following syntax:

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

Err[.{property | method}]
Where Err is the generated error object. The Err object’s
properties provide information about the error object. These
properties are listed below:
The Err object’s Properties
Property Description
Description Sets or returns the referenced error description.
Number Sets or returns the number value of the error.
Source Sets or returns the Application object that
generated the error.
HelpFile Sets or returns the fully qualified path to the help
file.
HelpContext Sets or returns the topic ID with the help file.

The Err object also provides methods that control it. The Err
object’s methods, as shown below, can be used to generate a
runtime error or rest the Err object’s properties.
The Err object’s Methods
Method Description
Raise Generates a runtime error.
Clear Resets all object properties.

In VBScript code, the On Error statement is used to enable error


handling. The Resume Next statement is used to respond to an
error condition by enabling the program to continue execution
with the next statement after the one that produced the error.
Jatin Soni : 9904181899 : Hi Friends.
Jatin Soni : 9904181899 : Hi Friends.

Therefore, to enable error handling in VBScript, use the following


line of code:
<% On Error Resume Next %>
If no error handling is activated, any runtime error that is
generated at the server will result in the immediate termination of
the script. However, if error handling is activated, the application
continues by executing the next line in the code.

Active Server Pages Objects – In Detail

The Application Object


The Application object is used to store and retrieve information
that can be shared among all users of an application. For
example, One can use the Application object to pass
information between users of the Web Site. Because more than
one user can share the Application object, there are Lock and
Unlock methods to ensure that multiple users do not try to alter
an application object’s property/properties simultaneously.
Syntax :- Application.method
Methods :-

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

 Lock :- The Lock method prevents other clients from


modifying Application object properties simultaneously.
The Lock methods blocks other clients from modifying the
content of the variables stored in the Application object,
ensuring that only one client at a time can alter or access
the Application variables.
Syntax :- Application.Lock
 Unlock :- The Unlock method allows other clients to modify
Application object properties. The Unlock method enables
other clients to modify the variables stored in the
Application object after in has been locked using the Lock
method. If this method is not called explicitly, the Web
server unlocks the Application object when the script ends
or times out.
Syntax :- Application.Unlock
The Server Object
The Server object enables to use the various utility functions on
the server. For example one can use the server object to create
instances of other objects.
Syntax :- Server.method
The Properties of Server object :-
 ScriptTimeout :- The ScriptTimeout property specifies the
maximum amount of time a script can run before it is
terminated. The timeout will not take effect while a server
component is processing.

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

Syntax :- Server.ScriptTimeout=NumSeconds
The Methods of Server object :-
 CreateObject :- The CreateObject method creates an
instance of a server component. If the component has
implemented the OnStartPage and OnEndPage methods, the
OnStartPage method is called at this time.
Syntax :- Server.CreateObject(progID)
 HTMLEncode :- The HTMLEncode method applies HTML
encoding to a specified string.
Syntax :- Server.HTMLEncode(string)
 MapPath :- The MapPath method maps the specified
relative or virtual path to the corresponding physical
directory on the server.
Syntax :- Server.MapPath(path)
 URLEncode :- The URLEncode method applies URL encoding
rules, including escape characters, to a specified string.
Syntax :- Server.URLEncode(string)

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

The Session Object


The Session object can be used to store and retrieve information
about particular sessions. One can use the Session object to
store information that persists over the course of a visit by a user
to your Web site.
The Web server automatically creates Session object when a Web
page from the application is requested by a user who does not
already have a session. The server destroys the session object
when the session expires or is abandoned.
One common use for the Session object is to store user
preferences set on a previous visit to the Web application.
Session state is only maintained for browsers that support
cookies.
Syntax :- Session.property | method
Properties :-
 SessionID – The SessionID property returns the session
identification for this user. Each session has a unique
identifier that is generated by the server when the session is
created. The SessionID is returned as data type LONG.
Syntax :- Session.SessionID
 Timeout – The Timeout property specifies the timeout
period for the Session object for this application, in minutes.
If the user does not refresh a page within the Timeout
period, the session ends.
Syntax :- Session.Timeout[ = minutes]
Methods :-
Jatin Soni : 9904181899 : Hi Friends.
Jatin Soni : 9904181899 : Hi Friends.

 Abandon – The Abandon method destroys all the objects


stored in a Session object and releases their resources. If
the Abandon method is not called explicitly, the server
destroys these objects when the session times out.
Syntax :- Session.Abandon
Events :- Scripts for the following events are declared in the
GLOBAL.ASA file. They are Session_OnEnd and Session_OnStart.
The Request Object
The Request object can be used to access all information sent in
request from a browser to the server. The Request object can be
used to retrieve information that a user has entered into an HTML
form. The Request object retrieves the values that the client
browser passed to the server during an HTTP request.
Syntax :- Request.[Collection](variable)
Collection of the Request Object :-
 ClientCertificate :- The values of fields stored in the client
certificate that is sent in the HTTP request. The
ClientCertificate collection retrieves the certification
fields (specified in the X.509 standard) from the request
issued by the Web browser. If a Web browser uses the
SSL3.0/PCT1 protocol (in other words, it uses a URL starting
with “https://” instead of “http://”) to connect to a
server and the server requests certification, the browser
sends the certification fields. If no certificate is sent, the

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

ClientCertificate collection returns EMPTY. The web


server must be configured to accept client certificates.
 Cookies :- The values of Cookies sent in the HTTP request.
A Cookie is an ASP object that allows a programmer to store
pertinent ”User data” either on the Web Server or on the
Client’s computer in C:\Windows\Cookies for retrieval and
use later. This data is largely used to maintain the ‘State’
of the connection between the Client and the Web server in
ASP applications. Cookies can store other data as well. The
Cookies collection enables you to retrieve the values of the
cookies sent in an HTTP request.
Syntax :- Request.Cookies(cookie)[(key)|attribute]
 Form :- The values of Form elements in the HTTP request
body. The form collection retrieves the values of form
elements posted to the HTTP request body by a form using
the POST method.
Syntax :- Request.Form(parameter)[(index)|.Count]
 QueryString :- The values of variables in the HTTP query
string. The QueryString collection retrieves the values of
the variables in the HTTP query string, that is, it retrieves the
values encoded after the questing mark (?) in an HTTP
request. It helps to extract and decode information through
the use of GET method.
Syntax:- Request.QueryString(variable)[(index)|.
Count]

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

 Server Variables :- The values of predetermined


environment variables. The ServerVariables collection
retrieves the values of environment variables.
Syntax :- Request.ServerVariables(variable)

The Response Object


One can use the Response object to send output to the client. The
Response object is used to send information back to a browser.
One can use the Response object to send output form the scripts
to a browser.
Syntax :- Response.collection|property|method
 Collections :- Specifies the type of collection. For example,
Cookies – The Cookies collection sets the value of a cookie.
If the specified cookie does not exist, it is created. If it exists,
it takes the new value and the old value is discarded.
Syntax:-Response.Cookies(cookie)[(key)|.
attribute]=value
Properties Of The Response Object :-
 Buffer :- Indicates whether page output is buffered. The
Buffer property indicates whether to buffer page output.
When page output is buffered, the server does not send a
response to the client until all of the server scripts on the
current page have been processed, or until the Flush or End
method has been called. The Buffer property cannot be set
after the server has sent output to the client. For this reason,

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

the call to Response.Buffer should be the first line of the


.asp file.
Syntax :- Response.Buffer[=flag]
 Content Type :- Specifies the HTTP content type for the
response. The Content Type property specifies the HTTP
content type for the response. If no Content Type is
specified, the default is “text/HTML”
Syntax :- Response.ContentType[=Content Type]
 Expires :- Specifies the length of time before a page cached
on a browser expires. The Expires property specifies the
length of time before a page cached on a browser expires. If
the user returns to the same page before it expires, the
cached version is displayed.
Syntax :- Response.Expires[=number]
 Expires Absolute :- Specifies the date and time on which a
page cached on a browser expires. The Expires Absolute
property specifies the date and time at which a page cached
on a browser expires. If the user returns to the same page
before that date and time, the cached version is displayed. If
a time is not specified, the page expires at midnight of that
day. If a date is not specified, the page expires at the given
time on the day that the script is run.
Syntax :- Response.ExpiresAbsolute[=[date][time]]
 Status :- The value of the status line returned by the server.
The status property specifies the value of the status line

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

returned by the server. Status values are defined in the HTTP


specification.
Syntax :- Response.Status=StatusDescription
Methods Of The Response Object:-
 AddHeader :- Sets the HTML header name to value. The
AddHeader method adds an HTML header with a specified
value. This method always adds a new HTTP header to
response. It will not replace an existing header of the same
name. Once a header has been added, it cannot be
removed. This method is for advanced use only.
Syntax :- Response.AddHeader(name, value)
 AppendToLog :- The AppendToLog method adds a string to
the end of the Web server log entry for this request. You can
call it multiple times in one section of script. Each time the
method is called it appends the specified string to existing
entry.
Syntax :- Response.AppendToLog string
 Binary Write :- Writes the given information to the current
HTTP output without any character-set conversion. The
BinaryWrite method writes specified information to the
current HTTP output without any character conversion. This
method is useful for writing nonstring information such as
binary data required by a custom application.
Syntax :- Response.BinaryWrite data
 Clear :- Erases any buffered HTML output. The Clear
method erases any buffered HTML output. However, the

Jatin Soni : 9904181899 : Hi Friends.


Jatin Soni : 9904181899 : Hi Friends.

Clear method only erases the response body, it does not


erase response headers. One can use this method to handle
error cases.
Syntax :- Response.Clear
 End :- Stops processing the .asp file and returns the current
result. The End method causes the Web server to stop
processing the script and returns the current result. The
remaining contents of the file are not processed.
Syntax :- Response.End
 Flush :- Sends buffered output immediately. The Flush
method sends buffered output immediately.
Syntax :- Response.Flush
 Redirect :- Sends a redirect message to the browser,
causing it to attempt to connect to a different URL. The
Redirect method causes the browser to attempt to connect
to a different URL.
Syntax :- Response.Redirect URL
 Write :- Writes a variable to the current page. The Write
method writes a specified string to the current HTTP output.
Syntax :- Response.Write variant

Jatin Soni : 9904181899 : Hi Friends.

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