Java EE Web Application Technologies

This course covers the main technologies used to build enterprise web applications on the Java platform (Java EE / Jakarta EE): database access with JDBC, presentation with JSP and JSF, business components with EJB, and object-relational mapping with Hibernate and JPA. Along the four labs, a single application is progressively refactored into a clean layered architecture.

Three-tier architecture
A multi-tier architecture: presentation, business logic and data tiers. The labs build each tier with a Java EE technology. Bartledan, Wikimedia Commons, public domain

This page gives an overview of the labs. The full statements, step-by-step instructions and solutions are provided to students during the course.

Lab 1 – JDBC: database access

Goals: connect a Java application to a relational database with JDBC, externalize the configuration, manage the connection inside an object, use PreparedStatement and DatabaseMetaData, and apply the DAO and DTO patterns.

Exercises

  1. Basic JDBC connection: a HelloDB class connects to the humanresources database and prints the number of employees.
  2. Configuration and connection management: move the connection parameters to a configDB.properties file; keep a single Connection per object, reuse it and close it properly; write a reusable countRowsInTable(String tableName) method with a PreparedStatement.
  3. DAO and DTO patterns – a trip management application: DTOs carry the data between layers; the DAO implementations use JDBC internally, convert each ResultSet into DTOs and never expose Connection or ResultSet. Reflection: how to organize the code so that a change of business domain (trips → events) has minimal impact?
  4. Metadata: count all the rows of the database by listing its tables with DatabaseMetaData; optionally, generate the CREATE TABLE statement of a table from its metadata.
public class PlaceDTO {
    private Long id;
    private String name;
    // constructors, getters and setters
}

public interface PlaceDao {
    Long createPlace(PlaceDTO p);
    PlaceDTO findPlaceById(Long id);
    boolean updatePlace(PlaceDTO p);
    boolean removePlace(PlaceDTO p);
}

Takeaway: the DAO hides how data is stored; the rest of the application only sees DTOs.

Lab 2 – JSP: the MVC presentation layer

Goals: extend the trip application of Lab 1 into a layered MVC web application with JSP as the view, and increase the level of abstraction step by step.

Exercises

  1. Displaying data: a PlaceController servlet calls the DAO, stores the list of places in the request and forwards it to listPlaces.jsp. No SQL in the JSP pages.
  2. The controller: map the servlet to /places and implement doGet.
  3. EL and JSTL: replace the Java scriptlets with EL expressions and <c:forEach>; the pages only handle DTOs.
  4. User input: an addPlace.jsp form posted to the controller, which creates the place and redirects to the list.
  5. Service layer: move the business logic from the controller to a PlaceService.
  6. Reflection: which layers change when the domain changes, and how to remove JDBC from the web layer entirely or replace JSP with another view technology (Thymeleaf, REST API)?
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<ul>
    <c:forEach var="place" items="${places}">
        <li>${place.name}</li>
    </c:forEach>
</ul>

Target architecture: JSP (view) → controller (servlet) → service layer → DAO layer → database.

Takeaway: each layer has a single responsibility; a view only displays data.

Lab 3 – JSF: component-based web interfaces

Goals: build web interfaces with Jakarta Faces (managed beans and Facelets), handle forms, navigation, validation and conversion, and connect the interface to a business layer made of EJBs.

Exercises

  1. First JSF page: a HelloBean managed bean displays the current time.
  2. Deployment: package a WAR and deploy it on Apache Tomcat, then on WildFly; compare the two.
  3. Login form and navigation: redirect to a different page depending on the user.
  4. Authentication against a simulated database (an application-scoped UserRepository).
  5. Tables: display the users with <h:dataTable>.
  6. Advanced form with dates, amounts and addresses, using converters.
  7. JSF and DAO: reuse the PlaceDao to create, list and delete places from a JSF page.
  8. Validators and converters: built-in validators, a custom postal code validator, a custom Address converter and global messages.
  9. EJB integration: a stateless PlaceService EJB injected into the JSF bean, with container-managed transactions and role-based security (@RolesAllowed).
@Named
@RequestScoped
public class HelloBean {
    public String getCurrentTime() {
        return java.time.LocalTime.now().withNano(0).toString();
    }
}

<h:body>
    <h1>Hello, it is #{helloBean.currentTime}</h1>
</h:body>

Target architecture: JSF → EJB (business layer) → DAO → database.

Takeaway: EJBs bring transactions, security and concurrency management for free; they are worth it for enterprise applications, not for a simple CRUD.

Lab 4 – Hibernate and JPA: object-relational mapping

Java objects Entreprise Long idString nom Employe String nom 1..* @EntityJPA / Hibernate Relational tables ENTREPRISE ID (PK)NOM EMPLOYE NOM, ENTREPRISE_ID (FK)
Object-relational mapping: classes become tables, attributes become columns, and an association becomes a foreign key.

Goals: use the Hibernate ORM, compare XML and annotation mappings, write HQL queries, move from the Hibernate Session API to the JPA EntityManager, map relationships and inheritance, and study performance issues (N+1 queries, connection pools).

Exercises

  1. Inserting data: map an Entreprise (company) class to its table with an XML mapping file, then with JPA annotations, and compare the two approaches.
  2. Retrieving an entity by its identifier, including the case where it does not exist.
  3. HQL queries: all companies, search by name with named parameters, sorting, counting.
  4. Update and delete, and the life cycle of entities.
  5. JPA EntityManager: redo the CRUD operations through JPA, with Hibernate as the provider.
  6. Relationships: @OneToMany / @ManyToOne between companies and employees, mappedBy and cascades.
  7. Inheritance: Employe, Manager and Developer; the SINGLE_TABLE, JOINED and TABLE_PER_CLASS strategies.
  8. Lazy vs. eager loading: reproduce the N+1 queries problem, then solve it with JOIN FETCH.
  9. Connection pools: time 100 successive queries without a pool, with C3P0 and with HikariCP.
  10. DAO + Service architecture with JPA, transactions being handled in the service layer.
Hibernate native APIJPA API
SessionFactoryEntityManagerFactory
SessionEntityManager
session.save()em.persist()
session.get()em.find()
session.update()em.merge()
session.delete()em.remove()
// N+1 queries: one query for the companies, then one query per company
List<Entreprise> list = em.createQuery("FROM Entreprise", Entreprise.class)
                          .getResultList();
for (Entreprise e : list) {
    e.getEmployes().size();
}

// Solution: fetch the employees in the same query
SELECT e FROM Entreprise e JOIN FETCH e.employes

Takeaway: an ORM removes most of the JDBC code, but you still need to understand the SQL it generates.

Further reading