Wednesday, August 4, 2010

Objects Of Classic ASP

ASP provided six built-in objects:
• Application
• Request
• Response
• Session
• Server
• ASPError

Application Object:

This Object is used to store information that can be shared among all the users of an application.
Because the Application object can be shared by more than one user, there are Lock and Unlock methods to ensure that multiple users do not try to alter a property simultaneously.
Lock: Locks the application object so that only one user at a time can modify the values.
Unlock: Unlocks the application object allowing other users to modify application level variables.
The following example uses the application variable NumVisits to store the number of times that a particular page has been accessed. The Lock method is called to ensure that only the current client can access or alter NumVisits. Calling the Unlock method then enables other users to access the Application object.
Application.Lock Application("NumVisits") = Application("NumVisits") + 1
This application page has been visited
<%= Application("NumVisits") %> times!
Application object Support two events
• Application_OnEnd
• Application_OnStart

This event occurs when the IIS is shut down after Session_OnEnd event. All the variables are destroyed after that.
This event occurs when the first .asp page is called after starting the IIS. Application level variables can be declared here.
Request Object

Request object is used to retrieve all the information sent in a request from client browser to web web server 
 Request Object Support following Collections
  • Cookies
  • Form
  • QueryString
  • ServerVariables
  • ClientCertificate   
The Cookies collection enables you to retrieve the values of the cookies sent in an HTTP request.
Cookies should never be used to store secure data, such as passwords. Cookies are transmitted as clear text. If a malicious user taps an Internet connection, then they can take cookie data to impersonate a client and gain access to their data. 

Cookies Will be described in detail in my next post.
The Form collection retrieves the values of form elements posted to the HTTP request body, with a form using the POST method.
The QueryString collection retrieves the values of the variables in the HTTP query string. The HTTP query string is specified by the values following the question mark (?). Several different processes can generate a query string.
Query strings are also generated by sending a form or by a user typing a query into the address box of the browser
All variables can be accessed directly by calling Request(variable) without the collection name. In this case, the Web server searches the collections in the following order:Compose
  • QueryString
  • Form
  • Cookies
  • ClientCertificate
  • ServerVariables
If a variable with the same name exists in more than one collection, the Request object returns the first instance that the object encounters.
It is strongly recommended that when referring to members of a collection the full name be used. For example, rather than Request.("AUTH_USER") use Request.ServerVariables("AUTH_USER"). This allows the server to locate the item more quickly.
The ServerVariables collection retrieves the values of predetermined environment variables
Response Object 

We can use the Response object to send output back to the client from the Web Server.
The Response object   defines different  methods but most of the commonly used methods are as follows .
  • BinaryWrite : Writes the given information to the current HTTP output without any character-set conversion.
  • Redirect :Sends a redirect message to the browser, causing it to attempt to connect to a different URL.
  • Write:Writes a variable or text to the current HTTP output as a string.
  • End : Stops processing the .asp file and returns the current result.
Buffer :Indicates whether page output is buffered.
ContentType :Specifies the HTTP content type for the response.

Session Object

You can use the Session object to store information needed for a particular user session. Variables stored in the Session object are not discarded when the user jumps between pages in the application; instead, these variables persist for the entire user session.
The server destroys the Session object when the session expires or is abandoned.
  • Abandon :This method destroys a Session object and releases its resources.
  • Contents.RemoveAll :This method deletes an item from the Contents collection.
  • Contents.Remove :This method deletes all items from the Contents collection.

The Session object defines the following properties.
SessionID :Returns the session identification for this user.
Timeout: The time-out period for the session state for this application, in minutes

Session_OnEnd Event 
Session_OnStart Event
Server Object

We can use the Server object to access various utility functions of server.
CreateObject :The CreateObject method creates an instance of a server component.
GetLastError : The GetLastError method returns an ASPError Object describing the error condition that occurred. This method is available only before the .asp file has sent any content to the client.

Example Code
The following three examples demonstrate different errors that generate a 500;100 custom error. The three types of errors are:
• Preprocessing errors
• Script compiling errors
• Run-time errors
The following example demonstrates a preprocessing error, which IIS generates when it tries to include the file. This error will be generated because the #include statement is missing the file parameter for the #include statement.
<% response.write "hello" %>

The following example demonstrates a script compiling error. The scripting engine does not compile this script because it is missing the keyword next in a For...Next loop.

<% dim I for i=1 to 1 nxt %>
The following example demonstrates a run-time error that occurs because the script attempts to divide by 0.
<% dim i,j dim sum sum=0 j=0 for i=1 to 10 sum=sum+1 next sum=sum/j %>

If a 500;100 custom error has been defined for an ASP application, it may refer to an .asp file. In this case, when an error occurs during the running of an .asp file within the application, the server automatically transfers to this ASP page via the Server.Transfer method. All of the state information from the executing ASP application will be available to the .asp file that is handling the error. In addition, the ASPError Object will be available, so you can expose the properties of the error through the .asp file that you set up to handle the error.

The default Web site is configured to use the file \iishelp\common\500-100.asp. You can either use this file for processing ASP errors, or create your own. If you want to change the .asp file for processing the 500;100 custom errors you can use IIS Manager.
A 500;100 custom error will be generated if IIS encounters an error while processing either an .asp file or the application's Global.asa file.
HTMLEncode : Applies HTML encoding to the specified string.
MapPath : Maps the specified virtual path, either the absolute path on the current server or the path relative to the current page, into a physical path.
Transfer : Sends all of the current state information to another .asp file for processing.

ScriptTimeout :The amount of time that a script can run before it times out.

ASPError Object

You can use the ASPError object to obtain information about an error condition that has occurred in script in an ASP page. The ASPError object is returned by the Server.GetLastError method. The ASPError object exposes read-only properties.
You can use the ASPError object in an ASP page that is configured to respond to an HTTP error. For more information about configuring custom errors, see Creating Custom Error Messages.
The ASPError object has no methods.
The ASPError object defines the following properties.
Returns an error code generated by IIS.
Returns a more detailed description of the error, if it is an ASP-related error
Indicates if the source of the error was internal to ASP, the scripting language, or an object.
Indicates the column position within the .asp file that generated the error.
Returns a short description of the error.
Indicates the name of the .asp file that was being processed when the error occurred.
Indicates the line within the .asp file that generated the error.
Returns the standard COM error code.
Returns the actual source code, when available, of the line that caused the error.


1 comment:

  1. thanks sir for the providing the knowledge of objects of ASP.