What Is an Active Server Page?

It's like an HTML Page

When you first see an Active Server Page, it might remind you of an HTML page. The resemblance is not accidental. First and foremost, an Active Server Page is designed to create standard HTML that is sent to the browser. One way to do this is simply to place HTML code into the Active Server Page. For instance, if you saved Listing 9-1 into an ASP file, you would create the page shown in Figure 9-1.

Listing 9-1

HelloWorld. asp

 <HTML> <HEAD> </HEAD> <BODY> <P><H1>HelloWorld!</H1></P> </BODY> </HTML> 

Of course, using ASP as a simple replacement for HTML is not its intended mission. The real use for ASP is to create applications that change and react to the user who sits at the browser. This is done by using server-side scripting. Server-side scripting allows developers of Web applications to create complex applications using VBScript, a simple language related to Microsoft Visual Basic.

click to view at full size.

Figure 9-1 A simple "Hello World" Active Server Page as seen by the user at a browser.

NOTE
ASP pages can also contain JavaScript (or JScript, Microsoft's version of JavaScript). There are several problems with using JavaScript. First, you might think it is closely related to Java. It is not. It was named JavaScript only after the marketing success of Java. Second, virtually all Microsoft documentation on Active Server Pages deals with VBScript.

This is not as big a problem as it might seem. While your server-based scripting has limited portability, in general that limitation is not as crucial as whether your application will run on the various browsers that are out there. Here is the good news: ASP pages can create plain vanilla HTML on the browser, even when the ASP page uses VBScript on the server. Furthermore, you can even use VBScript to create JavaScript on the client browser, as I'll demonstrate in the Troubleshooter example later in this chapter. Here, use of JavaScript might be more reasonable than use of VBScript, since Netscape Navigator and most other non-Microsoft browsers support some form of JavaScript.

...But It's Not Exactly the Same as an HTML Page

Server-side scripting using VBScript is what transforms ASP pages into dynamic, living applications. While I've devoted most of this book to the creation of server-side applications using C++, I focus this chapter on the use of server-side scripting using a language many C++ programmers would prefer to avoid. Why? Because sometimes the right tool for the job is not the tool you are most familiar with.

Many of the applications that we will create using ASP can be augmented with the other technologies mentioned elsewhere in this book, as I'll explain. For many Web-based applications, ASP is the right place to start, perhaps just for prototyping the application, but possibly even for the entire life cycle of many simpler Web applications. As a working programmer, I have discovered that exploring all alternatives can't help but make a better end product.

You will like some features and hate some features of VBScript. When I give classes on ASP and ISAPI, I always include this list of pros and cons:

Why you will hate coding in VBScript

  • It is like Visual Basic ”enough said.
  • Variables are untyped or, rather, variant types, which could hold almost anything.
  • Variables do not need to be declared before use ”terrible for lousy typists.
  • Debugging is not ideal.

Why you will use VBScript anyway

  • It is like Visual Basic ”enough said.
  • It gives reasonable performance for a scripting language.
  • It can be extended with ActiveX controls.
  • It is supported within Windows 2000, which can't be underestimated when planning which technology to use.
  • Debugging is ever so much better with IIS 4 and later.

I'll elaborate on some of these pros and cons, but first let's look at some simple server-side scripting in action. Listing 9-2 illustrates some simple scripting that creates the page shown in Figure 9-2.

Listing 9-2

FontSize.asp

 <%@Language=VBScript%> <HTML> <HEAD> </HEAD> <BODY> <% dimtLoop fortLoop=1to7 %> <BASEFONTSIZE=<%=Cstr(tLoop)%>> <P>HelloWorld!</P> <% next %> </BODY> </HTML> 

Just as important as the output on the browser screen is the actual source that the browser sees. Listing 9-3 shows the source, obtained by choosing Source from the View menu in Microsoft Internet Explorer. You can see that there is no magic involved. The loop is simply "unwound," and each change to the font and each instance of the actual text is placed inline in the resulting HTML code. This is a trivial example, but even the most exotic example ”which might use a recordset returned from a database ”is no different in principle than this example that uses a local variable.

click to view at full size.

Figure 9-2 Browser output from simple ASP scripting that loops to increase the font size.

Listing 9-3

Browser's View of FontSize.asp

 <HTML> <HEAD> </HEAD> <BODY> <BASEFONTSIZE=1> <P>HelloWorld!</P> <BASEFONTSIZE=2> <P>HelloWorld!</P> <BASEFONTSIZE=3> <P>HelloWorld!</P> <BASEFONTSIZE=4> <P>HelloWorld!</P> <BASEFONTSIZE=5> <P>HelloWorld!</P> <BASEFONTSIZE=6> <P>HelloWorld!</P> <BASEFONTSIZE=7> <P>HelloWorld!</P> </BODY> </HTML> 

When using IIS, any file with an .asp extension is processed by default by a special DLL called ASP.DLL. Figure 9-3 shows the IIS Application Configuration dialog that allows this configuration to be set. While ASP is a standard part of IIS, it is a standard part that could be disabled or even changed if you had a different requirement for handling ASP pages. A more reasonable alternative, used by a number of Web developers, is to define a new file extension ”for instance, .isp (for Internet Server Page) ”and create an extension DLL that is then declared as the handler for these .isp files, just like the ASP.DLL is declared for .asp files. With this DLL in place, any file requested that has an extension of .isp will be processed by our replacement DLL. I'll discuss the realm of ISAPI extensions in more detail in Chapter 10.

click to view at full size.

Figure 9-3 IIS Application Configuration dialog.

A closer look at Listing 9-2 shows the difference between standard HTML and ASP pages. The first thing to notice is the use of delimiters to enclose the server side script. For example, <% appears just before the declaration of tLoop , using the Dim keyword. As I've mentioned, the declaration of variables in VBScript is optional but always a good idea, especially if you intend to move some of the script code into components to be compiled in Visual Basic. Next we have the start of a for loop, followed by the script end delimiter, %>. What comes next is some standard HTML code, followed by a special case of the script delimiter . <%=Cstr(tLoop)%> means that what follows is script and thus the variables currently in scope in the script are available. The "=" directly after the opening script delimiter is a special signal that the following expression should be evaluated and placed into the HTML stream going to the client. While this mixing and matching of scripting and code can be convenient , it can also lead to code that defies maintenance every bit as much as the spaghetti code of old. Alternatively, the ASP page in Listing 9-2 could have been rewritten so that it was structured like Listing 9-4, which uses the Response object's Write method. (I'll describe both shortly.) From the code, even without an understanding of the Response object, the intention is clear. We are looking to write out the strings of HTML code, with our variable copied into the HTML stream. I find this way of creating ASP applications much more readable, and certain optimizations can take place. For example, when you're writing a large amount of text to the browser, you might be better off concatenating strings and then at the end having a single call to Response.Write . Of course, the slowness of VBScript's string handling makes this less than ideal as well, but sending a single string to the browser in a single Response.Write statement might still be faster than sending the individual strings.

Listing 9-4

Response.asp

 <%@Language=VBScript%> <HTML> <HEAD> <METANAME="GENERATOR"Content="MicrosoftVisualStudio6.0"> </HEAD> <BODY> <% dimtLoop fortLoop=1to7 Response.Write("<BASEFONTSIZE="+Cstr(tLoop)+">") Response.Write("<P>HelloWorld!</P>") next %> </BODY> </HTML> 


Inside Server-Based Applications
Inside Server-Based Applications (DV-MPS General)
ISBN: 1572318171
EAN: 2147483647
Year: 1999
Pages: 91

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