Table of Contents | Previous | Next | Index


Chapter 13
Videoapp and Oldvideo Sample Applications

This chapter describes the videoapp sample application, which illustrates the use of the LiveWire Database Service. It describes how to configure your environment to run the videoapp and oldvideo sample applications.

This chapter contains the following sections:


About Videoapp and Oldvideo Sample Applications

Netscape servers come with two sample database applications, videoapp and oldvideo, that illustrate the LiveWire Database Service. These applications are quite similar; they track video rentals at a fictional video store. The videoapp application demonstrates the use of the DbPool and Connection objects. The oldvideo application demonstrates the use of the predefined database object.

There are a small number of restrictions on the use of these applications:


Configuring Your Environment

Before you can run these applications, you must make minor changes to the source files and create a database of videos. This section tells you which files you must change and which procedures you use to make these changes and to create the database for each of the supported database servers. See the section for your database server for specific information.

NOTE: Your database server must be up and running before you can create your video database, and you must configure your database server and client as described in Chapter 10, "Configuring Your Database."
In addition, the database-creation scripts use database utilities provided by your database vendor. You should be familiar with how to use these utilities.

Connecting to the Database and Recompiling

The videoapp application is in the $NSHOME\js\samples\videoapp directory, where $NSHOME is the directory in which you installed the Netscape server. The oldvideo application is in the $NSHOME\js\samples\oldvideo directory.

For each application, you must change the connect string in the HTML source file, start.htm, to match your database environment. For information on the parameters you use to connect, see "Database Connection Pools;" for even more information, see the description of the connect method in the Server-Side JavaScript Reference.

For the videoapp application, change this line:

project.sharedConnections.pool = 
   new DbPool ("<Server Type>", "<Server Identifier>",
      "<User>", "<Password>", "<Database>", 2, false)
For the oldvideo application, change this line:

database.connect ("INFORMIX", "yourserver", "informix",
   "informix", "lw_video")
Save your changes and recompile the application. To recompile one of the applications from the command line, run its build file, located in the application's directory. Be sure your PATH environment variable includes the path to the compiler (usually $NSHOME\bin\https).

Restart the application in the JavaScript Application Manager.

Creating the Database

There are two sets of creation scripts for videoapp and oldvideo, in their respective application directories. The sets of scripts are identical. If you run one set, both applications will be able to use the database.

The first time you run the scripts you might see errors about dropping databases or tables that do not exist. These error messages are normal; you can safely ignore them.

Informix

Before using the following instructions, you must configure your Informix client as described in "Informix." In addition, make sure your PATH environment variable includes $INFORMIXDIR\bin and that your client is configured to use the Informix utilities.

The SQL files for creating the video database (lw_video) on Informix are in these two directories:

$NSHOME\js\samples\videoapp\ifx 
$NSHOME\js\samples\oldvideo\ifx
NOTE: Remember that path names in this manual are given in NT format if they are for both NT and Unix. On Unix, you would use $NSHOME/js/samples/videoapp/ ifx.

  1. On Unix, log in as "informix" user and run the ifx_load.csh shell script for videoapp and for oldvideo.
  2. On NT, in the Informix Server program group, double-click the Command-Line Utilities icon to open a DOS window, and then run the following commands:

    cd c:\netscape\server\js\samples\videoapp\ifx
    ifx_load.bat

    You can also run the commands from the oldvideo\ifx directory:

  3. You can now run the application by making the changes described in "Connecting to the Database and Recompiling."

Oracle

Before using the following instructions, you must configure your Oracle client as described in "Oracle." In addition, your client must be configured to run the Oracle utilities. To run SQLPlus, you may need to set the ORACLE_SID environment variable.

The SQL files for creating the video database on Oracle are in these two directories:

$NSHOME\js\samples\videoapp\ora
$NSHOME\js\samples\oldvideo\ora

  1. On both Unix and NT, start SQL Plus. From the SQL> prompt, enter this command:
  2. Start $NSHOME\js\samples\videoapp\ora\ora_video.sql

    You can also run the script from the oldvideo directory. This SQL script does not create a new database. Instead, it creates the Oracle tables in the current instance.

  3. On Unix, run the ora_load script file to load the video tables with data. On NT, run the ora_load.bat batch file to load the video tables with data. You must edit the appropriate file to connect to your server; the instructions for doing so are in the file.
  4. You can now run the application by making the changes described in "Connecting to the Database and Recompiling."

Sybase

Before using the following instructions, you must configure your Sybase client as described in "Sybase." In addition, on Unix be sure your PATH environment variable includes $SYBASE\bin and set DSQUERY to point to your server.

The SQL files for creating the video database on Sybase are in these two directories:

$NSHOME\js\samples\videoapp\syb
$NSHOME\js\samples\oldvideo\syb

  1. Run the appropriate script from the command line. On Unix, the script is:
  2. syb_video.csh userid password

    For example:

    $NSHOME\js\samples\videoapp\syb\syb_load.csh sa

    On NT, the script is:

    syb_load userid password

    For example:

    c:\netscape\server\js\samples\videoapp\syb\syb_load sa

    Alternatively, you can run the script from the oldvideo directory.

  3. You can now run the application by making the changes described in "Connecting to the Database and Recompiling."
NOTE: If you have both Sybase and MS SQL Server or DB2 installed on your machine, there is a potential naming confusion. These vendors have utilities with the same name (bcp and isql). When running this script, be certain that your environment variables are set so that you run the correct utility.

Microsoft SQL Server (NT only)

Before using the following instructions, you must configure your Sybase client as described in "ODBC." In addition on Unix, set DSQUERY to point to your server.

The SQL files for creating the video database on MS SQL Server are in these two directories:

$NSHOME\js\samples\videoapp\mss
$NSHOME\js\samples\oldvideo\mss

  1. From a DOS prompt, run this batch file:
  2. mss_load userid password

    For example:

    c:\netscape\server\js\samples\videoapp\mss\mss_load sa

  3. You can now run the application by making the changes described in "Connecting to the Database and Recompiling."
NOTE: If you have both MS SQL Server and Sybase or DB2 installed on your machine, there is a potential naming confusion. These vendors have utilities with the same name (bcp and isql). When running this script, be certain that your environment variables are set so that you run the correct utility.

DB2

The SQL files for creating the video database on DB2 are in these two directories:

$NSHOME\js\samples\videoapp\db2
$NSHOME\js\samples\oldvideo\db2

  1. (Unix only) Your PATH environment variable must include the $DB2PATH/bin, $DB2PATH/misc, and $DB2PATH/adm directories.
  2. Before you can run these scripts, you must have installed the DB2 Software Developer's Kit (DB2 SDK).
  3. Also, before you can run the script to create the tables, you must edit it to modify some parameters. On Unix, the script is in db2_load.csh; on NT, it is in db2_load.bat. Edit the appropriate db2_load file and modify the following parameters to reflect your environment:
  4. Make sure your /etc/services file has entries for your instance or service name if you are creating the database in a remote DB2 server.
  5. Run the appropriate version of the script from the DB2 command window. The db2_load script runs the db2_video.sql and import.sql scripts. These subsidiary scripts create the video tables and load them with data from the *.del files. They do not create a new database. Instead, they create the DB2 tables in the local database alias specified in the db2_load script.
NOTE: If you have both DB2 and Sybase or MS SQL Server installed on your machine, there is a potential naming confusion. These vendors have utilities with the same name (bcp and isql). When running this script, be certain that your environment variables are set so that you run the correct utility.

Running Videoapp

In this section, you get the videoapp sample application up and running. This sample is significantly more complex than the samples discussed in Chapter 4, "Quick Start with the Sample Applications." This chapter only gives an overview of it. You should look at some of the files to start familiarizing yourself with it.

Once you have created the video database and changed the database connection parameters, you can access the application here:

http://server.domain/videoapp
After connecting to the database, the Application Manager displays the videoapp home page, as shown in Figure 13.1.

Figure 13.1   Videoapp home page

If you cannot connect to the database, you see an error message. Make sure you have entered the correct database connection parameters, as described in "Connecting to the Database and Recompiling," recompiled, and restarted the application.

The first thing you must do when you're connected is to add a new customer. Until you have done this, there are no customers to use for any of the other activities.

You can use videoapp as a customer or as an administrator. As a customer, you can:

As an administrator, you can:

Run the application and make a few choices to perform different actions.


Looking at the Source Files

The source HTML files for videoapp, listed in the following table, are copiously commented.

Table 13.1 Primary videoapp source files  

home.htm

The application default page. Has links to pick.htm, status.htm, rentals.htm, customer.htm, and delete.htm. If not connected to the database, this page redirects the client to start.htm.

start.htm

Connects the application to the database, starts a transaction, and then redirects back to home.htm.

abort.htm

Cancels a transaction and begins a new transaction.

save.htm

Commits a transaction and begins a new transaction.

pick.htm

Allows the customer to rent a movie. It contains frames for category.htm, videos.htm, and pickmenu.htm.

The category.htm file displays video categories.

The videos.htm file displays all videos in selected category, linked to rent.htm to rent a particular video.

The pickmenu.htm file displays choices of other pages to visit.

status.htm 

Displays the videos the customer currently has rented. If the customer has not selected an ID, redirects to client.htm, which lets the customer select the ID.

rentals.htm

Displays a list of all rented videos. When the administrator clicks on one, it submits the choice to return.htm, which performs the logic to return the video, then redirects back to rentals.htm.

customer.htm

Allows the administrator to add a new customer. Submits form input to add.htm, which performs logic to add a customer, then redirects back to customer.htm.

delete.htm

Allows the administrator to delete a customer. Displays a list of customers with links to remove.htm, which deletes the specified row from the customer table, then redirects back to delete.htm.

modify.htm

Allows the administrator to modify a customer entry. Displays a list of the first five customers with links to modify1.htm and modify2.htm. Those pages update a specific row in the customer table and then redirect back to modify.htm. The modify3.htm file displays additional customers five at a time.

Application Architecture

This section orients you to the implementation of some of the functionality in videoapp. It describes only how the application works with the database and details the procedure for renting a movie. Other tasks are similar.

Connection and Workflow

When a user initiates a session with videoapp by accessing its default page (home.htm), videoapp checks whether it is already connected to the database. If so, videoapp assumes not only that the application is connected, but also that this user is already connected, and it proceeds from there.

If not connected, videoapp redirects to start.htm. On this page, it creates a single pool of database connections to be used by all customers, gets a connection for the user, and starts a database transaction for that connection. It then redirects back to home.htm to continue. The user never sees the redirection.

The database transaction started on start.htm stays open until the user explicitly chooses either to save or discard changes, by clicking the Save Changes or Abort Changes button. When the user clicks one of those buttons, save.htm or abort.htm is run. These pages commit or roll back the open transaction and then immediately start another transaction. In this way, the customer's connection always stays open.

Once it has a database connection, videoapp presents the main page to the user. From that page, the user makes various choices, such as renting a movie or adding a new customer. Each of those options involves displaying various pages that contain server-side JavaScript statements. Many of those pages include statements that use the connection to interact with the database, displaying information or making changes to the database.

The first thing you must do when you're connected is to add a new customer. Until you have done this, there are no customers to use for any of the other activities.

Renting a Movie

The pick.htm page contains a frameset for allowing a customer to rent a movie. The frameset consists of the pages category.htm, videos.htm, and pickmenu.htm.

The category.htm page queries the database for a list of the known categories of movie. It then displays those categories as links in a table in the left frame. If the user clicks one of those links, videoapp displays video.htm in the right frame. There are a few interesting things about the server-side code that accomplishes these tasks. If you look at this page early on, you see these lines:

var userId = unscramble(client.userId)
var bucket = project.sharedConnections.connections[userId]
var connection = bucket.connection
These statements occur in most of videoapp's pages. They retrieve the connection from where it is stored in the project object. The next line then gets a new cursor applicable for this task:

cursor = connection.cursor("select * from categories");
A variant of this statement occurs at the beginning of most tasks.

Here is the next interesting set of statements:

<SERVER>
...
while (cursor.next()) {
   catstr = escape(cursor.category)
</SERVER>
<TR><TD><A HREF=`"videos.htm?category=" + catstr` TARGET="myright">
<SERVER>write(cursor.category);</SERVER></A>
</TD>
</TR>
<SERVER>
} // bottom of while loop
This loop creates a link for every category in the cursor. Notice this statement in particular:

<A HREF=`"videos.htm?category=" + catstr` TARGET="myright">
This line creates the link to videos.htm. It includes the name of the category in the URL. Assume the category is Comedy. This statement produces the following link:

<A HREF="videos.htm?category=Comedy" TARGET="myright">
When the user clicks this link, the server goes to videos.htm and sets the value of the request object's category property to Comedy.

The videos.htm page can be served either from pick.htm or from category.htm. In the first case, the category property is not set, so the page displays a message requesting the user choose a category. If the category property is set, videos.htm accesses the database to display information about all the movies in that category. This page uses the same technique as category.htm to construct that information and create links to the rent.htm page.

The rent.htm page actually records the rental for the customer. It gets information from the request and then updates a table in the database to reflect the new rental. This page performs the update, but does not commit the change. That doesn't happen until the user chooses Save Changes or Abort Changes.

The pickmenu.htm page simply displays buttons that let you either return to the home page or to the page for adding a new customer.

Modifying videoapp

As way of getting used to the LiveWire functionality, consider modifying videoapp. Here are some features you might add:


Table of Contents | Previous | Next | Index

Last Updated: 09/29/99 18:02:00

© Copyright � 1999 Sun Microsystems, Inc. Some preexisting portions Copyright � 1999 Netscape Communications Corp. All rights reserved.