Dynamic HTML provides the primary client-side programming tool for Microsoft Internet Explorer version 4.0. But Dynamic HTML is not supported by browsers such as Netscape Navigator. In fact, very little of the client-side functionality supported by various browsers can be
If you want to design an Internet site accessible to a number of different browsers, you need to move the programming from the client to the server. Microsoft Active Server Pages (ASP) allows you to create server-side applications that can be used by a variety of browsers. ASP is
Listing 5-1 produces a simple ASP page that generates a greeting based on the time of day. In this example, the
Listing 5-1. A simple ASP example.
<%@ LANGUAGE="VBSCRIPT"%> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-8859-1"> <METANAME ="GENERATOR" CONTENT="Microsoft FrontPage 2.0"> <TITLE>Simple ASP Example</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <% Dim strGreeting If Hour(Now) < 12 Then strGreeting = "Good Morning!" ElseIf Hour(Now) > 11 And Hour(Now) < 18 Then strGreeting = "Good Afternoon!" ElseIf Hour(Now) > 17 Then strGreeting = "Good Evening!" End If %> <H1><%=strGreeting%></H1> </BODY> </HTML>
Notice that the code structure in the listing is
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-8859-1"> <META NAME="GENERATOR" CONTENT="Microsoft FrontPage 2.0"> <TITLE>Simple ASP Example</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <H1>Good Afternoon!</H1> </BODY> </HTML>
This is the point of using ASP. The result of the code is simply HTML! This page is visible in any browser and will work in Netscape Navigator as well as in Internet Explorer. ASP provides true platform independence for the Internet developer.
Listing 5-1 contains a few other features that are worth mentioning. Notice the line of code at the top of the page—the code that specifies the language that will be used for the page:
<%@ LANGUAGE="VBSCRIPT"%>
Normally, you would use VBScript as the language, but ASP also supports JavaScript. Unlike client-side scripting, the choice of language here has no impact on browser compatibility since the code is all executed on the server. You simply code using the language that makes you comfortable.
Also notice the line of code where the HTML is actually generated. The code uses the variable to write the greeting, as
<H1><%=strGreeting%></H1>
The variable strGreeting is
At the simplest level, creating an ASP page is nothing more than writing server-side code to produce the result you have in mind. VBScript, however, is not a fully functional language, and it falters when you try to create more complex pages. For example, VBScript has no intrinsic functions to allow data access, nor can it
ASP objects and components are nothing more than ActiveX components, like any ActiveX DLLs you might use in Microsoft Visual Basic. The difference between ASP objects and ASP components lies in the way they are packaged. ASP objects are ActiveX elements that are always available to VBScript. You do not have to explicitly create ASP objects for your use. ASP supports the Application, Session, Request, Response, and Server objects. ASP components, on the other hand, are DLLs that exist outside the ASP framework. These components can be created in any language, but Microsoft has packaged several useful ASP components with Visual InterDev. ASP components are not available unless they are
One of the biggest concerns
Defining an application in the Microsoft Windows environment is simple. The application starts when the icon is double-clicked, and the application ends when Exit is
This problem of defining the beginning and ending of an application affects the ability to correctly manage variables and work flow. Fortunately, Active Server Pages provides a solution. ASP uses a special file, named GLOBAL.ASA, to define the beginning and ending of an application as well as the beginning and ending of an individual user's session. GLOBAL.ASA is responsible for detecting four key events in your site: Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd. Listing 5-2 shows a typical GLOBAL.ASA file.
Listing 5-2. GLOBAL.ASA.
<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="Server"> ' You can add special event handlers in this file, which ' will be run automatically when special Active Server ' Pages events occur. To create these handlers, add ' a subroutine with a name from the list below that ' corresponds to the event you want to use. For example, ' to create an event handler for Session_OnStart, you would ' put the following code into this file (without the ' comments): ' ' Sub Session_OnStart ' **Put your code here ** ' End Sub ' EventName Description ' Session_OnStart Runs the first time a ' user runs any page in ' your application ' Session_OnEnd Runs when a user's session ' times out or quits your ' application ' Application_OnStart Runs once when the first ' page of your application ' is run for the first time ' by any user ' Application_OnEnd Runs once when the Web ' server shuts down </SCRIPT> <SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Session_OnStart End Sub Sub Session_OnEnd End Sub Sub Application_OnStart End Sub Sub Application_OnEnd End Sub </SCRIPT>
GLOBAL.ASA contains <SCRIPT> tags to designate scripting sections. These tags contain a special attribute named RUNAT=Server, which specifies that the contained VBScript should run on the server, not on the client. RUNAT=Server is similar in functionality to the angle
<SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Application_OnStart ` Application-specific code End Sub </SCRIPT>
Although the GLOBAL.ASA file uses events to mark the beginning and ending of an application, it is still not clear what
By this definition, an application can be used by many browsers at the same time. The Application_OnStart event fires only once—when the first user requests a Web page from the virtual directory. When other users then request pages from the same directory, only the Session_OnStart event fires.
While application can refer to multiple browsers accessing the same set of Web pages, session refers to an individual browser accessing the same Web pages. A session for an individual browser lasts as long as the user continues to request Web pages from the virtual directory. If the user stops requesting additional Web pages for 20 minutes, the session ends and the Session_OnEnd event fires. When all user sessions in the virtual directory have ended, the Application_OnEnd event fires.
As an example, consider the following scenario. Two users are about to access the Magazine application on a Web site. User1
Just 5 minutes later, User2 accesses the same application. Because User1 has been active in the last 20 minutes, the Magazine application is still live. Therefore, only the Session_OnStart event fires, signaling the beginning of a second user session. Additionally, the site will now require that both the User1 and User2 sessions end before the application can be closed.
During the
User2 remains active for an hour, reading magazine articles by requesting Web pages. Finally, though, User2 shuts down the computer, and 20 minutes after User2 leaves the site, the Session_OnEnd event fires. Because User2 is the last user of the application, the application terminates and the Application_OnEnd event fires.