Sun Java System Application Server 

Samples Index

The Java Persistence / Servlet Sample Application

This sample demonstrates the use of Java Persistence APIs based on the new Java Persistence specification within a servlet.

If you are familiar with EJB 2.1 persistence, you will notice many changes in this example based on the new Java Persistence specification:

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 defined as index.jsp which forwards to the ListPerson servlet. The servlet queries the list of Person records from the database using Java Persistence API. The list is passed on to ListPerson.jsp to render. You can click the Create a Person Record link at bottom of the listing to go to CreatePerson.jsp. This renders a form where you can enter data for a new Person record. When you click the CreateRecord button, the form is submitted to CreatePerson Servlet which creates a new Person record in the database using Java Persistence API.

Please note the pattern used to obtain an EntityManager instance in the servlets. It is always obtained from injected EntityManagerFactory. Here is a discussion about why this pattern is used instead of directly injecting an EntityManager.

Application Archive

The web-module is archived in web-jp-war.war.

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/web-jpa-war as the project.
  3. Right click on web-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/web-jpa-war/
      run-display-browser:
      run:
      BUILD SUCCESSFUL (total time: 2 seconds)
      
    Browser output
      http://localhost:8080/web-jpa-war/
    
      List of Persons currently in Database
      ID    FirstName   LastName
    
      Create a Person Record
      

Troubleshooting

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

 


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