Active Server Pages (ASP) and ASP.NET

 <  Day Day Up  >  


Microsoft's ASP is a server-side scripting environment primarily for the Microsoft Internet Information Server (IIS) Web server. A newer version called ASP.NET brings even more power to ASP programmers but with significantly more complexity. With either ASP or ASP.NET, it is possible to combine HTML, scripting code, and server-side ActiveX components to create dynamic Web applications. The ability to write scripts in standard scripting languages such as VBScript, JavaScript, or other scripting languages such as Perl, enables developers to create applications with almost any type of functionality. This makes the ASP/ASP.NET approach to server-side scripting very generalized for a broad range of applications.

To get started using ASP, the developer needs to have a working knowledge of HTML, as well as knowledge of a scripting language such as VBScript or JavaScript, which will be embedded into the ASP page. Files created for ASP have an .asp file extension while ASP.NET files use .aspx. When an ASP-enabled server sees a file with such an extension, the ASP-aware server executes it before delivering it to the user . For example, the simple VBScript embedded into the file shown here is used to dynamically display the current date on a Web page:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <script language="VBScript" runat="Server"></script>   <html xmlns="http://www.w3.org/1999/xhtml" lang="en">   <head>   <title>  ASP Example  </title>   </head>   <body>   <h1>  Breaking News  </h1>  <% = date() %>  <p>  Today the stock of a major software company  reached an all time high, making the Demo Company CEO the world's first and only trillionaire.  </p>   </body>   </html>  

The <script > tag is used to indicate the primary scripting language being employed. This element also tells the Web server to execute the script code on the server rather than the client with the runat attribute. This can be abbreviated as <%@ LANGUAGE=< script_language> %> . Notice how the <% %> is used to delimit the script code that is run. ASP is a generalized technology that can use nearly any scripting language and objects the developer may want. Think of ASP as more of a framework than a language like PHP or ColdFusion.

As a more meaningful example of ASP, let's look at database access similar to the previous ColdFusion example. As in the previous example, let's access the Positions database described earlier in the chapter. The first step in this example is to create an instance of the database component by adding the following line to an ASP file, which might be named example.asp:

  <object runat="Server" id="Conn" progid="ADODB.Connection">   </object>  

or more appropriately, just use a simple statement like the following:

 <% Set Conn = Server.CreateObject("ADODB.Connection") %> 

This statement creates an instance of a database access object called Conn that can be used with a server-side script.

Later on, the file will open a connection to the database and execute a SQL command to select job positions and return a set of records. The small code fragment shown next does this. The code is enclosed within <% and %> so that the server knows to execute this rather than display it on the screen:

 <%      Conn.Open ODBCPositions      SQL = "SELECT * FROM Positions"      SET RS = Conn.Execute(SQL)      Do While Not RS.EOF %> 

The code between the <% %> statements is VBScript, which is interpreted by the Web server when this page is requested . The Do While statement is a standard VBScript looping statement, which is used here to loop through the record set until an end of file (EOF) marker is reached, signifying the end of the records. While looping through each record, the output is displayed in the context of regular HTML code, such as displaying the Job Department field in a table cell :

  <td>  <% = RS("JobDepartment") %>  </td>  

Putting all of this together in a file called example.asp provides a complete ASP database access example:

  <%@ LANGUAGE = VBScript %>   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html xmlns="http://www.w3.org/1999/xhtml" lang="en">   <head>   <title>  Job Openings  </title>   <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />   </head>   <body>   <h2 align="center">  Open Positions  </h2>   <br /><br />   <table width="100%" border="1" cellspacing="0" cellpadding="4">   <tr>   <th>  Position Number<  /th>   <th>  Location  </th>   <th>  Description  </th>   <th>  Hiring Manager  </th>   <th>  Date Posted  </th>   </tr>   <!--  Open Database Connection        Execute SQL query statement       Set RS variable to store results of query       Loop through records while still records to process  -->   <%  Set Conn = Server.CreateObject("ADODB.Connection")     Conn.Open ODBCPositions     SQL = "SELECT JobTitle, Location, Description, HiringManager, PostDate FROM Positions"      Set RS = Conn.Execute(SQL)      Do While Not RS.EOF  %>   <!--  Display database fields in table cells  -->   <tr>   <td>   <%  = RS("JobTitle")  %>   </td>   <td>   <%  = RS("Location")  %>   </td>   <td>   <%  = RS("Description")  %>   </td>   <td>   <%  = RS("Hiring Manager")  %>   </td>   <td>   <%  = RS("Post Date")  %>   </td>   </tr>   <!-- Move to next record and continue loop -->   <%  RS.MoveNext    Loop  %>   </table>   </body>   </html>  

From this example, you can see that ASP is more complicated than PHP or ColdFusion, but it is also more like traditional programming environments. If you then add in the new .NET architecture with its objects, form controls, and support for Web services you have a very powerful programming framework indeed. More information about ASP and ASP.NET can be found at www.asp101.com and www.asp.net.



 <  Day Day Up  >  


HTML & XHTML
HTML & XHTML: The Complete Reference (Osborne Complete Reference Series)
ISBN: 007222942X
EAN: 2147483647
Year: 2003
Pages: 252
Authors: Thomas Powell

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