www.tamertolba.4t.com

 

Chapter 3

Active Server Pages Intrinsic Objects

 

Active Server Pages provide several objects that do not need to be instantiated (created) before using them in scripts. These intrinsic (built-in) objects enable you to perform many tasks that previously would have required ISAPI or CGI programs.

 

3.1- The Built-in Objects:

 The built-in objects follow:

Object

Task

Request object

Get information from a user.

Response object

Send information to a user.

Server object

Control the ASP execution environment.

Session object

To store information about and change settings for the user's current Web-server session

Application object

To share application-level information and control settings for the lifetime of the application

  

3.2- Object Syntax:

 The syntax by which you gain access to an object depends on the scripting language you are using. The examples that appear in this guide use VBScript syntax, except where noted otherwise. The Request and Response objects contain collections. A collection is a set of related pieces ofinformation that are accessed the same way. You can also gain access to information in a collection by using the For...Each statement. This statement is useful in debugging scripts.You gain access to objects from a script by using methods and properties.

 

3.3- Using Methods:

 A method is a procedure that acts on an object. The general syntax is

 Object.Method parameters

 Where parameters may be a variant, data, a string, or a URL, depending on the method

 

 

3.4- Using Properties:

 A property is a named attribute of an object. Properties define object characteristics, such as size,color, and screen location; or the state of an object, such as enabled or disabled. The general syntax is

 Object.Property parameters

 Where parameters may be a value, string, or flag, depending on the property.

 

 3.5- The Response object:

 The Response object is used to send information to the user. The Response object supports only Cookies as a collection (to set cookie values). The Response object also supports a number of properties and methods. Properties currently supported are:

              Buffer—set to buffer page output at the server. When this is set to true, the server will not send a response until all of the server scripts on the current page have been processed, or until the Flush or End method has been called.

             ContentType—to set the type of content (i.e: text/HTML, Excel, etc…)

             Expires—sets the expiration (when the data in the user's cache for this Web page is considered invalid) based on minute  (i.e.: expires in 10 minutes   (     

            ExpiresAbsolute—allows you to set the expiration date to an absolute date and time.

            Status—returns the status line (defined in the HTTP specification   for the server).

 

   The following methods are supported by the Response object:

 

             AddHeader—Adds an HTML header with a specified value

             AppendToLog—Appends a string to the end of the Web server log file

             BinaryWrite—writes binary data (i.e, Excel spreadsheet data)

             Clear—clears any buffered HTML output.

             End—stops processing of the script.

             Flush--sends all of the information in the buffer.

             Redirect—to redirect the user to a different URL

             Write—to write into the HTML stream. This can be done by using the construct

              Response.write("hello")  or the shortcut command <%=”hello”%>

 

3.6- The Request object:

    The Request object is used to get information from the user that is passed along in an HTTP request. As I mentioned earlier, the

   Request and Response objects support collections:

             ClientCertificate—to get the certification fields from the request issued by the Web browser. The fields that you can reques are specified in the X.509 standard

             QueryString—to get text such as a name,

             Form—to get data from an HTML form

             Cookies—to get the value of application-defined cookie

             ServerVariables—to get HTTP information such as the server name

 

3.7- The Server object:

    The Server object supports one property, ScriptTimeout, which allows you to set the value for when the script processing will timeout, and the following methods:

              CreateObject—to create an instance of a server component. This component can be any component that you have installed on your server (such as an ActiveX.)

             HTMLEncode—to encode the specified string in HTML.

             MapPath—to map the current virtual path to a physical directory structure. You can then pass that path to a component that creates the specified directory or file on the server.

             URLEncode—applies URL encoding to a specified string.

 

3.8- The Session object:

    The Session object is used to store information about the current user's Web-server session. Variables stored with this object exist as long as the user's session is active, even if more than one application is used. This object supports one method,

         Abandon : which (believe it or not!) abandons the current Web-server session, destroying any objects, and supports two properties,

        SessionID :   containing the identifier for the current session

       Timeout :  specifying a time-out value for the session.

 This Object supports two events

          Session_OnEnd : The Session_OnEnd event occurs when a session is abandoned or times out. Of the server built-in objects, only the Application, Server, and Session objects are available.

          Session_OnStart : event occurs when the server creates a new session. This script is processed prior to executing the requested page. You must set any session-wide variables during the Session_OnStart event. All the built-in objects (Application, Request, Response, Server, and Session) are available and can be referenced in the Session_OnStart event script.

 One thing to bear in mind about the session identifier: It's not a GUID. It's only good as long as the current Web-server session is running. If you shut down the Web-server service, the identifiers will start all over again. So don't use it to create logon IDs, or you'll have a bunch of duplicates and one heck of a headache.

 

3.9- The Application object:

    The Application object can store information that persists for the entire lifetime of an application (a group of pages with a common root). Generally, this is the whole time that the IIS server is running. This makes it a great place to store information that has to exist for more than one user (such as a page counter). The downside of this is that since this object isn't created a new for each user, errors that may not show up when the code is called once may show up when it is called 10,000 times in a row. In addition ,because the Application object is shared by all the users, threading can be a nightmare to implement.

This object supports two methods

Lock : The Lock method prevents other clients from modifying Application object properties.

Unlock :The Unlock method allows other clients to modify Application object properties.

And supports two events

Application_OnEnd : event occurs when the application quits, after the

Session_OnEnd event. Only the Application and Server built-in objects are

available.

Application_OnStart : event occurs before the first new session is created, that is,before the Session_OnStart event. Only the Application and Server built-in objects are available. Referencing the Session, Request, or Response objects in the Application_OnStart event script causes an error.

 

Scripts for the preceding events are declared in the Global.asa file.(We will talk about this file latter in details)