Chapter: Internet & World Wide Web HOW TO PROGRAM - Active Server Pages (ASP)

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Simple ASP Examples

In this section, we present simple ASP examples. The first example (Fig. 25.2) sends the Web server’s date and time to the client as XHTML markup.

Simple ASP Examples

 

In this section, we present simple ASP examples. The first example (Fig. 25.2) sends the Web server’s date and time to the client as XHTML markup.

 

          <% @LANGUAGE = VBScript %>

 

          <%

          ' Fig. 25.2 : clock.asp

          ' A simple ASP example

          Option Explicit

          %>

          <!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">

 

 

          <head>

          <title>A Simple ASP Example</title>

         

          <style type = "text/css">

          td      {        background-color: black;

                             color: yellow }

          strong         {        font-family: arial, sans-serif;

                             font-size: 14pt; color: blue }

          p{ font-size: 14pt }

          </style>

         

          </head>

         

          <body>

         

          <p><strong>A Simple ASP Example</strong></p>

          <table border = "6">

          <tr>

          <td>

          <% =FormatDateTime( Now, vbLongDate ) %>

          </td>

         

          <td> 

          <% =Time() %>  

          </td>

          </tr> 

          </table>

          </body>

 

          </html>

 


Fig. 25.2 Simple Active Server Page

 

Notice the scripting delimiters <% and %> wrapped around the VBScript code— these delimit the scripting code that is executed on the server, not the client. Script enclosed in scripting delimiters is not sent to the client; it is processed by the scripting engine. How-ever, the scripting code inside the delimiters can generate information that is sent to the client. Everything outside <% and %> is simply written to the client.

 

Line 1 uses the optional @LANGUAGE processing directive to specify VBScript as the scripting language. This indicates the scripting engine needed to interpret the scripting code. In this chapter, we use VBScript exclusively to develop our Active Server Pages, although other scripting languages, such as JavaScript, may be used. If the @LANGUAGE processing directive is omitted, VBScript is the default.

 

The Option Explicit statement in line 6 indicates that the programmer must explicitly declare all VBScript variables. Remember that by simply mentioning a new name, VBScript variables are implicitly declared. This can lead to subtle errors. When used, the Option Explicit statement must be the first VBScript scripting statement after the @LANGUAGE directive. In this particular example, variables are not declared but the Option Explicit statement is included as a good programming practice.

 

Line 33 calls VBScript function FormatDateTime to return a string formatted according to the server’s date and time. This function accepts two arguments, the date and the format in which to return the date. We call VBScript function Now to get the current date and specify the vbLongDate format, which indicates that the day, time, month and year should be displayed. This statement is short for

 

<% Call Response.Write( FormatDateTime( Now, vbLongDate ) ) %>

 

which calls the Response method Write to send the formatted date as text to the client. Line 37 calls VBScript function Time to get the current time on the server. Function

Time returns the time in the format, hh:mm:ss.This statement is short for

 

<% Call Response.Write( Time() ) %>

 

which calls the Response method Write to send the time as text to the client.

Fig. 25.3 shows the XHTML generated by clock.asp that is rendered in the client browser. This is what the user would see by selecting the View menu’s Source command in Internet Explorer. As you can see, server-side scripts, unlike client-side scripts, are not viewable by the client.

 

 

    <!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">

 

          <head>

          <title>A Simple ASP Example</title>

         

          <style type = "text/css">

          td      {        background-color: black;

                             color: yellow }

          strong         {        font-family: arial, sans-serif;

                             font-size: 14pt; color: blue }

          p{ font-size: 14pt }

          </style>

         

          </head>

         

          <body>

<p><strong>A Simple ASP Example</strong></p>  

            <table border = "6">         

            <tr>    

            <td>   

            Thursday, May 24, 2001  

            </td>  

                       

            <td>   

            2:22:58 PM  

            </td>  

            </tr>   

            </table>

            </body>

 

            </html>

 

 

Fig. 25.3 Viewing the XHTML generated by Fig. 25.2

 

 

ASP is also used to process form input. Data entered into a form can be sent to the server, processed and then sent back to the client in a different format. For example, an e-commerce site may use this to verify a customer’s order information. The order information is entered into the form and then sent to the server for processing. Once the information is received, the server may return an order confirmation page, for verification purposes, that displays all the information the customer entered into the form.

 

Fig. 25.4 shows how to pass information from a form to an .asp document using the post method. The action attribute of the form element indicates the .asp file to which the form information is posted.

In line 27 the Request object retrieves the form data posted to name.asp and returns the contents of the namebox field as XHTML to the client.

 

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                  

          <!-- Fig. 25.4 : name.html        -->

          <!-- XHTML document that request an ASP document -->

                  

          <html xmlns = "http://www.w3.org/1999/xhtml">       

                  

          <head>

          <title>Name Request</title>

          </head>

         

          <body>

         

          <p style = "font-family: arial, sans-serif">

          Enter your name:

          </p>

         

          <!-- request name.asp when posted -->

XHTML document that requests an ASP (part 1 of 2).

 

<form action = "name.asp" method = "post">

<input type = "text" name = "namebox" size = "20" />

<input type = "submit" name = "submitButton"

value = "Enter" />

</form>

 

</body>

 

</html>


Fig. 25.4 XHTML document that requests an ASP

 

 

When the Enter button is pressed, the form data is posted to name.asp (Fig. 25.5). ASP name.asp processes the form data and returns XHTML to the client.

<% @LANGUAGE = VBScript %>

 

          <%

          ' Fig. 25.5 : name.asp

          ' Another simple ASP example

          Option Explicit

          %>

 

          <!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">

 

 

          <head>

          <title>Name Information</title>

         

          <style type =        "text/css">

          p       {        font-family: arial, sans-serif;

                             font-size: 14pt; color: navy }

          .special { font-size: 20pt; color: green }

          </style>

          </head>

 

          <body>

 

          <!-- retrieve and display namebox's value -->

          <p>Hi <% =Request( "namebox" ) %>, </p><br />

          <p class = "special">Welcome to ASP!</p>

 

          </body>

 

          </html>

 


Fig. 25.5 ASP document that responds to a client request

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.