%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
E-mail our teacher: Robert
Madsen
E-mail this student: Jon
Slate
Robert
Madsen has been working in the information technology field for over 25 years.
He is an experienced programmer, web designer, and instructor. Robert currently
runs a successful consulting and training business and specializes in database
design and database driven web sites.
:
The ASP EnvironmentAlthough we don't get into working with the global.asa file, this is an appropriate time to introduce its purpose. The global.asa is found directly under the root directory of your web server (which is often c:\inetput\wwwroot, or something similar). This file contains global variables as well as programming for the application object and session object that can be accessed by anyone using the site. This global.asa file is basically an ASP file that is processed whenever the web server is started. You might try locating it and taking a look, but don't change anything unless you know what you are doing! Uses of the global.asa file will be covered in the advanced ASP class.
The Global.asa file can contain only the following:
Note: Because you cannot use the ASP script delimiters
(<% and %>) to insert scripts in
the Global.asa file, a subroutines is put inside an HTML <script> element.
<object> declarations
It is possible to create objects with session or application scope in Global.asa by using the <object> tag.
Note: The <object> tag should be outside the <script> tag!
A TypeLibrary is a container for the contents of a DLL file corresponding to a COM object. By including a call to the TypeLibrary in the Global.asa file, the constants of the COM object can be accessed, and errors can be better reported by the ASP code. If your Web application relies on COM objects that have declared data types in type libraries, you can declare the type libraries in Global.asa.
Note: METADATA tags can appear anywhere in the Global.asa
file (both inside and outside <script> tags). However, it
is recommended that METADATA tags appear near the top of the Global.asa
file.
#include directive
The content of one ASP file can be inserted into another ASP file before the server executes it, with the #include directive. The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.
Note: The Global.asa file must be stored in the root
directory of the ASP application, and each application can only have
one Global.asa file.
Application variables are available to all pages in one application. Application variables are used to store information about ALL users (not just ONE single user, as in Session variables) in a specific application.
The ASP Server object is used to access properties and methods on the server.
As you know, ASP is an object oriented language. This means that commands and settings are grouped under objects that encapsulate common functionality.
The server defines many objects as well (including the request and response object we already learned). It is important to understand that the world of server-side ASP and client-side VBScript are mutually exclusive. Objects that exist on the client don’t exist on the server and vica-versa. For example, you can’t access a form object from the server-side script, nor can you access the request object from the client-side script.
The dictionary object stores name/value pairs (referred to as the key and item respectively) in an array. The key is a unique identifier for the corresponding item and cannot be used for any other item in the same dictionary object.
Note: Unlike
the remaining objects in this module, the dictionary object can
also be defined on the client. However, dictionaries defined
on the client are still mutually exclusive from dictionaries
defined on the server.
The following code creates a dictionary object called "cars", adds some key/item pairs, retrieves the item value for the key 'b' using the Item property and then outputs the resulting string to the browser.
<%
Dim cars
Set cars = CreateObject("Scripting.dictionary")
cars.Add "a", "Audi"
cars.Add "b", "Buick"
cars.Add "c", "Cadillac"
Response.Write "The value corresponding to the key 'b' is " & cars.Item("b")
%>
Output:
The value corresponding to the key 'b' is Buick
Dictionaries are useful for storing information that you might want to access. For example, you could store username/password pairs (although this wouldn't be a secure method) or any other data that had a unique key paired with some data.
The tables below detail the properties and methods that belong to the dictionary object.
| CompareMode | Used to set and return the key's string comparison mode which determines how keys are matched while looking up or searching. |
Count |
Used to determine the number of key/item pairs in the dictionary object. |
Item |
Allows us to retrieve the value of an item in the collection designated by the specified key argument and also to set that value. |
Key |
Lets us change the key value of an existing key/item pair. |
| Add | Used to add a new key/item pair to a dictionary object. |
Exists |
Used to determine whether a key already exists in the specified dictionary. Returns True if it does and False otherwise. |
Items |
Used to retrieve all of the items in a particular dictionary object and store them in an array. |
Keys |
Used to retrieve all of the keys in a particular dictionary object and store them in an array. |
Remove |
Used to remove a single key/item pair from the specified dictionary object. |
RemoveAll |
Used to remove all the key/item pairs from the specified dictionary object. |
An application on the Web may be a group of ASP files. The ASP files work together to perform some purpose. The Application object in ASP is used to tie these files together.
The Application object is used to store and access variables from any page, just like the Session object. The difference is that ALL users share one Application object, while with Sessions there is one Session object for EACH user.
The Application object should hold information that will be used by many pages in the application (like database connection information). This means that you can access the information from any page. It also means that you can change the information in one place and the changes will automatically be reflected on all pages.
The application object is used for persistent storage of application-wide settings. Persistent refers to the fact that the data is stored and can be retrieved later. Before ASP came along, there was no such thing as data that could be accessed from one session to another because HTML is a stateless protocol that does not store persistent data.
Any properties set for the application object is available to all users of the web. You can store values in the application contents collections. A collection in ASP lingo is a set of like items (the dictionary object uses a collection to store your items).
Information stored in the application content collection is available throughout the application. In other words, data that you store in an application content collection can be accessed by ASP you create in any file on the web site. It is very common to initialize the contents collection in the global.asa file, although this can happen in any asp file.
The following script demonstrates storage of two types of variables.
<%
Application("greeting") = "Welcome to My Web World!"
Application("num") = 25
%>
Each of these variables are members of the Application.Contents collection. So, to display an application variable on the web page, you could use:
<%
Response.Write(Application.Contents(“greeting”))
%>
The tables below detail the collections, properties, and methods that belong to the application object.
| Contents | Collection of variables stored in the Application Object. |
StaticObjects |
Collection of variables created via the <OBJECT Scope=Application ...> tag in the Global.asa file. |
| Application_OnStart | Fired the first time the web server is used. Only the Application and Server objects are available. The event procedure is stored in global.asa. |
Application_OnEnd |
Fired before web server is unloaded, after Session_OnEnd. Only the Application and Server objects are available. The event procedure is stored in global.asa. |
Lock() |
Obtains exclusive write access to the Application object. This is necessary each time a client needs access to the application object so multiple concurrent accesses won’t occur. Subsequent access calls are qued until after the object is released. |
Unlock() |
Releases exclusive write access to the Application object. |
The server object allows us to access several methods that work directly with the server. The most common use is to call the CreateObject method, which allows use to add objects to our application and thereby extend ASP with functions and abilities that aren’t native. In the next module we see how important this is when we use the CreateObject method to add database connections and recordsets.
Another important use of the server object is the MapPath method detailed later in this module.
The tables below detail the properties and methods that belong to the server object.
| ScriptTimeout | Sets the number of seconds a script is allowed to run. The timeout is actually the larger of this value and that in the Registry. |
| CreateObject | Creates an instance of a server component. |
HTMLEncode |
Escapes special HTML characters. |
MapPath |
Returns a full physical path, given a virtual or relative path. |
URLEncode |
Escapes special URL characters. |
In the introductory class we learned about the Request and Response objects. We also learned about objects in the client (e.g. Internet Explorer) has such as the document and form.
In this lesson we will cover the dictionary, application, and server object. The session object should be covered in the advanced class.
The CreateObject method creates an instance of an object.
Note: Objects created with this method have page scope.
They are destroyed when the server are finished processing the current
ASP page. To create an object with session or application scope, you
can either use the <object> tag in the Global.asa file, or store
the object in a session or application variable.
The MapPath method maps a specified path to a physical path.
MapPath Method
The MapPath method can be used to discover the physical path
to a virtual directory. Virtual paths are the paths that are
used in the URL to access a web resource.
An example of a virtual path is:
http://www.mylastchoice.com/db/BookStore.mdb.
The folder db actually exists somewhere on the web server, and the file database.mdb is located in that folder. However, there are times when we need to know the physical path of the file, and this will differ depending on how the web server is setup.
For example, the physical path of the above file might be:
c:\domains\mylastchoice.com\wwwroot\db\BookStore.mdb
To understand the difference between the physical path and virtual path, you must understand the concept of the web root.
In the above example, the web root is:
c:\domains\mylastchoice.com\wwwroot\
In other words, the web root is the location from which all other virtual paths are designated. The web server takes care of translating incoming URLs to the actual physical path so that it can deliver files and other resources.
The following command would return the physical path of the root of the current web:
Response.Write(Server.MapPath("/"))
Notice the single slash inside the quotes. This tells the MapPath method that you want to see the web root. You can also specify any virtual path.
For example:
Response.Write(Server.MapPath("/db"))
will display the physical path of the db folder under the web root.
Note that you can use either a forward slash or backward slash
and get the same information.
The MapPath method will be useful next week when we create connections to our database.
Note: This method cannot be used in Session.OnEnd and
Application.OnEnd.
:
Connections
You use the ADO Connection object to create a connection to a data provider. The ADO Connection object is similar to the DAO database object or the RDO rdoConnection object.
:
RecordsetsYou use the ADO Recordset object to create a set of records from a query. As in the DAO Recordset and the RDO rdoResultset objects, you can move forward and backward through ADO recordsets. Sometimes, ADO recordsets are called cursors. ADO recordset cursors are always built on the server.
:
Data manipulation
:
Integrating Forms and Databases
:
Wrap-Up Project