VKS-LEARNING HUB

Home » Class XII » MMWT-XII » ASP-Objects

ASP-Objects

The interaction between Web clients and servers is complex, with many messages passed between the two.
However, most of the time we only need to work with one part of this complex interaction, for example a cookie sent by a user’s browser to our Web server.
One of the goals of ASP, and object-oriented programming in general, is to hide unneeded complexity from the developer.

The designers of ASP have built a model of this complex interaction in the ASP object model. As we work with ASP, we’ll do so through the five “built-in” objects of ASP:

  1. Application,
  2. Request,
  3. Response,
  4. Server, and
  5. Session.

REQUEST OBJECT

request

The ASP Request object represents the client-side request and any user data sent as part of that request.
The server’s task in receiving a request is to:

  1. Identify the data
  2. Store and/or process the received data
  3. Respond appropriately

Client-side: Running on a client, rather than a server. For example, a Java applet embedded in an HTML page runs on the client computer, not the server that sends out the page.

The Request object makes all the information sent from the user’s browser, including binary files sent as file uploads, available to the server . Some information, such as the browser type and operating system, may be used by the server to send browser-specific versions of Web pages to the user .

Uses of the Request object

Although the Request object has many features , it is most commonly used for four tasks:

  1. Reading data from browser cookies (Request.Cookies)
  2. Reading query strings (Request.Query)
  3. Reading input HTML <FORM> elements (Request.Form)
  4. Reading and storing Server Variables, the header information from browser requests (Request.ServerVariables)

Upcoming lessons will illustrate each of these uses of the Request object.
The next lesson describes how the Request object processes HTML Form data.

Reading FORM data

The following code segment shows part of an HTML input form, specifically a text box containing the user’s name:

<INPUT TYPE=”TEXT” NAME=”Name” SIZE=”25″ VALUE=”Enter your name”>

The ASP Form collection allows you to reference this and other data contained in the form. Since the text box form element is named Name, you would reference it as Form(“Name”). To write this piece of data back to the browser, you only need to write the ASP code:

<%=Request.Form(“Name”) %>

For some form elements (such as <SELECT> or check boxes), you can choose more than one item.
That’s not difficult in ASP, because you can specify an index for the Form collection and access one value at a time.
If you don’t specify an index, you get a comma-separated list of all the element values.

Form elements in HTML (text and password input areas, check boxes, radio buttons, and dropdown lists) allow the user to submit a variety of data to the server. But now what? The server needs to read, store, and process this information. The Request.Form version of the Request object can do this.

Query String

Query strings contain user data appended to a URL. Unlike data in browser cookies, the data in a query string can’t be stored between user visits to a Web site, but they are useful for retaining user information during a single visit.

Using GETs and POSTs

The METHOD field of an HTML FORM command allows the use  of  POST or GET for its method, and requests the URL in the ACTION field (in our case, an ASP script) from the server. Each method is useful, but used differently.

POST is used for passing more complicated information or information to be processed (in a situation, for example, where a user clicking the browser’s Reload button could result in an accidental duplicate order). In the previous lesson, we used Request.Form to read data submitted with the POST method.

create  vksform.html and write a code given below

<form method=”POST” action=”vksPost.asp“>

Name <input type=”text” name=”Name”/>

Age <input type=”text” name=”Age”/>

<input type=”submit” value=”Submit Query” />

</form>

output of vksform.html
form1
When user click Submit Button

code of vksPost.asp get excuted

<%
Dim name, age
name = Request.Form("Name")
age = Request.Form("Age")
Response.Write("Name: " & name & "<br />")
Response.Write("Age: " & age & "<br />")
%>
vksPost.asp will produce output like
Vinod
35

GET is typically used to pass parameters for retrieving data, and can be used to pass hidden fields as well, such as a user’s current location in a Web application. Using the GET method encodes the Form data and appends it to the end of the ACTION URL. A ? separator follows the URL; other form fields are separated from each other by a &. character.

create  vksform.html and write a code given below

<form method=”GET” action=”vksGet.asp”>

Name <input type=”text” name=”Name”/>

Age <input type=”text” name=”Age”/>

<input type=”submit” value=”Submit Query” />

</form>

 
output of vksform.html

form1 When user click Submit Button

code of vksGet.asp get excuted

<%
Dim name, age
name = Request.QueryString("Name")
age = Request.QueryString("Age")
Response.Write("Name: " & name & "<br />")
Response.Write("Age: " & age & "<br />")
%>
vksGet.asp will produce output like
Vinod
35

Cookies  A small chunk of data that is initially sent by a server to be stored on a client’s machine. Subsequent requests from that client are sent along with previously-defined cookies and their values.

For example, a user’s username and password might be stored in cookies, so that the user does not have to enter them every time to access a protected resourceAlthough you cannot store much information in any one cookie, they are still useful for:

Identifying returning users: Many sites store a unique user ID in a cookie and then use the cookie value to retrieve user information from a database.

Tracking a user’s order: Many sites that maintain “shopping carts” to store a record of items to be purchased use cookies to track the cart’s contents.

RESPONSE OBJECT

response

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

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

Statement

<% Response.Write(“<font color=red>Text from Write Method</font>”)%>

Output

Text from Write Method

Redirect

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

Will redirect or start newpage.hml page on web broswer

Write text with ASP

<%
response.write(“Hello World!”)
%>

</body>
</html>

Hello World!

 

Format text with HTML tags in ASP

<html>
<body>
<%
response.write(“<h2>You can use HTML tags to format the text!</h2>”)
%>

<%
response.write(“<p style=’color:#0000ff’>This text is styled with the style attribute!</p>”)
%>
</body>
</html>

You can use HTML tags to format the text!

This text is styled with the style attribute!

 

Redirect the user to a different URL

<%
if Request.Form(“select”)<>”” then
Response.Redirect(Request.Form(“select”))
end if
%>
<html>
<body>

<form action=”demo_redirect.asp” method=”post”>
<input type=”radio” name=”select”
value=”demo_server.asp”>
Server Example<br>
<input type=”radio” name=”select”
value=”demo_text.asp”>
Text Example<br><br>
<input type=”submit” value=”Go!”>

</form>

</body>
</html>

Output

redirect

if the user select Server Example he will be redirected to demo_server.asp

if the user select text Example he will be redirected to demo_text.asp

Response.clear

Response.flush

The Clear method erases any buffered HTML output. However, the Clear method erases only the response body; it does not erase response headers. You can use this method to handle error cases. Note that this method causes a run-time error if Response.Buffer has not been set to TRUE.

The Flush method sends buffered output immediately. This method causes a run-time error if Response.Buffer has not been set to TRUE.

<%

Response.Buffer=true

%>

<!DOCTYPE html>

<html>

<body>

<p>This is some text I want to send to the user.</p>

<p>No, I changed my mind. I want to clear the text.</p>

<%

Response.Clear

%>

</body>

</html>

<%

Response.Buffer=true

%>

<!DOCTYPE html>

<html>

<body>

<p>

This text will be sent to your browser when my response buffer is flushed.

</p>

<%

Response.Flush

%>

</body>

</html>

Output blank page as all clear

Output

This text will be sent to your browser when my response buffer is flushed

 

SESSION OBJECT

seesion

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 <%Session(“Name”)%> and <%Session(“EMail”)%> 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.

Top of Form

Sessions helps to preserve data across successive accesses. These can be done on a per user basis, via the use of session objects. Session objects give us the power to preserve user preferences and other user information when browsing a web application. To better understand the use of sessions, consider an example: Suppose you own a website in which you give the visitors the option to choose the background color of the pages they will browse. In such a case you need to remember the user’s choice on each of the page. This task can be accomplished using sessions.

An individual user’s Session object, an array that can grow as more data is stored for that user, is created by ASP when a user first contacts the Web site. The user is associated with a particular set of session data through an ASP-generated Session ID, sent as a cookie to the user’s browser.
Session cookie is a temporary one: it does not have an expiration date and time, and it does not store any data fields, just the Session ID. When the user closes his or her Web browser, session cookies are not saved.

Establishing Session ID

session1

session2

session3

session4

APPLICATION OBJECT: –    

 

application

A group of ASP files may be called an application on the Web . Some purpose can be achieved when the ASP file work together.In ASP ,the Application object is used to tie these files together.

For storing and accessing variables from any page, just like the Session object,the Application object is used . The difference is that in Sessions there is one Session object for EACH user while ALL users share one Application object .

In the Particular application (like database connection information),the Application object should hold information that will be used by many pages . This means it is possible access the information from any page. In an Application object , you can change the information in one place and the changes will automatically be reflected on all pages

The Global.asa file

In an ASP application,the Global.asa file is an optional file that stores declarations of objects, variables, and methods that can be accessed by every page . Global.asa file uses all valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) The Global.asa file stores only the following:

  • Application events
  • Session events
  • <object> declarations 
  • TypeLibrary declarations
  • the #include directive

Note: Each application can only have one Global.asa file and the Global.asa file must be stored in the root directory of the ASP application.

Events in Global.asa

when the application/session starts or application/session ends ,it is necessary to tell the application and session objects in Glogal.asa about the work to be done. The code for this is placed in event handlers. The Global.asa file uses four types of events:

Application_OnStart – This event occurs in an ASP application when the FIRST user calls the first page from . This event occurs after the Global.asa file is edited or after the Web server is restarted.

You can create Application variables using “Global.asa” like this:

<script language=”vbscript” runat=”server”>
Sub Application_OnStart
application(“vartime”)=””
application(“users”)=1
End Sub
</script>

In the example above we have created two Application variables: “vartime” and “users”.

Application variables can be accessed like this:

There are
<%
Response.Write(Application(“users”))
%>
active connections

The “Session_OnStart” event occurs immediately after this event.

Session_OnStart – In the ASP application,this event occurs EVERY time a NEW user requests his or her first page .

Sub Session_OnStart
Application.Lock
Application(“visitors”)=Application(“visitors”)+1
Application.UnLock
End Sub

Session_OnEnd – EVERY time a user ends a session, this event occurs. After a page has not been requested by the user for a specified time (by default this is 20 minutes),a user ends a session .

Sub Session_OnEnd
Application.Lock
Application(“visitors”)=Application(“visitors”)-1
Application.UnLock
End Sub

Application_OnEnd – Aafter the LAST user has ended the session,this event occurs. Typically, this event occurs when a Web server stops. To clean up settings after the Application stops,this procedure is used like delete records or write information to text files.

A Global.asa file could look something like this:

<script language=”vbscript” runat=”server”>
sub Application_OnStart
‘some code
end sub
sub Application_OnEnd
‘some code
end sub
sub Session_OnStart
‘some code
end sub
sub Session_OnEnd
‘some code
end sub
</script>

Global.asa Example

In this example we will create a Global.asa file that counts the number of current visitors.

  • When the server starts,the Application_OnStart sets the Application variable “visitors” to 0
  • Every time a new visitor arrives,the Session_OnStart subroutine adds one to the variable “visitors”
  • The Session_OnEnd subroutine subtracts one from “visitors” each time this subroutine is triggered

The Global.asa file:

<script language=”vbscript” runat=”server”>
Sub Application_OnStart
Application(“visitors”)=0
End Sub
Sub Session_OnStart
Application.Lock
Application(“visitors”)=Application(“visitors”)+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application(“visitors”)=Application(“visitors”)-1
Application.UnLock
End Sub
</script>

In an ASP file,to display the number of current visitors is given below:

<html>
<head>
</head>
<body>
<p>
There are <%response.write(Application(“visitors”))%> online now!
</p>
</body>
</html>

Application Lock Method

Application Unlock Method

Using “Lock” method, you can lock an application Using “Unlock” method, you can unlock an application
The users cannot change the Application variables(other than the one currently accessing it),when an application is locked This method is used to remove the lock from the Application variable

SERVER OBJECT

server


Leave a comment