Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASP Objects Active Server Pages (cont..) 1. 2 ASP : Objects ASP provides built-in objects for performing useful tasks that simplify web development.

Similar presentations


Presentation on theme: "ASP Objects Active Server Pages (cont..) 1. 2 ASP : Objects ASP provides built-in objects for performing useful tasks that simplify web development."— Presentation transcript:

1 ASP Objects Active Server Pages (cont..) 1

2 2

3 ASP : Objects ASP provides built-in objects for performing useful tasks that simplify web development. The built-in objects are automatically created for the user when the ASP request starts processing. The user can access the methods and properties of a built-in object to control sessions and web-server applications. 3

4 ASP-Objects An object has methods, properties and collections. – Methods determine the things that one can do with the object – Property is an attribute that describes an object – Collection constitutes different sets of key and value pairs related to the object 4

5 Object Property Syntax: Object.Property Properties of an Object can be set by using the assignment operator “=“ 5 Object Method Syntax for calling a method : Object.Method parameters Eg. Response.write “Hello World!” ObjectParameterMethod

6 ASP provides the following built-in-objects: 1.Application Object : To share information among all users of an application. 2.Request Object : To gain access to any information that is passed with an HTTP request the browser to the server. This includes parameters passed from an HTML form using either GET or POST methods and cookies etc. 3.Response Object : To control the information sent back to the browser from the server. 4.Server Object : This provides access to methods and properties on the server. Server object can be used to control script execution time, to create other objects. 5.Session Object : To store information required for a particular user session. 6.ASPError Object : To trap ASP errors and return more informative description to the users. 6

7 7 Request Object Response Object Session Object Client Request Server ASP Error Object Server Object Server Response Application Object

8 8 RESPONSE OBJECT Response and Request objects provide the user with complete control over how the Web server communicates with Web browsers. When a Web server receives a request, it returns an HTTP response. If the Web server has problems with the request, it returns an error and a description of the error in the status line. Request ObjectResponse Object An object holding information that the client sends to the server as an HTTP request. An object holding information that the server sends to the client as an HTTP response. Contains the Cookies, Form, QueryString and ServerVariables collection. Contains the Cookies collection.

9 Properties of Response Object 9 PropertySyntaxDescription Buffer Response.Buffer [=TRUE | FALSE] False indicates no buffering and is the default. True indicates buffering. Indicates whether the page output is buffered or not. Content Type Response.ContentType [=ContentType] Content type is a string describing the content type in the format type/sub-type, eg., “text/HTML” or “image/GIF” By default the content type is text/HTML Expires Response.Expires [=number] number is the time in minutes before the page expires. Expires Absolute Response.ExpiresAbsolute [ = [date] [time]] If time is not mentioned then the page expires at midnight. The date / time at which a page cached on a browser expires. If date is not mentioned then expires at the time on the day the script is run. Status Response.Status=[StatusDescription] Specifies the value of the status line returned by the server.

10 Methods of Response Object 10 MethodDescription AddHeaderSets the HTML header name to value. AppendToLogAdds a string to the end of the Web server log entry for this request. BinaryWriteWrites the given information to the current HTTP output without any character-set conversion. ClearErases any buffered HTML output. EndStops processing the.asp file and returns the current result. FlushSends buffered output immediately. RedirectSends a redirect message to the browser, causing it to attempt to connect to a different URL WriteWrites a variable or text to the current HTTP output as a string.

11 Methods of Response Object 11 MethodSyntax AddHeader Response.AddHeader HeaderName, HeaderValue AppendToLog Response.AppendToLog string e.g., BinaryWrite Response.BinaryWrite data ClearResponse.Clear EndResponse.End FlushResponse.Flush RedirectResponse.Redirect URL e.g., http://www.cbse.nic.in/index.html WriteResponse.Write variant e.g.,

12 12 Difference between Response.Clear and Response.Flush Response.ClearResponse.Flush Clear method clears any buffered HTML output. The Flush method sends buffered HTML output immediately. Buffer is cleared without contents being displayed. Displays output on screen and then buffer is cleared.

13 13 REQUEST OBJECT All communication between a browser and a Web server takes place in discrete Request-Response pair. An HTTP request contains a Header line, header fields and a message. Syntax : Request.[Collection] (variable) Form Collection : The FORM collection retrieves the values of form elements posted to the HTTP request body, with a form using POST method Syntax : Request.Form (element) [ (index) |.Count ] Where element is the name of the form element from which the collection is to retrieve values.

14 14 Form Your First Name : What is your favourite colour : Red Blue Yellow Green Next Page Welcome, Your favourite colour is. Form contains a text field fname, a selection list fcolour and a submit button Firstpage.asp Nextpage.asp

15 15 The QueryString Collection Syntax : Request.QueryString (Variable) [ (index) |.Count] where Variable specifies the name of the variable in the HTTP query string to retrieve. Choice What is your favourite colour ? Red Blue Green Choice.asp Choice Next Your favourite colour is > ! Choicenext.asp

16 16 ServerVariables Collection The ServerVariable collection retrieves the values of pre-determined environment variables. Syntax : Request.ServerVariables (server environment variable) The server environment variable can be one of the following: HTTP_USER_AGENT – returns a string describing the browser that sent the request. REMOTE_ADDR – the IP address of the remote host making the request., i.e., the IP address of the client. REMOTE_HOST – the name of the host making the request. SERVER_NAME – server’s host name, DNS alias or IP address as it would appear in self-referencing URLs. Browser Used: IP address of client: DNS Host: Host Server:

17 17 Cookies Collection A Cookie is an ASP object that allows a programmer to store user data either on the Web server or on the Client’s computer in C:\Windows\Cookies for retrieval and use later. When a visitor opens a page, the script instructs the browser to create the cookie. Creating Cookies Response.Cookies (cookie) [.attribute] = value Where cookie is the name of the cookie, attribute specifies information about the cookie itself, this can be one of the following. Retrieving values from a cookie Request.Cookies (cookie) [.attribute] NameDescription DomainWrite-only, if specified, the cookie is sent only to this domain ExpiresWrite-only, the date on which the cookie expires. HashKeysRead-only, specifies if the cookie contains key PathWrite-only, if specified, the cookie is sent to this address SecureWrite-only, specifies whether the cookie is secure

18 18 <% Response.Cookie (“Location “) = “India” Response.Cookie (“Location “).Expires = “December 31,2013” %> Creates a cookie named Location and assigns the value India and expires on December 31,2013

19 19 <% If (request.cookies (“NumVisits”)=” “) Then Response.Cookies (“NumVisits”) = 0 Else Response.Cookies (“NumVisits”) = Request.Cookies (“NumVisits”) + 1 End if %> Using Cookies in ASP Welcome to ASP with cookies Welcome! First time you are visiting this page! Thanks for visiting this page again! You have visited this page a total of times. Time is Date is 0) THEN %> You last accessed this web page on at

20 20 Application Object A group of related Active server Pages is called an application. This group of ASP files work together to perform a specific task. The Application Object in ASP bind together these files. The Application Object is used to store and retrieve information that can be shared among all users of an application. Application Variable <% Application (“Greetings”) = “Hello!” %> A new application variable Greetings is created and assigned the value Hello! The value of the application variable is outputted to the browser Once the application variable has been assigned a value, it retains that value until the Web server is shut down or the application is unloaded.

21 21 To initialize variables in the Application object, the information about them is stored in a special ASP file (optional) named global.asa which contains the information that is global to the application. An application can have only one global.asa file and it is stored in the root directory of the application. Application Object Collections: contents : contains all items that have been added to the application through a script command Syntax : Application.contents (“Key”) where Key specifies the name of the item to retrieve. The number of elements in the collection can be retrieved by using Count property. Methods of Application Object MethodDescription LockBlocks other clients from modifying the variables stored in the Application Object, ensuring only one client at a time can alter or access the Application variable. UnLockRemoves the lock from the variables stored in the Application object. Contents.RemoveRemoves an item from the Contents collection Contents.RemoveAllRemoves all items from the Contents collection

22 22 Example: <% Application.Lock ClickCount=Application(“BannerClick”) ClickCount=ClickCount+1 Application(“BannerClick”) = ClickCount Application.Unlock %> Remove Method deletes an item from a collection Syntax : Application.Contents.Remove ( name | index ) Where name is the identifier for the item to be removed, index is an index offset indicating which item in the list to be removed Remove All Method deletes all the items from a collection Syntax : Application.Contents.RemoveAll Application(“FirstVar”) = “apples” Application(“SecondVar”) = “oranges” Application.Contents.Remove(“FirstVar”) Will create 2 application variables FirstVar and SecondVar then removes the variable FirstVar. Application(“FirstVar”) = “apples” Application(“SecondVar”) = “oranges” Application.Contents.RemoveAll() Will create 2 application variables FirstVar and SecondVar then removes all the variables in the collection

23 23 Session Object Session starts when the user requests a page from a web-site, the Web server automatically creates a Session object and end soon after the user leaves. Session Variables: Session Example <% Session(“UserName”) = “john” Session(“Age”)=17 %> 2 session variables UserName and Age are initialized. Page1.asp Second Page Welcome You are years old. Page2.asp When user views Page2.asp it will display user’s name and age that was assigned in Page1.asp

24 24 Properties of Session Object SessionID – Session.SessionID - Returns the session identifier, a unique identifier that is generated by the server when the session is created. TimeOut – Session.TimeOut – Specifies the time out period assigned to the Session object for the application. Methods of Session Object Abandon – Session.Abandon – Destroys all the objects stored in the Session Contents.Remove – Session.Contents.Remove [Item / Index] – Removes a specific item from the Session object’s Contents collection. Contents.RemoveAll – Session.Contents.RemoveAll() – Removes all items from the Session object’s Contents collection.

25 25 Server Object Server object enables the use of the various utility functions on the server like creating instances of other objects. Property of Server Object: ScriptTimeOut – Server.ScriptTimeOut = where represents maximum number of seconds the script can execute before the server terminates it, default time is 90 seconds. Methods of the Server Object: 1.CreateObject: Creates an instance of the server component. This instance as page scope, automatically destroyed by the server after processing of the current ASP page. ServerCreateObject(progID), where progID specifies the type of object to create. 2. Execute: Executes an.asp file. Server.Execute(path), path is the location of the.asp file. 3. GetLastError: Returns an ASPError object that describes the error condition. Server.GetLastError() 4. Transfer: Sends all the current information to another.asp file for processing Server.Transfer (path)

26 26 Examples First.asp FIRST PAGE <% Response.write “ Welcome to First Page %> Second.asp SECOND PAGE <% Response.write “ This is Second Page %> Server_transfer.asp Executing and Transfer <% Response.write “Original Page” Server.Execute “First.asp” Response.write “ Back to original Page Server.Transfer “Second.asp” Response.write “ Once again back to original Page

27 27 ASPError Object This object is used to obtain information about an error condition that has occurred in the script in ASP page. ASPError object is returned by the Server.GetLastError method. Properties of ASPError Object: ASPCode – ASPError.ASPCode()- Returns the ASP error code Number - ASPError.Number()- Returns the COM error code Source – ASPError.Source() – Returns the portion of the code that caused the error. Category - ASPError.Category() – Returns whether the error is occurred by the script or the object of the ASP. File - ASPError.File() - Returns the name of file in which the error is located. Line - ASPError.Line() - Returns the line number of the script where the error is located. Column - ASPError.Column() – Returns the column within the file where the error is located. Description - ASPError.Description() - Returns short description of the error. ASP Description - ASPError.ASPDescription() - Returns detailed description of the error.


Download ppt "ASP Objects Active Server Pages (cont..) 1. 2 ASP : Objects ASP provides built-in objects for performing useful tasks that simplify web development."

Similar presentations


Ads by Google