Active Server Pages Fundamentals

Listing 5-1 produces a simple ASP page that generates a greeting based on the time of day. In this example, the hour of the day is determined by using the code Hour(Now), where Now is a VBScript function that returns the current date/time stamp. If the hour is less than 12, a variable is assigned the greeting "Good Morning!" From noon to 6 PM, the message is "Good Afternoon!" and after 6 PM, "Good Evening!"

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"> <META NAME="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 surrounded by special characters—angle brackets and percent signs (<%…%>). These symbols designate the code as server-side code, which means that the code will be evaluated before the page is actually sent to the browser. In fact, if you were to view the resulting HTML from Listing 5-1 in Internet Explorer, you would see the following (assuming it's afternoon, of course!):

<!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 follows:

<H1><%=strGreeting%></H1> 

The variable strGreeting is enclosed by the angle brackets and percent signs used in the rest of the server-side code, but it is also preceded by an equal sign. The equal sign plays an important role in ASP, telling ASP to insert the actual value of the variable in the page as HTML. Therefore, the value of the greeting is inserted at this point and is seen in the browser as text content.

Objects and Components

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 open text files. In fact, VBScript has no intrinsic functions that allow access to any external data sources. How, then, can you use ASP to perform advanced functions such as data access? The answer is that you supplement VBScript with ASP objects and components.

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 specifically created in code. ASP supports the Database Access, File Access, Browser Capabilities, Ad Rotator, and Content Linking components.

The GLOBAL.ASA File

One of the biggest concerns facing Internet developers, regardless of the technology they use, is the difficulty of creating a true application on the Internet. The interaction between a browser and a Web server is basically a stateless transaction in which the server hands a Web page to the client and then forgets that the client even exists. When the client subsequently asks for another Web page, the server has no memory of the first request. The essential problem for all Web applications is this: How do you define an application?

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 chosen from the File menu. In between these two events, data can easily be remembered in variables. However, this is not true for Internet applications. How can you determine when an application begins and ends? If a user comes to a site and views a page, you might say that the application has started. But what if the user jumps to another site and returns five minutes later? Is the application still live? What if the user leaves for an hour? Two hours?

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 bracket/percent sign symbols used in the Web page to designate server-side scripting. The events in GLOBAL.ASA can be trapped on the server side using standard syntax. For example, trapping the start of an application is accomplished with the following code:

<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 constitutes an application. One working definition, offered by Microsoft, defines an Internet application as a virtual directory and all of its pages. If a user requests a Web page from a virtual directory named Bookstore, the user has started the Bookstore application and the Application_OnStart and Session_OnStart events will fire in GLOBAL.ASA.

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 accesses first and requests the Web page DEFAULT.ASP. At this moment, the Application_OnStart and Session_OnStart events fire.

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 next 15 minutes, User1 does not access any pages in the Magazine application. Because User1 has been inactive for 20 minutes, ASP concludes that the session for User1 has ended and the Session_OnEnd event fires. The application is still active, however, because User2 has requested a page in the last 20 minutes.

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.



Programming Active Server Pages
Programming Active Server Pages (Microsoft Programming Series)
ISBN: 1572317000
EAN: 2147483647
Year: 1996
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net