N342 Overview

Modified

Getting Started

You can use the IU Web server for the examples by mapping the local W: drive to the IU Web server.

To map the W: drive:

  1. Computer
  2. Map Network drive
  3. Drive: W
  4. Folder: \\iu-uits-eiwp1.ads.iu.edu\username\wwwroot
  5. Finish
  6. Login using: ads\username
                      password

Create N342 folder

  1. Computer
  2. W:
  3. Right-click, Select New | Folder
  4. N342

Basics

Web servers broadly provide 2 key functions:

  1. return an HTML file requested by the browser; for example: http://iu-uits-eiwp1.ads.iu.edu/rwisman/homepage.htm
     
  2. execute a program requested by the browser and return results: http://iu-uits-eiwp1.ads.iu.edu/rwisman/Example.Asp

For our purposes, server-side programming, we are interested primarily in the second function of executing ASP programs written in JavaScript, a language used for server and client-side programming.

Below is a server-side JavaScript program that returns the numbers 1 through 10 to the browser when executed.

<%@ LANGUAGE = JavaScript %>
<%
     for( i=1; i<=10; i++)
          Response.Write(i+"<br/>");
%>

Exercise 1  

  1. Open FrontPage.
  2. Select Code (in lower left of window).
  3. Copy the above JavaScript to a file named W:\N342\1-10.asp
  4. Execute by: iu-uits-eiwp1.ads.iu.edu/username/N342/1-10.asp

Response.Write

Writes or outputs text to the browser, the browser then renders the text. In the above example, the:

Response.Write(i+"<br/>");

writes the string

1<br/>2<br/>...

Note that either ' or " can be used to start and end a string; so

"x'abc'y" is the string x'abc'y

and

'x"abc"y' is the string x"abc"y.

 

ASP programs can also input from the browser; the simplest approach is to add the input on the address as a variable=value pair.

The following prints back whatever the value of variable nickName.

<%@ LANGUAGE = JavaScript %>
<%
    s = Request( "nickName" );
    Response.Write( s );
%>

Exercise 2

  1. Copy the above JavaScript to a file named W:\N342\echo.asp
  2. Execute by: iu-uits-eiwp1.ads.iu.edu/username/N342/echo.asp?nickName=Ray

Request( "variable" )

Reads or inputs the value of a variable from the browser. In the above example, the s = Request( "nickName" ); reads the value of variable nickName.

 

While the variables and values can be entered with the address, normally an HTML form is used instead.

<form action='echo.asp'>
    Enter a nickname:<input type='text' name='nickName' value='' />
</form>

Exercise 3

  1. Copy the above HTML to a file named W:\N342\echo.htm
  2. Execute by: iu-uits-eiwp1.ads.iu.edu/username/N342/echo.htm
  3. Enter a name and press Enter. Notice the browser address line.

<form action='program.ASP'>

Executes the ASP program and sends along any form variables when form is submitted.

The above form has one variable: nickName.

 

We now have the tools to create simple server programs.

The below program prints one of two persons name, given their nickname.

<%@ LANGUAGE = JavaScript %>
<%
      if( Request( 'nickName' ) == 'Ray')
          Response.Write( '<h1>Raymond Wisman</h1>');
      else if( Request( 'nickName' ) == '43')
                 Response.Write( '<h1>George Bush</h1>');
            else
                 Response.Write( '<h1>Unknown nickname</h1>');
%>

Exercise 4

  1. Copy the above JavaScript to a file named W:\N342\fullName.ASP
  2. Execute by: iu-uits-eiwp1.ads.iu.edu/username/N342/fullName.ASP?nickName=Ray
  3. Notice the browser address line.

Question: What changes are necessary to echo.htm for executing fullName.ASP?

 

Access Database

Exercise 4 above implements a simple lookup using if-statements to transform nicknames to a full name. Should the program data, the nicknames change, the program would need to be modified.

Relational database systems, such as Access, have been designed to support general lookup and other, common data functions.

In the following, we use an Access database table to implement the same functionality as in Exercise 4.

The database NameDB has one table NameTable that contains a nickName and fullName field with the following entries.

A query can select from the table only the records and fields of interest.

For example, the SQL query:

SELECT fullName FROM NameTable WHERE nickName='43'

returns a set of records (record set) that match the criteria nickName='43', in this case the record consisting of fullName 'George Bush' is returned.

Exercise 5

  1. Download, unzip and save to your W:\N342 drive the Access database file NameDB.mdb
  2. Use Access to query the NameTable to return the fullName field for all records.

In Access 2007:

  • Open the NameDB database and the NameTable table.
  • Create | Query Design
  • Add NameTable | Close
  • View | SQL View
  • Edit the SQL statement to: SELECT fullName FROM NameTable WHERE nickName='43'
  • Click Design | Run to execute the SQL and return the resulting record set.

 

Access Database + ASP

ASP programs can execute SQL statements identical to database frontend such as Access. This allows server programs to utilize a database system to perform complex data functions simply. Although we are using Access, the SQL used is generally interoperable with other database systems such as Oracle, Microsoft, etc.

The following ASP program implements the SQL statement used above:

<%@ LANGUAGE = JavaScript %>
<%
      conn = Server.CreateObject("ADODB.Connection");

      conn.Open ("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath('NameDB.mdb'));

      rs = conn.Execute("SELECT fullName FROM NameTable WHERE nickName='43");

      Response.Write( rs( "fullName" ) );
%>

The results displayed by the browser always would be George Bush because the nickName='43'.

Recall that the browser can send a variable to the ASP program with the address, as in:

    iu-uits-eiwp1.ads.iu.edu/username/N342/fullName.ASP?nickName=Ray

To use the nickName variable in the SQL query, the '43' can be changed to Request( 'nickName' ). The main challenge is in matching quotation marks (" and ') as you will see in the below program.

Remember that either ' or " can be used to start and end a string; so "x'abc'y" is the string x'abc'y and 'x"abc"y' is the string x"abc"y.

<%@ LANGUAGE = JavaScript %>
<%
      conn = Server.CreateObject("ADODB.Connection");

      conn.Open ("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath('NameDB.mdb'));

      rs = conn.Execute("SELECT fullName FROM NameTable WHERE nickName= '" + Request( 'nickName' ) + "");

      Response.Write( rs( "fullName" ) );
%>

When Request( 'nickName' ) = '43' the SQL is the same as in the earlier query.

Exercise 6

  1. Copy the above JavaScript program to a file named W:\N342\fullNameDB.ASP
  2. Execute by: iu-uits-eiwp1.ads.iu.edu/username/N342/fullNameDB.ASP?nickName=Ray
  3. The program returns the fullName given the nickName. Change the program to return the nickName field given the fullName.

 

Record Set Operations

The SQL SELECT statement result is a recordset.

In previous examples, the variable rs held the records resulting from executing the SELECT statement.

   

For example:

rs = conn.Execute("SELECT fullName FROM NameTable");

results in rs holding the fullName fields returned from all the records of NameTable.

Operations on the recordset include accessing the value of a returned record. For example:

rs = conn.Execute("SELECT fullName FROM NameTable");

rs( "fullName" );

would have the value:

George Bush

because the first record of the recordset has fullName of George Bush.

Four basic recordset operations are:

Operation Example
Field value rs( "fullName" )
End of recordset rs.EOF
Move to next record rs.MoveNext()
Close rs.Close()

EOF is needed to determine whether the recordset is empty or at the End Of File.

The following returns either the fullName field value or a not found message.

<%@ LANGUAGE = JavaScript %>
<%
      conn = Server.CreateObject("ADODB.Connection");

      conn.Open ("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath('NameDB.mdb'));

      rs = conn.Execute("SELECT fullName FROM NameTable WHERE nickName='" + Request( 'nickName' ) + "'  ");

      if( rs.EOF )
           Response.Write( Request( 'nickName' ) + " not found." );
      else
           Response.Write( rs("fullName") );
%>

For URL:

iu-uits-eiwp1.ads.iu.edu/username/N342/fullNameDB.ASP?nickName=Ray

the SELECT is:

SELECT fullName FROM NameTable WHERE nickName='Ray'

returning rs.fullName = Raymond Wisman

and rs.EOF is false.

 

For URL:

iu-uits-eiwp1.ads.iu.edu/username/N342/fullNameDB.ASP?nickName=Fred

the SELECT is:

SELECT fullName FROM NameTable WHERE nickName='Fred'

and rs.EOF is true.

 

MoveNext is needed to move to the next record of the recordset. The following returns to the browser the fullName field value of all records selected.

<%@ LANGUAGE = JavaScript %>
<%
      conn = Server.CreateObject("ADODB.Connection");

      conn.Open ("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath('NameDB.mdb'));

      rs = conn.Execute("SELECT fullName FROM NameTable");

      while( !rs.EOF ) {

            Response.Write( rs("fullName") + "<br/>");

            rs.MoveNext();
      }
%>

Outputs:

George Bush

Raymond Wisman

Records are accessed sequentially in the recordset so that the first time rs("fullName")  is executed the value is 'George Bush'.

rs.MoveNext() moves to the next record so that the second time rs("fullName")  is executed the value is 'Raymond Wisman'.

Exercise 7

  1. Copy the above JavaScript to a file named W:\N342\fullNameRS.ASP
  2. Execute by: iu-uits-eiwp1.ads.iu.edu/username/N342/fullNameRS.ASP
  3. Change the program to display to the browser the nickName and fullName fields of all records.
  4. After displaying all recorders in the browser, view the source.

 

Repeated Execution

ASP programs are generally executed from HTML in one of two ways:

  1. Link.
     
    <a href="fullName.asp">Execute fullName.asp program</a>

     

  2. Submitting a form.
     
    <form action='fullName.asp'>
        <input type='text' name='nickName' value=' '  />
    </form>

To repeat the execution of a server program, the necessary HTML can be generated by the program itself.

For example, the following extends our earlier example to look up the nickname but also generates the HTML of a form for another lookup.
 

fullNameRepeated.asp
<%@ LANGUAGE = JavaScript %>
<%
      conn = Server.CreateObject("ADODB.Connection");

      conn.Open ("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath('NameDB.mdb'));

     rs = conn.Execute("SELECT fullName FROM NameTable WHERE nickName='" + Request( 'nickName' ) + "'  ");

      if( rs.EOF )
           Response.Write( Request( 'nickName' ) + " not found." );
      else
           Response.Write( rs("fullName") );
     
      Response.Write("<form action='fullNameRepeated.asp'>");
      Response.Write("    <input type='text' name='nickName' />");
      Response.Write("</form>");

%>

Exercise 8

  1. Copy the above JavaScript to a file named W:\N342\fullNameRepeated.ASP
  2. Execute by: iu-uits-eiwp1.ads.iu.edu/username/N342/fullNameRepeated.ASP