Understanding Active Server Pages (ASP)

Active Server Pages (ASP) is a technology that was introduced in 1997 by Microsoft, and it revolutionized dynamic content for the Internet. Prior to ASP, there were other technologies that existed for dynamic Web content, but ASP was the first technology that was widely accessible to most Web developers. Writing ASP pages doesn't require complex knowledge of confusing concepts. Instead, Web developers can leverage existing knowledge to add dynamic and interactive content.

When Microsoft first released ASP, it was available as an add-on to Internet Information Server (IIS) 3.0, the free Web server that Microsoft made available for use on Windows NT Server. ASP also worked with Peer Web Services, a Web server for Windows NT Workstation, and for Personal Web Server, the Web server that shipped with FrontPage 98. Starting with IIS version 4, ASP is a part of the Web server and nothing else needs to be installed for ASP support.

ASP applications run on a Web server, but the user interface for the application runs in a Web browser on the user's machine. There is actually no connection between the Web server and the user's browser. Once the Web server generates the page and sends it to the browser, the work is done and the Web server no longer has any knowledge of the user. This is obviously not conducive to an interactive and dynamic Web application. Therefore, the ASP object model gives ASP developers the ability to store information about a particular user's browser session. (An object model provides a method by which a developer can programmatically access a particular technology.)

For more information on object models and how they're used, see "Introduction to Programming with Visual Basic for Applications," p. 568.


The ASP Object Model

The ASP object model provides several objects that can be used, among other things, to store and retrieve information between browser requests.

  • Application object An object that is shared between all users of a Web application. Allows an ASP developer to get information about and manipulate an ASP application.

  • ObjectContext object Allows ASP developers to write ASP code that uses transactions. Transactions allow an ASP developer to take specific actions depending on whether the ASP page executed successfully in its entirety.

  • Request object Gives the ASP developer programmatic access to the user's request for the page being displayed. Provides access to things such as form fields in a form.

  • Response object Gives the ASP developer programmatic access to the Web server's response to a user's request.

  • Server object Provides for programmatic access to the Web server on which the ASP application is executing.

  • Session object An object that is unique to each application session a user has open. Allows an ASP developer to programmatically access a user's session and store and retrieve information about it.

For more information on the ASP object model, read Active Server Pages 3.0 By Example from Que Publishing.


Anatomy of an ASP Page

ASP pages consist of both HTML code and server-side script and have a .asp file extension. The server-side script is written using either VBScript or JavaScript, or JScript as Microsoft calls it. Server-side script is separated from the HTML code by using either ASP delimiters (<% and %>) or <SCRIPT> tags with a runat attribute that specifies the script is to run on the Web server.

The following code will display the current date and time of day as reported by the Web server:

 
 <HTML> <BODY>     <% Response.Write Now() %> </BODY> </HTML> 

Any code between the <% and %> delimiters is ASP code, and the Web server processes that code prior to sending the Web page to the Web browser. In the preceding example, the Web server will replace <% =Now() %> with the current date and time, as shown in Figure 35.1. The user browsing to this page will not see the ASP code. She will only see the result of that code running on the Web server.

Figure 35.1. The ASP code executes on the Web server and sends the current date and time to the Web browser.

graphics/35fig01.gif

graphics/troubleshooting_icon.jpg

If you browse to an ASP page and you are asked to download the file instead of the file being displayed in the browser, see "ASP Page Generates Download Prompt" in the "Troubleshooting" section of this chapter.


The following code produces the same output, but uses <SCRIPT> tags instead of the ASP delimiters.

 
 <HTML> <BODY>     <SCRIPT language="VBScript" runat="server">         Response.Write Now()     </SCRIPT> </BODY> </HTML> 

Most ASP developers will use <SCRIPT> tags for writing functions that perform a series of computations and will use the ASP delimiters <% and %> for inline ASP code. If you use both to produce output on the page, you might be surprised at the output. Consider the following ASP page:

 
 <HTML> <BODY>     <SCRIPT language="VBScript" runat="server">         Response.Write Now()     </SCRIPT>     <% = "The current date and time is " %> </BODY> </HTML> 

The result of this page is shown in Figure 35.2. Note that the result of the code inside the ASP delimiters is displayed prior to the ASP code inside the <SCRIPT> tags even though the latter appears first in the page. Code that appears inside <SCRIPT> tags will always execute after inline script.

Figure 35.2. ASP code inside ASP delimiters will always run before code inside <SCRIPT> tags.

graphics/35fig02.gif

Using ASP, it's easy to make a page very dynamic. For example, you can use ASP to access a database of users and customize a page's appearance depending on who is accessing the page. In Listing 35.1, a user's role is used to determine how the page is laid out.

Listing 35.1 Using ASP to Customize Page Content
 <HTML> <!--#include file="dbconnect.inc"--> <BODY>   <%     userRole = rs("Role")   %>   <TABLE>     <TR>       <TD>       <% If userRole = "employee" Then %>           From this page, you can:<BR>           <UL>             <LI>Check your vacation balance.</LI>             <LI>Review your current goals.</LI>             <LI>Review your personal statistics.</LI>           </UL>       <% ElseIf userRole = "manager" Then %>           Manager's Toolset<BR>           <UL>             <LI>Edit your employee's information.</LI>             <LI>Get team metrics.</LI>             <LI>Review employees.</LI>           </UL>       <% End If %>       </TD>     </TR>   </TABLE> </BODY> </HTML> 

In this example, the code to connect to the database and retrieve the value for userRole is stored in an external file called dbconnect.inc. The second line specifies that the file dbconnect.inc should be included in this page. The dbconnect.inc file is not listed here, but in this example, that page would contain ASP code that connects to a database and pulls out the role of the current user.

Once we know the role of the current user and have stored it in userRole, ASP code is used to determine the layout of the page based on that role. If userRole is equal to employee, the page displays a list of tasks that an employee can accomplish using the Web page. If userRole is equal to manager, the page will display tools for a manager to use.

As you can see, inline server-side code makes it easy to make an interactive Web application, but it does have drawbacks. Listing 35.1 is simple and uncluttered, but in a real-world environment, ASP pages can become very large. ASP code inline with HTML code can get confusing for a developer to deal with. This is one of many reasons that Microsoft created a new version of ASP from the ground up.



Special Edition Using Microsoft Office FrontPage 2003
Special Edition Using Microsoft Office FrontPage 2003
ISBN: 0789729547
EAN: 2147483647
Year: 2003
Pages: 443

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