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
|Getting information from the User
|Sending information to the User
|Storing information about a User’s Session
|Sharing information for the entire application
|Accessing the server
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
- 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”).
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.
If you have created a text box like one
then, your request statement should be,
Request.Form(“Text1”) or Request(“Text1”).
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
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.
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.
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
- Redirect – Used to send the user to a new Page.
The syntax to use these method is Response.Method
following statement will write the string inside paranthesis on the
Text from Write
Text from Write Method
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
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.
session object has one method, Abandon. This method is used to
explicitly close the session and hence destroying the session
You can create new variables with session scope
using the following syntax:
And the same can be referred using the following syntax:
The data you entered in the
form(name and email) of previous example is stored has session
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
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.
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.
object is typically used to create variables that need to maintain
application level scope.
The Application object has the
- Lock- To Lock the variable
- Unlock – To unlock the variable
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:
And the same can
be referred using the following syntax:
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:
number of Records entered so far:
Enter new values in the
previous form and submit the same. Then see the value in above line
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.
This object gives access to Server
components,its methods and properties.
This object has the
- CreateObject – An important method used to create instance of
- HTMLEncode – To HTML encode a string passed
- URLEncode – To URL encode the string.
CreateObject is a very important method and it is widely used in
most ASP Pages.The syntax to use any of these methods is
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.
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.
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:
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.