Previous Contents Index Next |
iPlanet Web Server, Enterprise Edition Server-Side JavaScript Guide |
Chapter 13 Videoapp Sample Application
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 sample application.This chapter contains the following sections:
About the Videoapp Sample Application
About the Videoapp Sample Application
Netscape servers come with a sample database application, videoapp, which illustrates the LiveWire Database Service. This application tracks video rentals at a fictional video store. The videoapp application demonstrates the use of the DbPool and Connection objects.There are a small number of restrictions on the use of these applications:
While these sample applications can be used with ODBC and SQL Server, if the driver on your platform does not support updatable cursors, the applications will not work. For information on which drivers support updatable cursors, see "Supported Database Clients and ODBC Drivers."
The videoapp application uses cursors that span multiple HTML pages. If your database driver is single-threaded, these cursors may hold locks on the database and prevent other users from accessing it. For information on which database drivers are single-threaded, see the Enterprise Server 3.x Release Notes.
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\plugins\samples\js\videoapp directory, where $NSHOME is the directory in which you installed the iPlanet Web Server.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)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\bin on Windows NT and $NSHOME\bin\https\lib on Unix).
Restart the application in the JavaScript Application Manager.
Creating the Database
The creation scripts for videoapp are in its application directory. 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 this directory:
$NSHOME\plugins\samples\js\videoapp\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.
On Unix, log in as "informix" user and run the ifx_load.csh shell script for videoapp.
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\plugins\samples\js\videoapp\ora
On both Unix and NT, start SQL Plus. From the SQL> prompt, enter this command:
Start $NSHOME\plugins\samples\js\videoapp\ora\ora_video.sql
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.
- This SQL script does not create a new database. Instead, it creates the Oracle tables in the current instance.
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\plugins\samples\js\videoapp\syb
Run the appropriate script from the command line. On Unix, the script is:
You can now run the application by making the changes described in "Connecting to the Database and Recompiling."
- For example:
$NSHOME\plugins\samples\js\videoapp\syb\syb_load.csh sa
- On NT, the script is:
- For example:
c:\netscape\server\plugins\samples\js\videoapp\syb\syb_load sa
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\plugins\samples\js\videoapp\mss
From a DOS prompt, run this batch file:
You can now run the application by making the changes described in "Connecting to the Database and Recompiling."
DB2
The SQL files for creating the video database on DB2 are in these two directories:$NSHOME\plugins\samples\js\videoapp\db2
(Unix only) Your PATH environment variable must include the $DB2PATH/bin, $DB2PATH/misc, and $DB2PATH/adm directories.
Before you can run these scripts, you must have installed the DB2 Software Developer's Kit (DB2 SDK).
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:
<nodename>: node name alias
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.<hostname>: host name of the node where the target database resides
<service-name>: service name or instance name from the services file
<database-name>: database name
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.
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:
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.
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.connectionThese 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:
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:
Change the assumption that the existence of the sharedConnections array implies that this particular user is connected. You can change start.htm to check whether there is an ID for this user in that array and whether the connection stored in that location is currently valid. See "Sharing an Array of Connection Pools."
This application never releases connections back to the pool. Consequently, once a small number of users have connected, nobody else can connect. You can modify this in a couple of ways: add a new command that lets the user indicate completion or implement a scheme to cleanup unused connections. See "Retrieving an Idle Connection."
Previous Contents Index Next
Copyright © 2000 Sun Microsystems, Inc. Some preexisting portions Copyright © 2000 Netscape Communications Corp. All rights reserved.
Last Updated August 09, 2000