- Introduction
- Introduction to Active Server Pages
- Creating ASP Files
- Database Access with Active Server Pages
- The ASP Objects
- Using Your Own ActiveX DLL with ASP
- The IIS Application Project
- From Here...
Introduction to Active Server Pages
If you are developing an intranet or Internet application, Active Server Pages is definitely worth investigating. Active Server Pages (ASP) is a component of Microsoft's Web server software that allows you to embed server-side script code in Web pages. As you know, the Hypertext Markup Language (HTML), used to created pages on the World Wide Web, is by itself static content. You can enhance this content on the client side through the use of client-side script, Java applets, Dynamic HTML, or ActiveX controls. However, Active Server Pages is a server-side enhancement because all the script code (usually VBScript) runs on the server. This means you can create Web sites that are dynamic and database-driven, yet return only standard HTML that is viewable in all browsers. A solution that uses Active Server Pages is ideal when your audience includes both Netscape and IE users, or when you want to avoid the issues associated with downloading applets or other client-side controls.
NOTE
Microsoft's Web server product for the Windows NT operating system is called Internet Information Server (IIS). In addition, Microsoft Personal Web Server is available as a "lightweight" Web server for a desktop PC. The Active Server Pages component is built into both Web servers, which at the time of this writing was available for download in the NT/95 Option Pack. For a glimpse at some of the differences between these products, see the upcoming section "Virtual Directories."
Active Server Pages Versus Standard HTML
Internet Information Server identifies an Active Server Page by its .ASP extension. To create a new ASP file, you simply change the extension on an existing .HTM file or start a new text file in a text editor. The ASP file can contain either standard HTML or script code, but usually contains some of both. The script code is what makes an Active Server Page different from a standard HTML page.
Consider the URL, or Universal Resource Locator, that identifies documents on the Web. Here are two URLs, one for a standard Web page and the other for an Active Server Page:
http://www.mysite.com/mypage.htm http://www.mysite.com/mypage.asp
A conventional URL (the first line) represents only a single static Web page. On the other hand, one Active Server Page (the second line) can produce many different Web pages in a user's browser, all from a single URL.
Think about how browsing works: through request-and-response communication between the browser and the Internet server. No matter what the Web server does, the end result is that a stream of HTML is returned to the browser in response to a request. When that request is for a standard .HTM file, it simply reads the file from the disk drive and passes it back to the browser. However, when the Internet server processes an .ASP file, it executes any embedded code during the process. This embedded script code can take different actions on the server, including generating the HTML that is returned to the browser.
For example, suppose that you want to create a Web page that displays the time of day when it is requested. The time of day is dynamic and cannot easily be placed in a standard static HTML page. However, you can do this easily with an Active Server Page, such as the one shown in Listing 31.1.
Listing 31.1 A Simple Active Server Page Displays the Date and Time
<HTML> <BODY> The Current date and time is <% Dim sTime sTime = Now Response.Write(sTime) %> </BODY> </HTML>
This ASP file in Listing 31.1 displays the current date and time. It contains a mixture of standard HTML and VBScript code. If you access the ASP file on your Web server, each time you press the browser's Refresh button, the server will execute the VBScript code, updating the date and time. However, if a user chooses to view the source in his browser, he will only see the resulting HTML:
<HTML> <BODY> The Current date and time is 7/5/1998 12:30:00 </BODY> </HTML>
Notice that the server-side VBScript code is hidden from the user because it is never sent back to the browser. Because the code runs on the server, it can access databases or use custom components, as shown in Figure 31.1.
Server-side scripting allows you to create powerful Web sites.
Virtual Directories
Before you can jump right in and use Active Server Pages, you have to do a little Web server administration. As you know, the files on your Web site are located in a directory on the Web server hard drivefor example, C:\InetPub\wwwroot\financeinfo\. This path is known as the physical directory because it points to an actual location on the server's hard drive. However, when a user on the Internet wants to access files in this directory, he uses an URL such as http://myserver/finance/Monthlyreport.htm. How does Internet Information Server map the address in the URL to the appropriate physical directory? The answer is through the use of virtual directories.
In our example, the Web server knows that the virtual directory /finance is really the financeinfo subdirectory. Virtual directories act as an alias to real directories. Virtual directories, as well as many other important aspects of the Web server, are controlled with the Internet Service Manager. (Similarly, Personal Web Server is managed with Personal Web Manager.) Screenshots of Internet Service Manager and Personal Web Manager are shown in Figure 31.2.
Creating Virtual Directories for ASP Files
The process of creating a virtual directory is simple. Follow these steps:
-
Use Windows Explorer or the MS-DOS prompt to create a new physical directory (for example, \Inetpub\wwwroot\test).
-
Open the Internet Service Manager, which should have been installed in a program group on your server.
-
The process of creating a virtual directory differs slightly in different versions of the Internet Service Manager: Each version of this software is pictured in Figure 31.2.
NOTE
IIS can be managed remotely if you have administrative authority on the Web server.
Internet Information Server 3.0, 4.0, and Personal Web Server have different administration tools.
Directories tab, and click the New button.
If you have IIS 4.0, right-click your Web site and choose New Virtual directory.
If you are using Personal Web Manager, click the Advanced icon and then the Add button.
- Specify the physical directory (which you can find with the Browse button) and the virtual directory (also known as the alias). For your sample, enter /test as the alias for the new directory.
- The next step is to set the appropriate permissions for the new directory. You should see several choices available:
- Press OK to close the new directory window. You are now ready to begin placing ASP files in the directory.
Read permission allows users to read files from the virtual directory. In general, you should enable this permission on all of your virtual directories.
Script permission means that scripts can be executed from the virtual directory. You need to check this option if you intend to use ASP files in the directory.
Execute permission is script permission plus the ability to execute other programs. (Script permission was not a separate choice in IIS 3.0).
Write permission allows users to upload files to a directory.
For this sample directory, you need to enable Read and Script permissions. (If you are using IIS 3.0, enable Read and Execute permissions.)
CAUTION
Because ASP code executes on your server, think very carefully about directory permissions. If you give a virtual directory Write and Execute permission, potentially anyone on the Internet could upload his own .ASP file and execute it on your servera very dangerous situation!
That's it! Now that you have created the /test virtual directory, you can place ASP files in it and execute them from a browser. You create your first sample ASP file and use it with the /test virtual directory later in the section "Creating a Simple ASP File."
Applications and Sessions
When designing a Web site, directory layout is important from an organizational standpoint, but with Active Server Pages, it becomes even more important because the virtual directories represent applications. In our previous example, the /test directory and the files contained within it are considered to be an ASP application. Subdirectories underneath virtual directories are also part of the application. For example, if you use Windows Explorer to create a subdirectory underneath the /test directory, then the new directory is part of the test application.
When a user first accesses any page in an application, he initiates a new session with the application. As long as the user stays within the same application and keeps loading Web pages, he is "in" the same session. However, if the user jumps to another application, closes the browser, or stops browsing for an extended period of time, then the Web server ends the session.
Why are these semantics important? As you will see, Active Server Pages contain objects and events that allow you to maintain state within a session. In other words, you can store session- and application-specific information on the server and share it between multiple ASP files.