Tigerfarm Tigerfarm

Computers, Localization, Travel, Southeast Asia, Sun Microsystems, Writing
« Previous month (Jul 2009) | Main | Next month (Sep 2009) »

Open Source Course Content: Web 2.0 | Frameworks | Solaris
Wednesday Aug 26, 2009

JSF 2.0, NetBeans 6.8, and GlassFish

Following are steps to get JSF 2.0 running on GlassFish, and can develop JSF 2.0 projects in NetBeans. Also, included are NetBeans project using JSF 2.0, and JSF 2.0 with some new Facelets functions.
Below is a screen shot of the JSF 2.0 selection step in NetBeans 6.8.

NetBeans 6.8 is packaged with GlassFish v3, which, I believe (I did not test), includes JSF 2.0.
Because I am running GlassFish V2, I followed the instructions to download the JSF JAR files to update my GlassFish for JSF 2.0:
Installing Mojarra 2.0.0 RC on GlassFish v2
* download the Mojarra 2.0.0 RC binary bundle bundle from the project page.
- filename: mojarra-2.0.0-RC-binary.zip
* backup your existing jsf-impl.jar found in GF_HOME/lib
* copy the new jsf-api and jsf-impl JARs to GF_HOME/lib
* edit your GF_HOME/domains/<domain-name>/config/domain.xml and
- add (or update the existing classpath-prefix) 'classpath-prefix="${com.sun.aas.installRoot}/lib/jsf-api.jar" to the java-config element
- Example: <java-config classpath-suffix="" classpath-prefix="${com.sun.aas.installRoot}/lib/jsf-api.jar" ... >
* Restart the GlassFish server
Note, the above instructions from: https://javaserverfaces.dev.java.net/nonav/rlnotes/2.0.0/releasenotes.html

Install NetBeans 6.8:
* To download NetBeans 6.8, click on 6.8 M1, from the NetBeans download page: http://www.netbeans.org/downloads/index.html
- Filename: netbeans-6.8_m1-java-windows.exe
* Run the installation.

Now that you have a JSF 2.0 GlassFish version, and NetBeans 6.8, you are ready to develop JSF 2.0 projects.
* Start NetBeans.
* Select New Project/Java Web/Web Application. In the project form, enter:
- Project Name: jsf1
- Project Location: C:\java\jsf\samples
- Click Next.
- The Server selected is your JSF 2.0 GlassFish version.
- Click Next.
- Check JavaServer Faces, which then displays JSF 2.0 configuration information.
- Click Finish.
*** You now have your first JSF 2.0 NetBeans Project ready to run.

Descriptions of the jsf1 project files, under the Projects tab: jsf1/Web Pages:
- forwardToJSF.jsp : the project's home page which uses the JSF Servlet URL Pattern (faces/) to forward the browser to the first JSF page:
<jsp:forward page="faces/template-client.xhtml"/>
- template-client.xhtml : JSF 2.0 page, which uses a Facelets template.
- template.xhtml : Facelets template
- In the directory jsf1/Web Pages/WEB-INF, are the configuration files: web.xml and faces-config.xml
* Run the project.

There are sample JSF 2.0 code which can be downloaded in the Mojarra 2.0.0 RC binary bundle. I have built one and zipped it up for your convience:
Click here to download jsf2ajax.zip, a JSF 2.0 code sample NetBeans Project.

Enjoy JSF 2.0, Stacy Thurston, Sun Learning Services developer.

Friday Aug 21, 2009

MySQL 5.1 with NetBeans 6.7

By example, this is how I installed and configured MySQL, then modified a NetBeans Java EE project to switch from using JavaDB to MySQL. Can be done in under an hour.

Download, Install, and Configure MySQL

Download MySQL 5.1: http://dev.mysql.com/downloads/mysql/5.1.html
File downloaded: mysql-5.1.37-win32.msi, about 100 meg
Install:
* double click on mysql-5.1.37-win32.msi
* Welcome page is displayed. Click Next.

* Use default, Custom, click Next.
- Default installation directory (Destination Folder): C:\Program Files\MySQL\MySQL Server 5.1\
- Open MySQL Server branch, click on MySQL Server Datafiles, click Change button.
- Set destination folder (Data Folder) to C:\sun\mysql, click OK.
- Remove documentation from install
- To be installed: MySQL Server, Client Programs, MySQL Instance Manager.
- No other changes.
- Click Next.
* Echoes the above selections. Click Install.
* Software installs. MySQL notices come up. Click Next on each.
* Wizard Completed is displayed. Configure the MysSQL Server now, is checked.
Click Finish.
* Welcome to the MySQL Server Instance Configuration Wizard is displayed. Click Next.
* Choose Standard Configuration. Click Next.
* Install As Windows Service is checked,
- Service Name: MySQL,
- Launch automatically is unchecked,
- Include Bin Directory in Windows PATH is checked.
- Click Next.
* Enter a root password, example root. Click Next.
* Ready to execute, is displayed. Click Execute.
*** The first time, it failed to start the server. I restarted my computer.
On restart, I click: Start/All Programs/MySQL/MySQL Server 5.1/MySQL Server Instance Configuration.
Note, I can run this at anytime.

The installation and configuration is completed. And an database instance has been created.

use the Windows Services Manager to start and stop the MySQL instance.


Create a Database

Sample simple commands: http://dev.mysql.com/doc/world-setup/en/world-setup.html

My steps:

>mysql -u root -p
mysql> create database studentdb;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| studentdb          |
| test               |
+--------------------+
4 rows in set (0.00 sec)

Now I have MySQL Server instance running, and a database created.


Modify the NetBeans Project to use MySQL

In NetBeans:
Under Services tab, Databases/Drivers, right mouse click MySQL (Connector/J driver) select Connect using.
In the popup, enter:

Name: MySQL (Connector/J driver)
Host: localhost
Port: 3306
Database: studentdb
User Name: root
Password: root
Remember password is checked.
Click OK

A connection is now under the Services tab, Databases. jdbc:mysql://localhost:3306/studentdb [root]

In the NetBeans Java EE EJB project:
Make a backup of the JavaDB persistence.xml file, which is under the project's setup folder: copy to persistenceJavaDB.xml.
In persistence.xml, I select Data Source: New Data Source ...
In the popup:

   JNDI Name: mysql/studentdb
   Database Connection: jdbc:mysql://localhost:3306/studentdb [root]
   Click OK


Clean and build EJB project.
Clean and build Java EE project.
Deploy and test.
*** Works with the MySQL database.


View New Rows in the Database

Use either MySQL command line utility, or Connect and Execute command in NetBeans.

>mysql -u root -p

mysql> use studentdb;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> show tables;
+---------------------+
| Tables_in_studentdb |
+---------------------+
| address             |
| dorm                |
| sequence            |
| student             |
| studentmajor        |
+---------------------+
5 rows in set (0.00 sec)

mysql> select * from studentmajor;
+----+---------+
| ID | NAME    |
+----+---------+
|  1 | English |
|  2 | Science |
+----+---------+
2 rows in set (0.00 sec)

Done, tested, and verified!.


GlassFish Resource Configuration

In the above, the persistence resource is configured using NetBeans wizards.
Following are screen prints of the configurations created in GlassFish by NetBeans. This can be used to setup a resource in GlassFish, without using NetBeans.


GlassFish Resource Configuration on Solaris