Web Programming Unit-IV

Embed Size (px)

Citation preview

  • 8/6/2019 Web Programming Unit-IV

    1/16

    1

    http://www.saif4u.webs.com

    B.COM COMPUTER III YEAR - NOTES

    WEB PROGRAMMING

    INDEX UNIT - IV

    S.no: Title Page no:

    1. Active Server Pages (ASP) Introduction 02 - 03

    2. Scripting Languages and Script Engines 03 - 03

    3.

    i.ii.iii.

    iv.v.

    vi .vii.

    viii.ix.

    x.xi.xii.

    xiii.

    ASP Objects

    ASP ResponseASP RequestASP Application

    ASP SessionASP Server

    ASP ErrorASP F ileSystem

    ASP TextStreamASP Drive

    ASP FileASP Folder

    ASP DictionaryASP ADO

    04 - 13

    04 - 0505 - 0707 - 07

    07 - 0707 - 08

    08 - 0909 - 09

    09 - 1010 10

    11 - 1111 - 12

    12 - 1213 - 13

    4. ASP Application 14 - 16

    6. Conclusion 16 - 16

  • 8/6/2019 Web Programming Unit-IV

    2/16

    2

    http://www.saif4u.webs.com

    Active Server P ages (ASP) Introduction

    Active Server Pages (ASP), also known as Classic ASPor ASP Classic,

    was Microsoft's firstserver-sidescript-engine for dynamically-generated web pages.

    Initially released as an add-on toInternet Information Services (IIS) via the Windows NT

    4.0 Option Pack (ca 1998), it was subsequently included as a free component of

    Windows Server (since the initial release ofWindows 2000 Server). ASP.NET has

    superseded ASP.

    ASP 2.0 provided six built-inobjects: Application, ASPError, Request, Response,

    Server, and Session. Session, for example, represents a cookie-basedsession that

    maintains the state of variables from page to page. The Active Scripting engine's

    support of the Component Object Model (COM) enables ASP websites to access

    functionality in compiled libraries such as DLLs.

    Web pages with the .aspfile extension use ASP, although some web sites disguise their

    choice of scripting language for security purposes (e.g. still using the more

    common .htmor .htmlextension). Pages with the .aspxextension use

    compiled ASP.NET (based on Microsoft's .NET Framework), which makes them faster

    and more robust than server-side scripting in ASP, which is interpreted at run-time;

    however, ASP.NET pages may still include some ASP scripting. The introduction of

    ASP.NET led to use of the term Classic ASPfor the original technology.

    Programmers write most ASP pages using VBScript, but any other Active Scripting

    engine can be selected instead with the @Languagedirective or the syntax. JScript (Microsoft's implementation

    of ECMAScript) is the other language that is usually available. PerlScript (a derivative

    of Perl) and others are available as third-party installable Active Scripting engines.

    What is ASP?

    ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information Services IIS comes as a free component with Windows 2000 IIS is also a part of the Windows NT 4.0 Option Pack The Option Pack can be downloaded from Microsoft PWS is a smaller - but fully functional - version of IIS PWS can be found on your Windows 95/98 CD

  • 8/6/2019 Web Programming Unit-IV

    3/16

    3

    http://www.saif4u.webs.com

    ASP Compatibility

    To run IIS you must have Windows NT 4.0 or later To run PWS you must have Windows 95 or later ChiliASP is a technology that runs ASP without Windows OS

    InstantASP is another technology that runs ASP without Windows

    What is an ASP File?

    An ASP file is just the same as an HTML file An ASP file can contain text, HTML, XML, and scripts Scripts in an ASP file are executed on the server An ASP file has the file extension ".asp"

    How Does ASP Differ from HTML?

    When a browser requests an HTML file, the server returns the file When a browser requests an ASP file, IIS passes the request to the ASP engine.

    The ASP engine reads the ASP file, line by line, and executes the scripts in thefile. Finally, the ASP file is returned to the browser as plain HTML

    What can ASP do for you?

    Dynamically edit, change, or add any content of a Web page Respond to user queries or data submitted from HTML forms Access any data or databases and return the results to a browser Customize a Web page to make it more useful for individual users The advantages of using ASP instead of CGI and Perl, are those of simplicity

    and speed Provide security - since ASP code cannot be viewed from the browser Clever ASP programming can minimize the network traffic

    Note: Because ASP scripts are executed on the server, the browser that displaysthe ASP file does not need to support scripting at all!

    Scripting Languages and Script Engines

    We have seen that Active Server Pages are really a combination of VBScript and HTMLtags. Processing these scripts on the server generates the dynamic HTML that can berendered in any browser. VBScript is the default scripting language, although you coulduse JScript. All of our examples use the native VBScript. Everything within the iswritten in VBScript, which has almost identical syntax to VB.

  • 8/6/2019 Web Programming Unit-IV

    4/16

    4

    http://www.saif4u.webs.com

    ASP ObjectsASP ResponseASP RequestASP ApplicationASP Session

    ASP ServerASP ErrorASP FileSystemASP TextStreamASP DriveASP FileASP FolderASP DictionaryASP ADO

    ASP Response Object

    The ASP Response object is used to send output to the user from the server.The ASP Response object is used to send output to the user from the server. Itscollections, properties, and methods are described below:

    Collections

    Collection Description

    Cookies Sets a cookie value. If the cookie does not exist, it will be created,and take the value that is specified

    Properties

    Property DescriptionBuffer Specifies whether to buffer the page output or not

    CacheControl Sets whether a proxy server can cache the output generated byASP or not

    Charset Appends the name of a character-set to the content-type headerin the Response object

    ContentType Sets the HTTP content type for the Response object

    Expires Sets how long (in minutes) a page will be cached on a browserbefore it expires

    ExpiresAbsolute Sets a date and time when a page cached on a browser willexpire

    IsClientConnected Indicates if the client has disconnected from the server

    Pics Appends a value to the PICS label response header

    Status Specifies the value of the status line returned by the server

  • 8/6/2019 Web Programming Unit-IV

    5/16

    5

    http://www.saif4u.webs.com

    Methods

    Method Description

    AddHeader Adds a new HTTP header and a value to the HTTP response

    AppendToLog Adds a string to the end of the server log entry

    BinaryWrite Writes data directly to the output without any characterconversion

    Clear Clears any buffered HTML output

    End Stops processing a script, and returns the current result

    Flush Sends buffered HTML output immediately

    Redirect Redirects the user to a different URL

    Write Writes a specified string to the output

    ASP Request ObjectThe ASP Response object is used to send output to the user from the server.The ASP Response object is used to send output to the user from the server. Itscollections, properties, and methods are described below:

    Collections

    Collection Description

    Cookies Sets a cookie value. If the cookie does not exist, it will be created,and take the value that is specified

    Properties

    Property Description

    Buffer Specifies whether to buffer the page output or not

    CacheControl Sets whether a proxy server can cache the output generated byASP or not

    Charset Appends the name of a character-set to the content-type headerin the Response object

    ContentType Sets the HTTP content type for the Response object

    Expires Sets how long (in minutes) a page will be cached on a browserbefore it expires

    ExpiresAbsolute Sets a date and time when a page cached on a browser willexpire

    IsClientConnected Indicates if the client has disconnected from the server

    Pics Appends a value to the PICS label response header

    Status Specifies the value of the status line returned by the server

  • 8/6/2019 Web Programming Unit-IV

    6/16

    6

    http://www.saif4u.webs.com

    Methods

    Method Description

    AddHeader Adds a new HTTP header and a value to the HTTP response

    AppendToLog Adds a string to the end of the server log entry

    BinaryWrite Writes data directly to the output without any characterconversion

    Clear Clears any buffered HTML output

    End Stops processing a script, and returns the current result

    Flush Sends buffered HTML output immediately

    Redirect Redirects the user to a different URL

    The Request object is used to get information from a visitor.

    When a browser asks for a page from a server, it is called a request. The Request

    object is used to get information from a visitor. Its collections, properties, and methodsare described below:

    Collections

    Collection Description

    ClientCertificate Contains all the field values stored in the client certificate

    WriteWrites a specified string to the outputThe Request object is used to get information from a visitor.

    When a browser asks for a page from a server, it is called a request. The Requestobject is used to get information from a visitor. Its collections, properties, and methodsare described below:

    Collections

    Collection Description

    ClientCertificate Contains all the field values stored in the client certificate

    Cookies Contains all the cookie values sent in a HTTP request

    Form Contains all the form (input) values from a form that uses the postmethod

    QueryString Contains all the variable values in a HTTP query string

    ServerVariables Contains all the server variable values

    Properties

    Property Description

    TotalBytes Returns the total number of bytes the client sent in the body ofthe request

  • 8/6/2019 Web Programming Unit-IV

    7/16

    7

    http://www.saif4u.webs.com

    Methods

    Method Description

    BinaryRead Retrieves the data sent to the server from the client as part of apost request and stores it in a safe array

    ASP Application ObjectA group of ASP files that work together to perform some purpose is called anapplication. The Application object is used to tie these files together.

    An application on the Web may consists of several ASP files that work together toperform some purpose. The Application object is used to tie these files together.

    The Application object is used to store and access variables from any page, just like theSession object. The difference is that ALL users share ONE Application object (withSessions there is ONE Session object for EACH user).

    The Application object holds information that will be used by many pages in theapplication (like database connection information). The information can be accessedfrom any page. The information can also be changed in one place, and the changes willautomatically be reflected on all pages.

    ASP Session ObjectA Session object stores information about, or change settings for a user session.

    When you are working with an application on your computer, you open it, do some

    changes and then you close it. This is much like a Session. The computer knows whoyou are. It knows when you open the application and when you close it. However, onthe internet there is one problem: the web server does not know who you are and whatyou do, because the HTTP address doesn't maintain state.

    ASP solves this problem by creating a unique cookie for each user. The cookie is sentto the user's computer and it contains information that identifies the user. This interfaceis called the Session object.

    The Session object stores information about, or change settings for a user session.

    Variables stored in a Session object hold information about one single user, and areavailable to all pages in one application. Common information stored in sessionvariables are name, id, and preferences. The server creates a new Session object foreach new user, and destroys the Session object when the session expires.

    ASP Server Object

    The Server object is used to access properties and methods on the server.

  • 8/6/2019 Web Programming Unit-IV

    8/16

    8

    http://www.saif4u.webs.com

    The ASP Server object is used to access properties and methods on the server. Itsproperties and methods are described below:

    Properties

    Property Description

    ScriptTimeout Sets or returns the maximum number of seconds a script can runbefore it is terminated

    Methods

    Method Description

    CreateObject Creates an instance of an object

    Execute Executes an ASP file from inside another ASP file

    GetLastError() Returns an ASPError object that describes the error condition thatoccurred

    HTMLEncode Applies HTML encoding to a specified stringMapPath Maps a specified path to a physical path

    Transfer Sends (transfers) all the information created in one ASP file to asecond ASP file

    URLEncode Applies URL encoding rules to a specified string

    ASP ASPError Object

    The ASPError object was implemented in ASP 3.0 and is available in IIS5 and later.

    The ASPError object is used to display detailed information of any error that occurs inscripts in an ASP page.

    Note: The ASPError object is created when Server.GetLastError is called, so the errorinformation can only be accessed by using the Server.GetLastError method.

    The ASPError object's properties are described below (all properties are read-only):

    Properties

    Property Description

    ASPCode Returns an error code generated by IIS

    ASPDescription Returns a detailed description of the error (if the error is ASP-related)

    Category Returns the source of the error (was the error generated by ASP?By a scripting language? By an object?)

    Column Returns the column position within the file that generated the

  • 8/6/2019 Web Programming Unit-IV

    9/16

    9

    http://www.saif4u.webs.com

    error

    Description Returns a short description of the error

    File Returns the name of the ASP file that generated the error

    Line Returns the line number where the error was detected

    Number Returns the standard COM error code for the error

    Source Returns the actual source code of the line where the erroroccurred

    ASP FileSystemObject Object

    The FileSystemObject object is used to access the file system on a server.

    The FileSystemObject object is used to access the file system on a server.

    This object can manipulate files, folders, and directory paths. It is also possible toretrieve file system information with this object.

    The following code createsa text file (c:\test.txt) and then writes some text to the file:

    The FileSystemObject object's properties and methods are described below:

    Properties

    Property Description

    Drives Returns a collection of all Drive objects on the computer

    ASP TextStream ObjectThe TextStream object is used to access the contents of a text file.

    The TextStream object is used to access the contents of text files.

    The following code creates a text file (c:\test.txt) and then writes some text to the file(the variable f is an instance of the TextStream object):

  • 8/6/2019 Web Programming Unit-IV

    10/16

    10

    http://www.saif4u.webs.com

    To create an instance of the TextStream object you can use the CreateTextFile orOpenTextFile methods of the FileSystemObject object, or you can use theOpenAsTextStream method of the File object.

    The TextStream object's properties and methods are described below:

    Properties

    Property Description

    AtEndOfLine Returns true if the file pointer is positioned immediately before theend-of-line marker in a TextStream file, and false if not

    AtEndOfStream Returns true if the file pointer is at the end of a TextStream file,and false if not

    Column Returns the column number of the current character position in aninput stream

    Line Returns the current line number in a TextStream file

    ASP Drive ObjectThe Drive object is used to get information about a local disk drive or a network share.

    The Drive object is used to return information about a local disk drive or a networkshare. The Drive object can return information about a drive's type of file system, freespace, serial number, volume name, and more.

    Note: You cannot return information about a drive's content with the Drive object. Forthis purpose you will have to use the Folder object.

    To work with the properties of the Drive object, you will have to create an instance of theDrive object through the FileSystemObject object. First; create a FileSystemObjectobject and then instantiate the Drive object through the GetDrive method or the Drivesproperty of the FileSystemObject object.

  • 8/6/2019 Web Programming Unit-IV

    11/16

    11

    http://www.saif4u.webs.com

    ASP File Object

    The File object is used to return information about a specified file.

    To work with the properties and methods of the File object, you will have to create an

    instance of the File object through the FileSystemObject object. First; create aFileSystemObject object and then instantiate the File object through the GetFile methodof the FileSystemObject object or through the Files property of the Folder object.

    The following code uses the GetFile method of the FileSystemObject object toinstantiate the File object and the DateCreated property to return the date when thespecified file was created:

    Example

    ASP Folder Object

    The Folder Object is used to return information about a specified folder.

    The Folder object is used to return information about a specified folder.

    To work with the properties and methods of the Folder object, you will have to create aninstance of the Folder object through the FileSystemObject object. First; create aFileSystemObject object and then instantiate the Folder object through the GetFoldermethod of the FileSystemObject object.

    The following code uses the GetFolder method of the FileSystemObject object toinstantiate the Folder object and the DateCreated property to return the date when the

    specified folder was created:

  • 8/6/2019 Web Programming Unit-IV

    12/16

    12

    http://www.saif4u.webs.com

    set fo=nothingset fs=nothing%>

    Output:

    Folder created: 10/22/2008 10:01:19 AM

    ASP Dictionary ObjectThe Dictionary object stores information in name/value pairs.

    The Dictionary object is used to store information in name/value pairs (referred to askey and item). The Dictionary object might seem similar to Arrays, however, theDictionary object is a more desirable solution to manipulate related data.

    Comparing Dictionaries and Arrays:

    Keys are used to identify the items in a Dictionary object You do not have to call ReDim to change the size of the Dictionary object When deleting an item from a Dictionary, the remaining items will automatically

    shift up Dictionaries cannot be multidimensional, Arrays can Dictionaries have more built-in functions than Arrays Dictionaries work better than arrays on accessing random elements frequently Dictionaries work better than arrays on locating items by their content

    The following example creates a Dictionary object, adds some key/item pairs to it, and

    retrieves the item value for the key gr:

    Output:

    The value of key gr is: Green

  • 8/6/2019 Web Programming Unit-IV

    13/16

    13

    http://www.saif4u.webs.com

    ASP ADO

    ADO can be used to access databases from your web pages.

    Accessing a Database from an ASP Page

    The common way to access a database from inside an ASP page is to:

    1. Create an ADO connection to a database2. Open the database connection3. Create an ADO recordset4. Open the recordset5. Extract the data you need from the recordset6. Close the recordset

    7. Close the connection

    What is ADO?

    ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a Microsoft Active-X component ADO is automatically installed with Microsoft IIS ADO is a programming interface to access data in a database

  • 8/6/2019 Web Programming Unit-IV

    14/16

    14

    http://www.saif4u.webs.com

    ASP Application

    A group of ASP files that work together to perform some purpose is called anapplication.

    Application Object

    An application on the Web may consists of several ASP files that work together toperform some purpose. The Application object is used to tie these files together.

    The Application object is used to store and access variables from any page, just like theSession object. The difference is that ALL users share ONE Application object (withSessions there is ONE Session object for EACH user).

    The Application object holds information that will be used by many pages in theapplication (like database connection information). The information can be accessedfrom any page. The information can also be changed in one place, and the changes willautomatically be reflected on all pages.

    Store and Retrieve Application Variables

    Application variables can be accessed and changed by any page in an application.

    You can create Application variables in "Global.asa" like this:

    Sub Application_OnStart

    application("vartime")=""application("users")=1End Sub

    In the example above we have created two Application variables: "vartime" and "users".

    You can access the value of an Application variable like this:

    There are

    active connections.

  • 8/6/2019 Web Programming Unit-IV

    15/16

    15

    http://www.saif4u.webs.com

    Loop Through the Contents Collection

    The Contents collection contains all application variables. You can loop through theContents collection, to see what's stored in it:

    If you do not know the number of items in the Contents collection, you can use theCount property:

    Loop Through the StaticObjects Collection

    You can loop through the StaticObjects collection, to see the values of all objects storedin the Application object:

    Lock and Unlock

    You can lock an application with the "Lock" method. When an application is locked, theusers cannot change the Application variables (other than the one currently accessingit). You can unlock an application with the "Unlock" method. This method removes thelock from the Application variable:

  • 8/6/2019 Web Programming Unit-IV

    16/16

    16

    http://www.saif4u.webs.com