Sun Java System Application Server 

Samples Index

The JavaServer Faces/Java Persistence Sample Application

This sample demonstrates the use of Java Persistence APIs based on the new Java Persistence specification along with JavaServer Faces that creates a simple authentication scheme using JavaServer Faces PhaseListeners and a simple Entity bean representing a user.

If you are familiar with EJB 2.1 persistence, you will notice many changes in this example based on the new Java Persistence specification. Although this application provides the same functionality as a corresponding EJB 2.1 implementation, it is much simpler:

Additionally, JavaServer Faces can easily use JPA within its managed bean facility through resource injection, making it easier to bridge between web and EJB applications.

Application Structure

The application is built and deployed as a WAR file.

Web Module

The web module consists of:

The starting point of the application is the mainindex.html page. Follow these steps to see the application in action:
  1. Click the JavaServer Faces Welcome Page link. Because the user hasn't been authenticated, the PhaseListener will direct the user to the login page.
  2. Make an attempt to log in. The JSF managed bean, UserManager will use JPA to attempt to locate the appropriate Enity based of the provided user name. Because the example starts from scratch each time it is deployed, an error message states that the user doesn't exist.
  3. Click the Create New Account link. Fill out the form. If any fields are left empty, or the passwords do not match, error messages are provided. Click the Create button. If everything is in order, the user is directed back to the login page.
  4. Attempt to log in again, using the user name previously specified, and a bad password. This time, we locate the appropriate Entity, but because the password isn't correct, an error message is displayed.
  5. Now log in again, with the correct user name and password. This will take you to the resource originally referenced by the JavaServer Faces Welcome Page link. The welcome page should greet the user with their first and last name, and the date they registered.

Building, Deploying, and Running the Application

Follow these steps to build, deploy, and run the application.

  1. Set up your build environment and Configure the application server with which the build system has to work by following the common build instructions.
  2. Make sure the JavaDB database is started. If not, refer to common build instructions.

  3. Use the target all to build,deploy and run the project.
  4. Use the target clean to remove the temporary directories like build and dist

Building, Deploying, and Running the Application in NetBeans IDE

Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.

  1. Refer to common build instructions. for setting up NetBeans IDE and the application server with which the IDE will use.
  2. In NetBeans IDE, select File->OpenProject and select samples_install_dir/javaee5/enterprise/jsf-jpa-war as the project.
  3. Right click on jsf-jpa-war and select Run Project which will build, deploy and run the project. If configured properly, the IDE will open the browser with appropriate url. Sample output is given below.
  4. NetBeans IDE output
      All operations completed successfully
      run-deploy:
      Browsing: http://localhost:8080/jsf-jpa-war/
      run-display-browser:
      run:
      BUILD SUCCESSFUL (total time: 5 seconds)
      
    Browser output
      http://localhost:8080/jsf-jpa-war/
    
      Java Server Faces Welcome Page
      

Troubleshooting

If you have problems when running the application, refer to troubleshooting document.

 

Copyright © 2006 Sun Microsystems, Inc. All rights reserved.