Chapter 4. Building a Minimal Web App with Java EE in Seven Steps

Table of Contents

1. Java Basics
2. Step 1 - Set up the Folder Structure
3. Step 2 - Write the Model Code
3.1. Storing Book objects in a database table books
3.2. Creating a new Book instance and storing it
3.3. Retrieving all Book instances
3.4. Updating a Book instance
3.5. Deleting a Book instance
3.6. Creating test data
3.7. Clearing all data
4. Step 3 - Configure the App
4.1. Create the EntityManager and UserTransaction objects
4.2. Configure the JPA database connection
4.3. Create the main template
4.4. Define the managed beans needed in facelets
4.5. Build the WAR file and deploy it to TomEE
5. Step 4 - Implement the Create Use Case
6. Step 5 - Implement the Retrieve/List All Use Case
7. Step 6 - Implement the Update Use Case
8. Step 7 - Implement the Delete Use Case
9. Style the User Interface with CSS
10. Run the App and Get the Code
11. Possible Variations and Extensions
11.1. Using resource URLs
11.2. Using an alternative DBMS
12. Points of Attention
12.1. Boilerplate code
12.2. Offline availability
12.3. Architectural separation of concerns
13. Practice Projects
13.1. Project 1 - Managing information about movies
13.2. Project 2 - Managing information about countries
14. Quiz Questions
14.1. Question 1: Casting types in Java
14.2. Question 2: JPA standard identifier property
14.3. Question 3: Valid Java Bean classes
14.4. Question 4: HTML5 attributes with JSF forms
14.5. Question 5: JPQL query

In this chapter, we show how to build a simple Java EE web application using the Java Persistence API (JPA) for object-to-storage mapping and Java Server Faces (JSF) as the user interface (or "view") technology. Such an application requires a web server that supports the Java EE specifications Java Servlets, Java Expression Language (EL), JPA and JSF, such as the open source web server Tomcat/TomEE (pronounced "Tommy"). In addition to the Java code executed on the back-end computer that runs the TomEE web server, a Java EE web app also consists of HTML, CSS and possibly some auxiliary JavaScript code that is executed by a web browser running on the user's front-end computer. Since essentially all data processing is performed on the back-end, and the front-end only renders the user interface, we classify Java EE web applications as back-end web apps.

We show how to develop, deploy and run a simple example app using the TomEE web server, which is Apache Tomcat extended by adding basic Java EE features for providing an execution environment for light-weight Java EE web apps. We assume that you have already installed the TomEE Plume server and MySQL. Then simply follow our installation and configuration instructions.

The purpose of our minimal example app is to manage information about books. For simplicity, we deal with a single object type Book, as depicted in Figure 4.1.

Figure 4.1. The object type Book.

The object type Book.

The following table shows a sample data population for the model class Book.

Table 4.1. Sample data about books

ISBN Title Year
006251587X Weaving the Web 2000
0465026567 Gödel, Escher, Bach 1999
0465030793 I Am A Strange Loop 2008

What do we need for a data management app? There are four standard us e cases, which have to be supported by the app:

  1. Create a new book record by allowing the user to enter the data of a book that is to be added to the collection of stored book records.

  2. Retrieve (or read) all books from the data store and show them in the form of a list.

  3. Update the data of a book record.

  4. Delete a book record.

These four standard use cases, and the corresponding data management operations, are often summarized with the acronym CRUD.

For entering data with the help of the keyboard and the screen of our computer, we use HTML forms, which provide the user interface technology for web applications.

For any data management app, we need a technology that allows to store data in persistent records on a secondary storage device, such as a hard-disk or a solid state disk. JPA allows using a great number of different data storage technologies, including many SQL database management systems (DBMS) such as Oracle, MySQL and PostgreSQL. We don't have to change much in the application code for switching from one storage technology to another. Adding the right driver implementation to our Java runtime environment, properly setting up the DBMS and changing the database access configuration is in general all we need to do. In our example application, we explain how to set up the JPA configuration for MySQL.

1. Java Basics

Compared to JavaScript, what is different in Java? We list a few observations:

  1. No program without a class: Any Java program must include at least one class.

  2. Java is strongly typed: Properties, parameters and variables must be declared to be of some type.

  3. No global variables, no global procedures: In Java, variables, procedures and functions must be defined in the context of a class, which provides their name space.

  4. Arrays are static: Arrays have a fixed size, which cannot be changed at run-time.

  5. No object without a class: For creating an object, a class has to be used (or defined) for

    • defining the properties of the object's property slots

    • defining the methods and functions that can be applied to the object (and all other objects instantiating the class)

  6. Classes, properties and methods are defined with a visibility level (public, protected or private), which restricts their accessibility.

  7. Type parameters: Classes and complex data structures (such as lists) can be defined with the help of type parameters. See, for instance, the generics tutorial by Oracle.

  8. Java programs must be compiled before they can be executed.

  9. Speed: Java is about twice as fast as optimized JavaScript.

In Java, visibility levels are used to define the possible levels of access:

Table 4.2. Java Visibility Level

Class Package Subclass World
public y y y y
protected y y y n
no modifier y y n n
private y n n n


Normally, properties are defined as private, such that they can only be assigned with a public setter, which allows to control, and possibly log, any change of the property value. Unfortunately, Java does not allow to protect only write, but not read access. Therefore, we always have to define getters along with setters, even if they are not needed.

A Java bean class (or, simply, bean class) is a Java class with a parameter-free constructor where all properties are serializable and have a get and set method (also called "getter" and "setter"). A Java bean is an object created with the help of a bean class.

A JPA entity class (or, simply, entity class) is a Java bean class with an @Entity annotation, implying that a Java EE runtime environment (such as provided by the TomEE PLUS web server) will take care of the persistent storage of its instances.

JPA is a Java API for the management of persistent data in Java applications. It uses the Java Persistence Query Language (JPQL), a platform-independent object-oriented query language based on SQL. JPQL query expressions look very similar to SQL query expressions, but they are executed in the context of JPA entity objects.

JSF is a Java specification for building component-based user interfaces for web applications. Its current version, JSF 2, by default, uses Facelets as its view technology. By contrast, JSF 1 has used JavaServer Pages (JSP) as its default view technology.

A JSF facelet is an XML-based template that combines static UI content items with data variables for generating an XHTML user interface page.