So far, we have examined how to build Web pages that interact with the Web server. A collection of Web pages forms a Web site. But how about Web applications? Are they special applications that run over the Internet? The answer is yes and no. They are client-server applications that run over the Internet, but they are not special applications, and there are no new topics to learn. A Web application is a Web site that uses ASP files on the server. The windows of a regular application correspond to the pages of a Web application. The pages exchange information with one another through cookies (as explained earlier in this chapter) or through the Application and Session objects.
To use the Application and Session objects efficiently, you must learn about the ASA files. Every Web application has a GLOBAL.ASA file, which must reside in the server’s root folder. This file contains the event handlers of the Application and Session objects and is looked up by the Web server when a Web application or a session starts. A Web application starts when the first connection to the server is made. When the first visitor is connected to the Web server, the server looks up the GLOBAL.ASA file for an Application_onStart event handler. If one is found, it’s .executed.
At the same time, a new session is established. The server also looks up the GLOBAL.ASA file for a Session_onStart event handler. If one is found, it’s executed. At the same time, a new Session object is created, which is associated with the client that established the session. This object remains alive for as long as the client remains connected. When the session is terminated, the server executes the Session_onEnd event handler (which must also reside in the GLOBAL .ASA file).
Let’s look at a typical GLOBAL.ASA file. The file shown next contains the code for keeping track of the total number of visitors that have hit the site (variable Visitors), as well as the number of users currently connected (variable Viewers).
When the Web server starts, it creates a new variable, Viewers. This.variable is initialized to zero when the application starts, and it increases by 1 every time a new session is established. When a session terminates, the Viewers variable is decreased by 1.As a result, it reflects the number of users currently connected .
.The variable Visitors is increased each time a new session is established. This variable reflects the total number or visitors that have hit the site. Of course. this variable is reset to zero every time the Web server is shut down, so you must store its value in . a local file and read it from the file when the application starts.
In each session’s onStart event handler, we store the IP address of the client to the Session variable User. This variable is unique within the scope of the Session object and can be used to identify the client. Each Session object has its own User variable, and no other Session object can see it. In fact, you can’t even enumerate the User variables of all sessions.
Notice that you must lock the Application object when You update the values of its variables, but this is not necessary for the Session object. When a new Session object is initiated, it has its own set of local variables.
To test the GLOBAL.ASA file shown here, you can write an ASP file that accesses the Application and Session variables declared in the GLOBAL.ASA file. The VIEWERS.ASP file, shown next, produces a page li~e the one shown.
The VIEWERS. ASP page reads the values of the Visitors, Viewers, User, and HName variables’ and displays their values on the page it produces. It also demonstrates the BrowserType component, which returns information about the client’s browsing capabilities. Active Server Pages produce straight HTML code that can be viewed on any browser, so it’s a good idea to consider the basic capabilities of the client before dumping to it a document it can’t display properly. Nearly every browser supports tables and frames, but you can’t assume that all browsers can also execute VBScript code.