asp objects active server pages (cont..) 1. 2 asp : objects asp provides built-in objects for...

27
ASPObjec ts Active Server Pages(cont..) 1

Upload: gabriel-neal

Post on 17-Jan-2016

223 views

Category:

Documents


5 download

TRANSCRIPT

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

1

ASPObjectsActive Server Pages(cont..)

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

2

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

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.

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

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

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

5

Object PropertySyntax: Object.PropertyProperties of an Object can be set by using the assignment operator “=“

Object MethodSyntax for calling a method : Object.Method parametersEg. Response.write “Hello World!”

Object ParameterMethod

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

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.

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

7

Request Object

Response Object

Session ObjectClient

Request

Server

ASP Error Object

Server Object

Server Response

Application Object

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

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 Object Response 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.

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

9

Properties of Response ObjectProperty Syntax Description

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.

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

10

Methods of Response Object

Method DescriptionAddHeader Sets the HTML header name to value.AppendToLog Adds a string to the end of the Web server log entry for this

request.BinaryWrite Writes the given information to the current HTTP output

without any character-set conversion.Clear Erases any buffered HTML output.End Stops processing the .asp file and returns the current result.Flush Sends buffered output immediately.Redirect Sends a redirect message to the browser, causing it to attempt

to connect to a different URLWrite Writes a variable or text to the current HTTP output as a string.

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

11

Methods of Response Object

Method SyntaxAddHeader Response.AddHeader HeaderName, HeaderValueAppendToLog Response.AppendToLog string

e.g.,<% Response.AppendToLog “Unauthorised Access, logging out!!!” %>

BinaryWrite Response.BinaryWrite dataClear Response.ClearEnd Response.EndFlush Response.FlushRedirect Response.Redirect URL

e.g., <% Response.Redirect http://www.cbse.nic.in/index.html %>Write Response.Write variant

e.g., <% Response.write Date %>

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

12

Difference between Response.Clear and Response.Flush

Response.Clear Response.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.

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

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.

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

14

<HTML><HEAD><TITLE> Form </TITLE></HEAD><BODY><FORM ACTION = “nextpage.asp” METHOD = “post”><P> Your First Name : <INPUT NAME = “fname” SIZE = 20 TYPE = “text” ><P> What is your favourite colour : <SELECT NAME=“fcolour”> <OPTION> Red <OPTION> Blue <OPTION> Yellow <OPTION> Green </SELECT><P> <INPUT TYPE= SUBMIT></FORM></BODY></HTML>

<HTML><HEAD><TITLE> Next Page </TITLE></HEAD><BODY> Welcome , <B> <%= Request.Form(“fname”) %> </B>Your favourite colour is <B> <%= Request.Form (“fcolour”) %> </B>.</BODY><HTML>

Form contains a text field fname,

a selection list fcolour and a submit button

Firstpage.asp

Nextpage.asp

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

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.

<HTML><HEAD><TITLE>Choice </TITLE></HEAD><BODY><P> What is your favourite colour ? </P><P> <A HREF =“choicenext.asp ? Favcolour =Red”> Red </A> </P><P> <A HREF =“choicenext.asp ? Favcolour =Blue”>Blue </A> </P><P> <A HREF =“choicenext.asp ? Favcolour =Green”> Green </A> </P></BODY></HTML>

Choice.asp

<HTML><HEAD><TITLE>Choice Next </TITLE></HEAD><BODY><% fcol = Request.QueryString(“Favcolour”) %>Your favourite colour is <FONT color=<%=fcol%> > <% = fcol % > ! </font></BODY></HTML>

Choicenext.asp

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

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.

<HTML><BODY><P> Browser Used:<% Response.write Request.ServerVariables(“HTTP_USER_AGENT“) %> </p><P> IP address of client:<% Response.write Request.ServerVariables(“REMOTE_ADDR“) %> </p><P> DNS Host:<% Response.write Request.ServerVariables(“REMOTE_HOST“) %> </p><P> Host Server:<% Response.write Request.ServerVariables(“SERVER_NAME“) %> </p></BODY></HTML>

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

17

Cookies CollectionA 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 CookiesResponse.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 cookieRequest.Cookies (cookie) [ .attribute]

Name Description

Domain Write-only, if specified, the cookie is sent only to this domain

Expires Write-only, the date on which the cookie expires.

HashKeys Read-only, specifies if the cookie contains key

Path Write-only, if specified, the cookie is sent to this address

Secure Write-only, specifies whether the cookie is secure

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

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

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

19

<%@ language = “VBSCRIPT” %><% LastAccessDate = Request.Cookies (“LastDate”) %><% LastAccessTime = Request.Cookies (“LastTime”) %><%If (request.cookies (“NumVisits”)=” “) Then Response.Cookies (“NumVisits”) = 0Else Response.Cookies (“NumVisits”) = Request.Cookies (“NumVisits”) + 1End if%><% Response.Cookies (“LastDate”) = Date %><% Response.Cookies (“LastTime”) = Time %> <HTML><HEAD><TITLE>Using Cookies in ASP</TITLE></HEAD><BODY><H3> Welcome to ASP with cookies </H3><CENTER style=”border:double;background-color=pink”><% IF (Request.Cookies(“Numvisits”)=0) then%><P> Welcome!<br> First time you are visiting this page! <br> </P><% ELSE %><P> Thanks for visiting this page again! You have visited this page a total of <%= Request.Cookies(“NumVisits”) %> times. </P><% END IF%></CENTER><HR>Time is <% = Time %> Date is <%= Date %> <BR><% IF (Request.Cookies(“NumVisits”)>0) THEN %>You last accessed this web page on <% = LastAccessDate %> at <% =LastAccessTime %><% End If %><HR></BODY></HTML>

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

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.

<HTML><HEAD><TITLE> Application Variable </TITLE></HEAD><BODY><%Application (“Greetings”) = “Hello!”%><%=Application (“Greetings”) %></BODY></HTML>

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.

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

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.<%=Application.Contents.Count%>

Methods of Application ObjectMethod Description

Lock Blocks 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.

UnLock Removes the lock from the variables stored in the Application object.

Contents.Remove Removes an item from the Contents collection

Contents.RemoveAll Removes all items from the Contents collection

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

22

Example:<% Application.LockClickCount=Application(“BannerClick”)ClickCount=ClickCount+1Application(“BannerClick”) = ClickCountApplication.Unlock%>

Remove Method deletes an item from a collectionSyntax : 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 collectionSyntax : 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

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

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:

<HTML><HEAD><TITLE>Session Example</TITLE></HEAD><BODY><%Session(“UserName”) = “john”Session(“Age”)=17%></BODY></HTML>

2 session variables UserName and Age

are initialized.

Page1.asp

<HTML><HEAD><TITLE>Second Page</TITLE></HEAD><BODY>Welcome <%Session(“UserName”) %>You are <%Session(“Age”)%> years old.</BODY></HTML>

Page2.asp

When user views Page2.asp it will

display user’s name and age that was

assigned in Page1.asp

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

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 ObjectAbandon – 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.

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

25

Server ObjectServer 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 = <time> where <time> 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)

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

26

Examples

First.asp<HTML> <HEAD> <TITLE> FIRST PAGE</TITLE></HEAD><BODY><% Response.write “<H2>Welcome to First Page</H2>%></BODY></HTML>

Second.asp<HTML><HEAD><TITLE> SECOND PAGE </TITLE></HEAD><BODY><% Response.write “<H2>This is Second Page</H2>%></BODY></HTML>

Server_transfer.asp<HTML> <HEAD> <TITLE> Executing and Transfer </TITLE></HEAD><BODY><%Response.write “Original Page”Server.Execute “First.asp”Response.write “<BR>Back to original Page<BR>Server.Transfer “Second.asp”Response.write “<BR>Once again back to original Page<BR></BODY></HTML>

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

27

ASPError ObjectThis 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.