Server-Side Development
Jakarta EE is typically used for server-side development. Most of its APIs are powerful and used for server-side processing and management.
This chapter will provide you with some common and useful scenarios that you may face as a Jakarta EE developer and will show you how to deal with them.
In this chapter, we will cover the following recipes:
- Using Jakarta CDI to inject context and dependencies
- Using Jakarta Bean Validation for data validation
- Using Jakarta Servlet for request and response management
- Using Server Push to make objects available beforehand
- Using EJB and JTA for transaction management
- Using EJB to deal with concurrency
- Using JPA for smart data persistence
- Using EJB and JPA for data caching
- Using Jakarta Batch processing
Let's get started!
Using Jakarta CDI to inject context and dependencies
Jakarta Contexts and Dependency Injection (CDI) is one of the most important APIs under the Jakarta EE umbrella. Introduced in Java EE 6, it now has a big influence over many other APIs.
In the recipe, you will learn how to use Jakarta CDI in a couple of different ways and situations.
Getting ready
First, let's add the required dependency:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
How to do it...
Using Jakarta Bean Validation for data validation
You can use Bean Validation to constrain your data in many different ways. In this recipe, we are going to use it to validate a JSF form so that we can validate it as soon as the user tries to submit it and avoid any invalid data right away.
Getting ready
First, we need to add our dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
How to do it...
You need...
Using Jakarta Servlet for request and response management
Jakarta Servlet was created even before Jakarta EE existed – actually, before J2EE existed! It became part of EE in J2EE 1.2 (Servlet 2.2) in 1999.
This is a powerful tool that's used to deal with request/response contexts. This recipe will show you an example of how to do this.
Getting ready
Let's add our dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
How to do it...
...Using Server Push to make objects available beforehand
One of the most important new features of Jakarta Servlet 4 is its HTTP/2.0 support. It brings another cool and reliable feature – Server Push.
This recipe will show you how to use Server Push in a filter and push the resources needed in every request that we want.
Getting ready
First, we need to add the required dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
How to do it...
Using EJB and JTA for transaction management
Jakarta Transaction (formerly JTA) is an API that enables distributed transactions over the Jakarta EE environment. It is most powerful when you delegate transaction management to the server.
This recipe will show you how to do this!
Getting ready
First, add the required dependencies:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version...
Using EJB to deal with concurrency
Concurrency management is one of the biggest advantages supplied by a Jakarta EE server. You can rely on a ready environment to deal with this tricky topic.
This recipe will show you how to set up your own Jakarta Enterprise Beans (formerly EJBs) to use it!
Getting ready
To get ready, you simply need to add a Jakarta EE dependency to your project:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
How to do it...
Using JPA for smart data persistence
Jakarta Persistence (formerly JPA) is a specification that describes an interface for managing relational databases using Jakarta EE.
It eases data manipulation and reduces a lot of the code written for it, especially if you are used to the SQL American National Standards Institute (ANSI).
This recipe will show you how to use it to persist your data.
Getting ready
First, let's add the required dependencies:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId...
Using EJB and JPA for data caching
Knowing how to build a simple and local cache for your application is an important skill. It may have a big impact on some data access performance and is quite easy to do.
This recipe will show you how to do this.
Getting ready
Simply add a Jakarta EE dependency to your project:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
How to do it...
You need to perform the following steps...
Using Jakarta Batch processing
Running background tasks is a useful and important skill in an enterprise context.
You could use it to process data in bulk or just to separate it from the UI processes. This recipe will show you how to do this.
Getting ready
Let's add our dependencies:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
...