Vous êtes sur la page 1sur 19

WEB TECHNOLGY 200

ACTIVE SERVER PAGES OBJECT


MODEL
The ASPs server component, ASP.DLL contains built-in objects that are
readily available to the user when an ASP application is created.
There are seven main objects that comprise the ASPs object model as in
the list below:
• Request Object
• Response Object
• Session Object
• Application Object
• Server Object
• Object Context Object
• ASP Error Object

REQUEST OBJECT
1
WEB TECHNOLGY 200
9
Request object handles all of the requests from the user, including data
from cookies, forms and security certificates. This object is used when
getting information from the user.
An overview of Request Object
Collections Events Methods Properties
Client Certificate Binary Read Total Bytes

Cookies

Form

Query String

Server Variable

Reading binary data from the user


This method is used to read the specified number of bytes directly from
the http request body sent by the client using post method into an asp
program.
Syntax:

Special array name=request.binaryread(no


of bytes)

Cookies: cookies are small files of clear text that contain information such
as user preferences when changing pages within a site or for subsequent
visits.
Servervariables
Syntax:

Variable=request.servervariables (name of the environment


variable)

1
WEB TECHNOLGY 200
9

Some of the commonly used environment variables are given below:


1.URL The complete path except the query string.
2.PATH_INFO The complete path except the query string.
3.PATH_TRANSLATED The full physical path of the currently executed
ASP program.
4.APP_PHYSICALPATH The physical address of the server’s root
directory.
5.QUERY_STRING Just the query string portion of the URL.
6.SERVER_NAME The name of the computer that is running the
web server.
7.SERVER_SOFTWARE The type of the web server along with its version
number.

1
WEB TECHNOLGY 200
9

RESPONSE OBJECT
Response Object handles the task of sending data back to the user. The
Response Object’s write method can be used explicitly and implicitly. The
Response Object’s Is Client Connected property checks to see if the
connection between the user or client is still alive, which relies on the
session object behind the scenes.
An overview of Response Object

Collections Events Methods Properties


Cookies Add Header Buffer

Append To Log Cache Control Working with


buffer
Binary Write Char Set
The output to a
Clear Content Type client can be
sent by the
End Expires server in 2
different ways
Flush Expires namely
Absolute
Redirect 1. Buffered
IsClientConnect output:
Write The
ed
buffered
PICs output is
not sent
Status until the
script is
finished i.e., the complete output of the script is sent at once.
2. Un –Buffered output: An un-buffered output is sent immediately as
each line is created.
In order to send buffered output to the client we can use the property
buffer of response object

2
WEB TECHNOLGY 200
9
Syntax:

Response.buffer=booleanvalue[it can be true


or false]

Common status values


Status Value Short Description
200 Ok

201 Created

202 Accepted

204 No Content

301 Moved Permanently

302 Moved Temporarily

304 Not Modified

400 Bad Request

401 Unauthorized

403 Forbidden

404 NA Found

500 Internet Server Error

501
1. response.write Not Implemented

502 Bad Gateway


2. response.binarywritebinaryarray

3. response.redirect=URL
503 Service Unavailable
4. response.addheader(“header name”,”header
value”)
Syntax for methods of response object:
5. response.appendtolog(string)

6. <%response.clear%>
1
7. <%response.end%>

8. <%response.flush%>
WEB TECHNOLGY 200
9

Setting Page Expiration

The user can set for the user’s browser to cache the page. If it is to
be cached, how long? The web pages should be cached on the
client’s machine, the response object is provided with two properties.
They are:
– Expires
– Expires Absolute
Expires
Using this property we can specify for how long? The client’s
machine should cache the current page in terms of minutes.
Syntax:

<%response.expires=time in
minutes%>

Expires Absolute
If we want a particular page to be removed from the client’s machine
on a particular date and time, we can use the Expires Absolute
property of the Response Object.
Syntax:

<%response.expiresabsolute=#date,
2
time#%>
WEB TECHNOLGY 200
9

Checking if the user is still connected


Sometimes, we may face a situation that to check whether or not the user
is still connected in such scenarios we can use the property of Response
Object called “IsClientConnected”
Syntax:

<
%response.isclientconnecte

Setting the content type


In order to inform the browser about the type of content that is present in
a response from the server, we can use property called “Content type”.
Syntax:
Response.contenttype=”maintype/s
ubtype”

Clear
When clear method is invoked the response body of the http is erased
Syntax:
<
%response.clear

End
The End method causes the server to stop the execution the script and
send any buffered output.
Syntax:
<%response.end
%>

Flush
2
WEB TECHNOLGY 200
9
This method immediately sends any previously buffered output to the
client but continues the processing of the script.
Syntax:

<%response.flush%>

SESSION OBJECT
Session Object maintains a connection between an ASP application and
each user. The session object prevents users from having to authenticate
repeatedly.
Session object helps to maintain variables or information required for the
entire session.
Session object relies on cookies.
An overview of session object
Collections Events Methods Properties
Contents Onstart Abandon Codepage

Staticobjects Onend Contents.remove LCID

Contents.remove SessionID
all
TimeOut

Properties
Session: The interval an user spends after he has made a request to one
of the pages and before the user abandons it either through the code or
after the specified timeout value has reached is called a session.
Session Id: Each user is given an unique number usually known as
sessionid at the beginning of his session that is stored on the web server.

1
WEB TECHNOLGY 200
9
The sessionid is then written automatically into a cookie on the client’s
machine.
The sessionid value can be determined from within an ASP script as
follows
<% dim idval

Idval=session.sessio
nID

%>

Timeout: A timeout can also be set for the user sessions. The time period
for which the server will maintain a session for a user can be important.
Default timeout is 20 minutes.
Note: the default timeout depends on the version of IIS.
Syntax:

<%session.timeout=time in minutes
%>

Codepage: A page that contains the character set used by a specific


locale with all the alphanumeric characters and punctuations is called a
code page.
The different code pages are identified by a unique number called
codepage id.
Syntax:

Session.codepage
=ID

LCID: The acronym for LCID is Location Identifier. The user can set his
ASPs to use a specific system locale by setting the location identifier. This
helps to set the locale for an ASP to present information in a more suitable
manner.

3
WEB TECHNOLGY 200
9
Syntax:

Session.LCID=val
ue

Events

Onstart: This event is triggered when a user who has not instantiated a
session on the web server requests any page from the server i.e., begins a
session.

The code for this event if exists resides in the global.asa file. This code is
executed well before executing any code of the requested page.

Sub
session_onstart

----------

----------

End sub

Onend: This event is triggered either when the user’s session times out or
when the ASP program invokes the abandon method of the session object.

The code
Sub
for this event if exists resides in global.asa file.
session_onend

----------
1
----------
WEB TECHNOLGY 200
9

Methods
Abandon: The session object’s abandon method can be used to clear all
the session variables in one step.
Syntax:
Session.aband
on

Contents.remove: removes a single item from the contents collection


with contents.remove method.
Syntax:
Session.contents.remove(object
name)

Contents.removeall: removes all items in the session.


Syntax:

Session.contents.remov
eall

3
WEB TECHNOLGY 200
9

APPLICATION OBJECT
Application Object basically all of the files contained within a virtual
directory and all of its subdirectories. So a collection of files that make up
a particular ASPs site are referred to as ASPs applications or ASP
applications.
The user can also access all of the global variables and objects that are in
global, as a file, through the use of Application Object.
Collections Events Methods Properties
Contents OnEnd Contents.remove

Staticobjects OnStart Contents.remove


all
Lock

Unlock

Note: There are no properties for application object.


Using Locking Mechanisms with the application object
The application object offers lock method and the unlock method to keep
track of the variables used by many ASPs.
Both the events require no arguments. The user has to first use the Lock
method
<% and then the unlock event as follows:

Application.lock

Statements

1
Application.unlock

%>
WEB TECHNOLGY 200
9

Fact: collections, events are same as session object. In the


application object apart from onstart and onend events there are
other two events namely lock and unlock.

SERVER OBJECT
The server object is the web server. This object is used to incorporate
some of the server’s own functionality into the ASPs. The object can be
used to handle script time outs to execute other ASPs from within an ASP.
An overview of server object
Collections Events Methods Properties
Createobject scriptTimeOut

Execute

Getlasterror

HTMLencode

Mappath

Transfer

URL Encode

PROPERTIES
ScriptTimeOut: The server object’s ScriptTimeOut property is used to set
a timeout for the ASPs. The default time out for every script in ASP is 90
seconds unless the timeout is altered explicitly.

1
WEB TECHNOLGY 200
9
Syntax:

Server.scripttimeout=an
integer value
METHODS
CreateObject: method is used to create an instance of non-intrinsic
objects.
Syntax:

Dim name of the instance variable

Set name of the


instance=server.createobject(“name of the
object that should be used”)
Execute: method is similar to the response object’s redirect method. The
redirect method will send information back to the client but the execute
method will handle the calling entirely on the server. The user can split
the script into smaller parts that can be accessed without performance
loss. It will also return the calling script.
Syntax:

Server.execute(“name of ASP
file”)

HTMLEncode: method allows us to display the source code of html page


on the client’s browser without allowing it to interpret them.
Syntax:

Servre.HTMLEncode(“a set of html


tags”)

MapPath: is used to determine the physical path of the file. This method
takes virtual path or relative path as a parameter and converts it into a
physical path.
Syntax:

String name=server.mappath(“virtual
string”)

1
WEB TECHNOLGY 200
9
Transfer: method is like the execute method, but does not return to the
original script.
Syntax:
Server.transfer(“name of a different ASP
file”)

Getlasterror: The ASP scripts raise 3 different types of errors namely


1. Pre-processing errors: are raised when the ASP pre-processor is
unable to find a file that is mentioned in the include directive.
2. Script compiling errors: are raised when the script compiler finds
an unrecognized token or missing the required keyword.
3. Run time errors: are raised due to a variety of factors such as
logical errors or missing or locked or altered resources.
Syntax:

Set
strvariable=server.getlasterr

URLEncode: method helps to encode a string so that it appears when


passed to a URL. It is used instead of the GET method to send the data
over the URL.
Syntax:

Strvariavle=server.URLEncode(“URL
string”)

2
WEB TECHNOLGY 200
9

OBJECT CONTEXT OBJECT


Object context object handles transactions, where an entire sequence
of steps either succeeds or fails as a whole. This object can be used with
Microsoft Transaction Server MTS and COM+ component services.
Collections Events Methods Properties
Ontransactionabor Setabort
t
Setcomplete
Ontransactioncom
mit

METHODS
Set Abort: If a transaction has become a failure, the entire transaction
should be aborted.
Syntax:
Objectcontext.seta
bort
Set Complete: this method can be invoked to indicate the transaction is
being successful.
Syntax:

Objectcontext.setcomp
lete

1
WEB TECHNOLGY 200
9

EVENTS
Ontransactioncommit: helps to use a sub routine that informs the user
of the success of a particular transaction that was requested.
Syntax:
<%

Sub
ontransactioncommit

-------------------

-------------------

End sub

%>
Ontransactionabort: is used when the user is able to use a subroutine
that informs the user of the failure for the particular transaction that was
requested.
Syntax:

<%

Sub
ontransactionabort

---------------

---------------

End sub

%>

3
WEB TECHNOLGY 200
9

ASP ERROR OBJECT


ASP Error Object provides greater error handling information that the
error object which comes with VBscript and is new addition in ASP3.0. The
ASP error object to catch bugs and errors in the scripts and code.

Collections Events Methods Properties


ASPcode

ASPdescription

Category

Column

Description

File

Line

Number

Source

Category property lets to know whether it is a run time error or syntax


error or a preprocessing error.
Number property is standard COM error number-a long value.
Line and Column property indicate the line and the column respectively
of the first character that the server encountered where the error
occurred.
File property lets to know what file the error occurred in.
Description property gives short description of what the error was and
the ASP description property gives a lengthier description of the problem.

1
WEB TECHNOLGY 200
9
ASPcode property is a code that IIS assign to specify ASP’S error.
Source property displays the actual line of script where the error
occurred. It is helpful with syntax error.

Vous aimerez peut-être aussi