Migrating a Web Server to IIS 5.0 |
A key difference between ASP and CGI applications is that in an ASP page you can interweave industry-standard HTML and server-side scripting code to deliver the appropriate HTML to the client. By doing so, you combine the most powerful elements of both environments.
Using standard HTML as part of the application code is a benefit that cannot be overemphasized. With this feature, the developer can emit any HTML that the client browser supports, whereas utilities such as Cgi.pm are necessarily limited by the tags they implement. With new developments such as DHTML, tools such as Cgi.pm must be modified, perhaps heavily, to support new tags or other new client-side features.
In addition, with ASP, Web designers and nonprogrammers can author in pure HTML. On the other hand, having to use a special HTML dialect, as required by CGI tools such as Cgi.pm makes it difficult for these users to modify the generated HTML.
Many CGI applications emit HTML by using the output facilities of the language in which they’re written. For example, a simple Perl-based CGI application might look like this:
#!/usr/local/bin/perl
print "Content-type: text/html", "\n\n";
$server_name = $ENV('SERVER_NAME');
print "<HTML><BODY>Your server name is ", $server_name;
print "</BODY></HTML>";
exit(0);
Note that the HTML is embedded within the print function. The following is the corresponding ASP page, with code written in VBScript:
<HTML>
<BODY>
Your server name is <%= Request.ServerVariables("SERVER_NAME") %>
</BODY>
</HTML>
Note that the entire page is just simple HTML, except for the VBScript expression
<%= Request.ServerVariables("SERVER_NAME") %>
For some applications you must specify HTTP header information, rather than using the default MIME type of text/html. In the ASP environment, you can accomplish this by using the ContentType property of the Response object. For example, the following instruction sets the content type to “text/plain”:
<% Response.ContentType = "text/plain" %>
Many Perl-based CGI applications use the Cgi.pm or Cgi-lib.pl modules for formatting HTML output, as well as for performing other tasks. For example, the following script uses Cgi.pm to generate a form that collects the user’s address; once the form is submitted, the form is redisplayed with the address beneath it:
use CGI qw(:all);
print header;
print start_html('Enter your address'),
h1('Enter your address'),
hr,
p,
start_form,
table(
Tr(td("Street"),td(textfield('street'))),
Tr(td("City"), td(textfield('city'))),
Tr(td("State"), td(textfield('state'))),
Tr(td("Zip"), td(textfield('zip')))
),
submit,
end_form,
hr;
if (param())
{
print
"Street is: ", param('street'), p,
"City is: ", param('city'), p,
"State is: ", param('state'), p,
"Zip is: ", param('zip'), p,
hr;
}
The preceding script provides a convenient way to lay out a form if you aren’t able to generate the HTML as part of the source code. The drawback to these utilities is that they require the developer to master a “pseudo-HTML” dialect in order to generate the page.
The corresponding ASP page looks almost exactly like the actual HTML page that will be sent to the browser, except for the VBScript that is interwoven with the HTML:
<HTML>
<HEAD>
<TITLE>Enter your address</TITLE>
</HEAD>
<BODY>
<H1>Enter your address</H1>
<HR>
<P>
<FORM METHOD=POST>
<TABLE>
<TR><TD>Street</TD><TD><input name="street"></TD></TR>
<TR><TD>City</TD><TD><input name="city"></TD></TR>
<TR><TD>State</TD><TD><input name="state"></TD></TR>
<TR><TD>Zip</TD><TD><input name="zip"></TD></TR>
</TABLE>
<INPUT TYPE=SUBMIT>
</FORM>
<HR>
<% If Request.Form.Count > 0 %>
Street is: <%= Request.Form("street") %> <P>
City is: <%= Request.Form("city") %> <P>
State is: <%= Request.Form("state") %> <P>
Zip is: <%= Request.Form("zip") %> <P>
<HR>
<% End If %>
</BODY>
</HTML>