February 25, 2021
Hot Topics:

Programming Microsoft .NET : Web Forms

  • By Jeff Prosise
  • Send Email »
  • More Articles »

The Active Server Pages Solution

A third solution to the problem of processing input from HTML forms on Web servers, and the one that made Windows a popular platform for Web applications in the second half of the 1990s, is Active Server Pages (ASP). Active Server Pages lower the barrier to entry for Web developers by allowing HTML and server-side script to be freely mixed in ASP files. Scripts are typically written in JScript (Microsoft's version of JavaScript) or VBScript, but they can be written in other languages as well. Intrinsic objects available to those scripts abstract the low-level details of HTTP and make it exceedingly easy to write code that generates HTML content dynamically. Just how easy is ASP? Compare the code in Figures 5-4 and 5-5 and judge for yourself.

When an Active Server Page is requested, ASP parses the page and executes any scripts contained inside it. Scripts access the input accompanying the request by using the ASP Request object, and they write HTML to the HTTP response using the ASP Response object. Figure 5-5 shows the ASP version of Calc.html. The VBScript between <% and %> tags checks the incoming request for inputs named op1 and op2. If the inputs aren't present, an empty calculator is rendered back to the client. If the inputs are present-that is, if Request ("op1") and Request ("op2") evaluate to non-null strings-the server-side script converts the inputs to integers, adds them together, converts the result to a string, and writes the string to the HTTP response using Response.Write.

To prevent the numbers typed into the text boxes from disappearing following a postback, Calc.asp uses ASP's inline output syntax (<%= %>) to initialize the Value attributes returned in the <input type="text"> tags. When the page is first requested from the server, Request ("op1") and Request ("op2") return empty strings, so the tags output to the client produce empty text boxes:

<input type="text" name="op1" value=""/><input type="text" name="op2" value=""/>

But when the form is rendered again following a postback, Request ("op1") and Request ("op2") return the values input by the user and are echoed to the client in the tags' Value attributes:

<input type="text" name="op1" value="2"/><input type="text" name="op2" value="2"/>

To verify that this is the case, drop Calc.asp into \Inetpub\wwwroot and bring it up by typing http://localhost/calc.asp. Then enter a couple of numbers, click the = button, and use the View/Source command in Internet Explorer to view the HTML returned by ASP.

The appeal of ASP-and the reason it caught on so quickly after its introduction in 1997-is that it provides an easy-to-use model for dynamically generating HTML on Web servers. ASP provides a higher level of abstraction than either CGI or ISAPI, which means a flatter learning curve and faster time to market. And ASP integrates seamlessly with ActiveX Data Objects (ADO), which makes it a great solution for writing Web apps that interact with back-end databases.


<%@ Language="VBScript" %><html>  <body>    <form>      <input type="text" name="op1" value="<%= Request ("op1") %>"/>      +      <input type="text" name="op2" value="<%= Request ("op2") %>" />      <input type="submit" value="  =  " />      <%        If Request ("op1") <> "" And Request ("op2") <> "" Then            a = CInt (Request ("op1"))            b = CInt (Request ("op2"))            Response.Write (CStr (a + b))        End If      %>    </form>  </body></html>
Figure 5-5. Source code for an ISAPI DLL.

Page 3 of 4

This article was originally published on February 26, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date