Information Technology

                      Active Server Pages  

                              Visual Basic | Asp | Excel VBA | Sql Server | Oracle |Business Objects
                                      The material that appears on this site is for informational purposes only.           
                   We are not responsible for any wrong Question/answer,  use these information at your own risk.
                                                                         Knowledge Base for ASP
 
 ASP Home      
 ASP FAQ    
 

Internet Information Server(IIS)

IIS provides the ability to provide web services, not only for web pages, but also for the ftp sites(ability to transfer whole files from one site to another) , video and audio services.

It integrate with the database facilities of SQL server, the management facilities of Site Server, and the e-mailing and messaging abilities of Exchange.

Personal Web Server

PWS is Microsoft slimmed-down web server, which provides a basis on which to develop corporate network applications.

You'll find that Personal Web Server installs most of its bit and pieces on your hard drive under the InetPub directory.If you expand this directory, you'll find that it contains four subdirectories:

a) IISSamples : You'll find two directories under here. The first is default: this holds a default home page. Second is home page which contains some example ASP pages.

b) Scripts : This is an empty directory, which is a useful place to store any ASP scripts you might create.

c) WebPub : This is also empty. This is a 'Special Virtual directory, used for publishing files via the publish wizard.

d) WWWroot : This is the top of the tree for your web site. This should be your default web directory, this is where you should create any physical directories on your web site.

Virtual Directories

When you add a directory, you must specify the directory path; you must also add an alias, which is known as virtual directory. Virtual directories are used as aliases for directory paths on the server.

Virtual directories also provide a very useful way of protecting the users of your site from the necessity of typing an extremly long URL name each time they want to access a particular page on your site.

For ex: http://mycoolpage/onedirectory/anotherdirectoryhavinglongname/asppage/default.asp

You can see that it's laborious to type,and consequently quite error-prone too. Instead, by setting up a simple /asppage alise, the user can simply type:

http://mycoolpage/asppage/default.asp

What is ASP?

ASP is actually an extension to the your web server that allows server-side scripting. At the same time it also provides a compendium of objects and components which manage interaction between the web server and the browser. These objects can be manipulated by scripting languages.

Active Server Pages Object Model

Six objects make up the core of Active Server Pages. These are known as the Built-in- objects.

1.Server Object

2.Application Object

3.Session Object

4.Request object

5.Response Object

6.ObjectContext object

Server Object

The server object is a low-level object that provides some basic properties and methods that can be used in almost every Active Server Pages.

The object will allow you to do things such as :

Set the amount of time a script can run before an error occurs.

Create an instance of an ActiveX component.

<% Dim objTelephone

set objtelephone = server.createobject("Mytelephone.telephone")

objtelephone.color = "Green"

objtelephone.weight = 22

objtelephone.NumberOfkeys = 12

response.write "Done" %>

Application Object

With this object, you can:

Be notified when an application is first started,so that you can perform some startup processing.

Be notified when an application is ending, so that you have the opportunity to perform functions so that the application closes down clearly. One of the features of applicatin is that you can store information that is available to all clients that are accessing the application.

To initialize variables in the application object, you store the information about them in a text file name global.asa. Each application can have only one global.asa, and it's placed in the virtual directory's root.  

There are four possible events that can be handled in global.asa

1. application_onstart : this event will be fired when the first visitor hits the page.So, an application is 'started' the first time of its pages is accessed by a user.

Sub Application_onStart

          application("myappVariable") = ""

          application(anothervariable") = "0"

End Sub

2. application_onEnd

3. session_OnStart

4. session_onEnd

Session Object

There is one application object for each application in the web server. Every client accessing the application can get a reference to it. Each of these clients is called a Session. Therefore each of them has a reference to a unique session object.

The session object will allow you to:

Be notified when a user session begins, so that you can take appropriate actions for a new client, be notified when a client has ended their session. this can either be caused by a timeout or an explicit method called Abandon.

Store information that can be accessed by the client throughout the session.

A user's session begins when any user without a current session opens any .asp page within an ASP application. The user's session will continue as they navigate from page to page in the site.

There are two ways that a session can be terminated. If the user stops interacting with the application, then the session will end after certain period of time has elapsed. The default value for this time period is 20  minutes. The ABANDON statement in an ASP Script can also explicitly end the session.

The session can be used to store piece of information that will be available to every page in the application.

The session object has two collections:

Contents collection

Staticobject Collection

The contents collection is used to determine the value of specific session item, or to iterate through the collectio and retrieve a list if all items in the sessions.

session.contents("key")  --->    session.contents("VisitorId")

 

Request Object

When a web browser or other client applicaition asks for a page from the web server, this is called making a request. Along with the actual page that the client wants, it can send a great deal of information to the server as well. The request object is responsible for packaging up that information to make it easily accessible to the ASP application.

The client asks the server to provide a page with a .asp suffix. When the server ses this request , it interprets this type of page as an Active Server page. All of the information that the client is sending along with the request is then packaged into the Request object.

The collection hold information about:

The values that are provided in the URL that are sent by the client. In the URL, the client can include name-value pairs of information after the file name. This information is stored in the collection called QueryString.

The client is sending form request,then the value of the form element are stored in another collection.

The web server itself has a great deal of information about the request. These are called as HTTP server Variables.

If the client is sending cookies along with the request , these are included in their own collection.

What the Request Object Does:

The QueryString collection

The Form Collection

The ServerVariables collection

The ClientCertificate collection

The Cookies Collection

Response Object

With the response object , the ASP script can:

Insert information into the page being sent back to the client.

Send cookies back to the client

send the client to another page via redirection.

 

ObjectContext object

The objectcontext object is used in the conjunction with MTS. This advanced tools is a part of IIS4.0. It allows you to develop scalable applicaiton out of components. The object allows you to access the MTS system from within an ASP page.

SUMMARY of Built-in Objects

The Server object provides basic functionality  across the web server.

The Application and Session objects provides the application functionality that is not present in a basic web server.

The Request and Response obejcts are used to interpret the information send by the client and then construct the HTML page that will sent back in response.

The Cookies Collection

When a browser looks at a web page, it's impossible to for the web site to determine whether that visitor has been there before, which part of the site they  might be interested in, and so on.

Cookies were introduced as a method of identifying and marking each diffrent visitor to a web site.

Cookies are text files that reside on the user's computer. they store information about the user, and are used by particular server that the user has visited previously to personalize web pages, determine where a user has been before, and keep users up to date with relevent information.

A cookie can only store information which the user sends voluntarily or selects on a page and that can only happen if the "accept cookies" option is turned on by the user.

Individual cookies are limited to 4Kb of data. The maximum number of cookies allowed is 300.

Unlike the Form and QueryString collection cookies collection does not have a Count property, but it can hold multiple values for the same cookie name. When this happens, the cookie is said to have Keys, and each key holds a separate value.

<% Request.cookies("cookie")("key") = value %>

ex:

Response.cookies("Savelogin")("Email") = Request.Form("email")

Response.cookies("Savelogin")("pw") = Request.Form("Password")

Response.cookies("Savelogin").Expires = Date+30

          bLogeSaved = True

Else

          bLogsaved = False

-----

If request.cookies("Savelogin").Haskeys then

          response.redirect "CheckLogin.asp?cookie=1"

end if

-------

Dim StrEmail

 

If request.querystring("cookie") = 1 then

          strEmail = request.cookies("Savelogin")("Email")

else

          strEmail = request.form("Email")

end if

 

Other Components

Ad Rotator Component

Content Linking component

Browser Capabilities component

Database Access Component

The Scripting Objects

These objects do not directly deal with the communication between the client and the web server, as the built-in objects do. These objects provides additional functionality to the scripting language itself. This is why they are known as the Scripting Objects.

1. Dictionary object

IT allows you to store information in a single data structure for easy retrieval. It is similar to an array, and it is also similar to a collection.

2. Filesystemobject object

This is a group of related objects that are working together to provide access to a certain group of functions on the server.

The object model has following hierarchy:

Filesystemobject

Drives collection

Drive object

The information includes :

          Free Space available

          The volume name of the drive

          An indication whether or not the drive is ready

          The physical type of drive it is

          A reference to the root folder on the drive

 

Folders collection

Folder object

The folder object allows you  to access all of the properties of a folder. These properties include the name of the folder, the collection of files within it, the size of the folder in bytes and what its attributes are. In addition, if there are subfolders within the folder, then it will contain a reference to a collection of folder objects that represents the subfolders.

Files Collection

File object

The lowest level object in the FileSystemObject object model is the File Object. The File objects allows access to all of the propeties of an individual file in the system. These properties includes the File Name, the path to the file, a reference to the folder object where  the file exists and the size of the file.

Dim objFso, ObjFile, objFolder

Dim strPhysicalPath

Dim StrPathInfo

strpathinfo = request.servervariables("PATH_INFO")

strphysicalpath = sever.mapPath(strpathinfo)

Set objFso = Createobject("Scripting.Filesystemobject")

Set objFile = objFile.GetFile(strPhysicalpath)

Set objFolder = objfile.parentFolder

It provides access from an Active Server Pages script to the hard disk file system of the server. This object will allow you to work with files, as well as directories and sub-directories.

3. The textstream object allows  you to deal with the contents of the file that you have got information about using the FileSystemObject object. You read the information from the file and write data to it.

This does not mean that the file has to have a .txt extension. Rather, its contents have to be in text readable form. Naturally .txt files works fine, but you can also open .html files, .asp files, and event .log files

Debugging ASP

1. Use option explicit

          <% Option Explicit %>

 

2. Use Response.write

          Response.write "Debug: Name=" & strName & "<BR>"

 

3. Use Subprocedures

          Instead of repeating code you can just put it into a sub-procedure and then just call this procedure.

 

4. Use Include Files

          Using include files is just one step up from using procedures, as you can make your procedures available to many ASP files.

 

5. Conditional Tracking

         

6. Use Err Object

          This object is a part of VBScript, and not ASP. It only gives 5 properties

          Description

          Number

          Source

          HelpFile

          Helpcontext

and it's only got two methods:

          Raise

          Clear

If you have used other programming languages, you can usually have a central error routine, and using ON ERROR you can identify this routine. Unfortunately that's not possible with VBScript, as all you've got is the RESEME NEXT statement, which tells VBScript to continue with the next line if an error occurs.

          Response.write "10 / 0 = " &  10/0

          Response.write "<BR>Err.Description = " & Err.Description

7. Using the Script Debugge

 

DataBase with ASP

RecordSet object

Recordset is the set of records.

Dim ObjRec

Set objRec = server.createobject("ADODB.Recordset")

objRec = "Contect", StrConnect, adOpenForwardOnly, adLockReadonly, adCmdTable

' Now loop through the records

While Not objRec.EOF

          Response.write objRec("name") & ", "

          objRec.MoveNext

Wend

objRec.Close

Set objRec = Nothing

 

ADO Recordset Types: there are 4(Four) to deal with

1. Forward only

This is defaut type, using this type we cannot move backword.

2. Static

Similar to Forward  only recordset, except that it is scrollable, so you can move back to previous as well as moving forward.

3. Dynamic

This recordset is fully dynamic and lets you see addition, changes and deletion that are made by other users. It's fully scrollable so you can move around the recordset any way you like.

4. Keyset

Similar to Dynamic recordset, but you can't see records that other users add, although you can see chnages in existing records. Any records that other users delete becomes inaccessible.

What is Locking

Locking prevents other people from changing them. There are four types of locking you can use:

1. Read Only

This is default, and no locking is performed since you can't change the data.

2. Pessimistic

This is where you become very protective, and the record is locked as soon as you start editing it. It means that no one else can change the record until you release the lock.

3. Optimistic

This is more carefree attitute, where the records are only locked when you update them. This assumes that no one else will edit the record whilst you are editing it.

4. Optimistic Batch

Batch update mode allows you to modify several records and then have them all updated at once, so this only locks each record as it is being updated.

Bookmarks : It uniquely identifies a record in a recordset so you can always jump straight back to it.

Dim VarBookmark

...

           varbookmark = objRec.bookmark

..

          objrec.bookmark = varbookmark

 

Moving Directly

           objrec.move 3, adbookmarkcurrent

           objrec.move -3, adbookmarkcurrent

 

Finding Records

          objRec.Find  "name= 'Arun'"

          objRec.Find  "Birthday = #10/23/00#"

          objRec.Find  "name like 'A*'"

 

Filtering Records

          objRec.Filter = "State = 'IL'"

 

Arrays of Rows

Another useful method is GetRows, which returns an array of the records and fields in a recordset. GetRows will return the number of rows you request, starting from the current row.

Command Object

The command object is used for running processes againts a data store. These can be command that returns recordset, or they could be commands that don't return any values.

Dim objCmd

Dim objRec

set objCmd = Server.createobject("ADODB.Command")

set objRec = server.Createobject("ADODB.Recordset")

objCmd.ActiveConnection = Cn

cmd.CommandText = strSql

cmd.commandtype = adcmdtext

set objRec = objcmd.Execute

 

   OR

objcmd.commandText = "UPDATE authors SET name = 'arun' Where condition"

objcmd.Execute intRecords                      ' Where intRecords are number of records affected

Connection Object

This object is used to open a connection to Database or any other source.