GuidesExplaining Objects in ASP

Explaining Objects in ASP

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.





by Ajoy Krishnamoorthy

The World is talking about
component development, ActiveX controls, COM etc. We being an IT
professional, need to upgrade our skills with all these
technologies. Although there is a very little need for developing
our own components, using the existing components is a good choice .
This articles gives you in-depth information about various objects like Response, Request, Session in ASP. This will help you in understanding the objects that comes with ASP. The article explains the Objects with sample codes.

For instance, in Web development, it started with
information publishing using HTML. Then people got bored of static
content. This forced us to give some dynamic behaviour to the site.
We started adding interactive features and now we have come to a
stage of developing data intensive application over the web. And we
cannot rely on the scripts alone to deliver all goods for us. There
is a strong urge to reduce the repetitive tasks.

Precisely,in any database application, we need to have forms
to get data from the user.(For eg. the userform in the sample
application of previous article Easy steps to
build Web Application using ASP.
)

Likewise, we also need
to send some data back to the user. It might be search result or
account information or for that matter anything. But everyone agrees
that we need to implement a get and send method in all our
applications – A repetitive task.

The solution is to use
Objects to reduce these repetitive tasks. An object is a collection
of functionality and information. ASP has given us a handfull of
Intrinsic Objects.

Intrinsic Objects

Object Used For
Request Getting information from the User
Response Sending information to the User
Session Storing information about a User’s Session
Application Sharing information for the entire application
Server Accessing the server
resources


Request Object

This object is mainly used to retrieve the
information from the from in a HTML Page.

The Request Object
has the following Collections:

  • Form – To access value from a form submitted using POST
    method.
  • QueryString – To access varibales sent with URL after “?” or
    from a from submitted using GET method.
  • Cookies – To access the value of a Cookie.
  • ServerVariables – To access information from HTTP Headers.

The syntax to access the variables of any of these
collections is Request.Collections(“Variables”).

But, you
can also use Request(“Variables”) instead.

When we use the
Form collection, we need to pass the name of the element that we
create in HTML Form as the Variable to the Request Object.

For example:

If you have created a text box like one
below:

then, your request statement should be,

Request.Form(“Text1”) or Request(“Text1”).

Likewise,
when using Querystring you need to pass the variable name used in
the URL or the name of the element specified in the form.(See the
eg. below)

We can use the cookies collection to retrieve any
cookies values set before. Again the variable should be the variable
name used to set cookie value.

The last collection in the
Request Object is ServerVariable. This is used to access the
information from the HTTP headers like the remote user IP address,
TCP/IP server port etc. We have a set of pre defined variables to
pass with the request object.

Example:

In the below
form, enter values in the name and email text box and click the
Submit button,to see how request object is working.

Name :

EMail :

  


Response Object

This object is used to send information to
the user ( i.e. to the browser).

The most used methods of
Response object are:

  • Write – Used to send information to be displayed on the
    browser.
  • Redirect – Used to send the user to a new Page.

The syntax to use these method is Response.Method

Eg: Response.Redirect(“newpage.html”)

The
following statement will write the string inside paranthesis on the
browser screen.

Statement

Text from Write
Method”)%>

Output

Text from Write Method


Session Object

This object is used to store information with
a scope to that user session. The information stored are maintained
even when the user moves through various pages in the web
application.

The session object has two properties.

  • SessionID – Created by the web application and sent as a
    cookie to client.
  • TimeOut – To set Session timeout period.

The
session object has one method, Abandon. This method is used to
explicitly close the session and hence destroying the session
object.

You can create new variables with session scope
using the following syntax:

Session(“Variablename”)=Value

And the same can be referred using the following syntax:

Session(“Variablename”).

The data you entered in the
form(name and email) of previous example is stored has session
variables.

The values entered in the previous form are:(if
you have not entered any values you will see “Please enter values
for the name and email field in the form” message).

Please enter values for the name and email field in the
form.

I have stored the values has
and in the
previous form. And for retrieving i use the same syntax , but with a
“=” symbol prefix.

Click the below button to close the
session. This will clear the session variables set earlier.Here, I
am using the Session.Abandon Method to destroy the session object.

Since the values are cleared, it will
display “Please enter values for name and email field in the
previous form” message.


Application Object

This object is used to share information
among the users of the web application. The variable becomes alive,
when the first request to the application comes in.

This
object is typically used to create variables that need to maintain
application level scope.

The Application object has the
following methods:

  • Lock- To Lock the variable
  • Unlock – To unlock the variable

Since the
application variables can be accessed by all the users of the
application, the lock and unlock method is used to avoid more than
one user accessing the same variable.

You can create new
variables with Application scope using the following syntax:

Application(“Variablename”)=Value.

And the same can
be referred using the following syntax:

Application(“Variablename”).

For every set of data
you enter in the form(name and email) of previous example, we have
stored a counter. This counter is stored as an Application variable.
This is common to the entire application.

See the Total number of Records entered below:

Total
number of Records entered so far:

Enter new values in the
previous form and submit the same. Then see the value in above line
being incremented.

I am using Application(“NumValues”) to
store the value and increment it by one for every set of data you
enter. Before incrementing I use Application.Lock method and after
incrementing I use Application.UnLock method.

This is how an
Application variable is useful.


Server Object

This object gives access to Server
components,its methods and properties.

This object has the
following methods:

  • CreateObject – An important method used to create instance of
    Server Components
  • HTMLEncode – To HTML encode a string passed
  • URLEncode – To URL encode the string.

The
CreateObject is a very important method and it is widely used in
most ASP Pages.The syntax to use any of these methods is
Server.Method.

For Eg:

Server.CreateObject(“ADODB.Connection”)

If you
remember, ASP can use any of the Active Server Components registered
on the server. And we have a set of Active Server Components with
IIS. We use Createobject method to access this components.

There are components like Data Access Component, Browser
Capabilities Component, Ad Rotator Component etc.

Each
component exposes certain objects and methods. We can use these
methods in our ASP Pages.

For instance let us see how we can
utilise the Browser Capabilities Component.

We will use the
Server.CreateObject method to access various methods and properties
of Browser Capabilites component.

Browser Capabilites
Component is used to find out the capabilities of the browser. We
can verify the capabilites of the browser and deliver content based
on its capabilities. For example, we can verify whether a browser
supports frames or not and depending on the result, you can direct
the user to a page with frames or to a page without frames. (Use
response.redirect method to direct the users different pages.Hope
you remember this method.)

The below table displays the
capabilites of your browser:

Property Result
Browser Type Netscape
Version 4.00
Frames True
Tables True
Cookies True
JavaScript True
VBScript False
ActiveX unknown

Source

Property Result
Browser
Type
Version
Frames
Tables
Cookies
JavaScript
VBScript
ActiveX

The first line of the source creates an instance of the
Browser Capabilites component. Then I am using the various
properties of this component and displaying its value in a table.

Similarly, we can use any of the components installed with
the server or we can create our own active server component and use
them in our ASP pages.

The opportunity to reuse any of the
server components set the stage open for ASP developers to build
their own component and use them, thus leaving only your imagination
as a limit.

You can download all the source code used
including this article from here.

Ajoy Krishnamoorthy

Get the Free Newsletter!
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories