Friday, December 08, 2006

Part I: Creating the Entertain(r) Project

The Entertain project will be used to create the Entertain(r) web application, which contains MyMovies. As introduced earlier, MyMovies is a simple application used to manage/catalog my growing library of movies on DVD. It is a learning project meant as a simple how-to for those just starting to program with Java EE 5 technologies (especially JDK 1.6 and EJB 3). It focuses on using the NetBeans 5.5 IDE, Sun Java System Application Server 9, and MySQL 5.

Setup the database in MySQL
- Open MySQL Administrator and click on Catalogs. Right-click the bottom left pane and choose "Create New Schema" from the popup menu. Name the schema "entertain". This will be the database to use for the Entertain application, which contains MyMovies.

Add a JDBC resource in Sun Java SAS
- Copy the MySQL Connector/J JDBC driver JAR (mysql-connector-java-5.0.4-bin.jar) into ${SJSAS}/domain/domain1/lib/ext. This ensures that driver classes are included in the application server's classpath for the domain.
- Start the application server, if it not yet started, and log in using the Admin Console.
- Select Resources > JDBC > Connection Pools. Click [New...] to create a new pool. Name=EntertainDB; Resource Type=javax.sql.ConnectionPoolDataSource; Database Vendor=mysql. Click [Next]. Datasource Classname=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. Down under Properties, databaseName=entertain; serverName=localhost; port=3306; user=root; password=[whatever you assigned to the DB for root]. Click [Finish].
- Back in the list of connection pools, select the EntertainDB pool link and click [Ping]. If the connection was successfully set up, you will see a "Ping Successful" message.
- Now set up the JNDI resource that will be referenced in the application: select Resources > JDBC > JDBC Resources and click [New...]. JNDI Name=jdbc/entertain; Pool Name=EntertainDB. Click [OK] to create the resource.

Create a new project in NetBeans 5.5
- File | New Project ... > Enterprise > Enterprise Application > [Next]. Name="Entertain", other defaults ok [Finish].
The enterprise project created will be used to create the EJBs and webapps that we need to implement MyMovies.

Create a user-friendly URL
Just so we don't forget to deal with it later, we need to set the web module URI for the application to /entertain so that from a web browser, it can be accessed as "http://localhost:8080/entertain" (assumes SJSAS listens on its default HTTP port 8080).
- In Netbeans, right-click the Entertain-war webapp node and select Properties from the menu. Select the Run node and set Context Path=/entertain, then click [OK].

Clean up the welcome page
A index.jsp page already exists in the web module (Entertain-war > Web Pages) that will be opened when the above URL is sent from a browser. You can edit it to better suit requirements as the home page of the application.

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Entertain(r)</title>
</head>
<body>
<h1>Welcome to Entertain</h1>
Entertain is a web-based application used to manage personal entertainment artifacts.
</body>
</html>

Deploy and Test
The application cannot be deployed at this time because Java EE 5 enterprise application require at least a Session bean or Message-Driven bean.