Skip to content
JackSparrow414
Go back

Getting Started with Java EE: An Example Application

Table of contents

Open Table of contents

A Simple Java EE Application

A very simple Java EE application.

Prerequisites

What You Need to Know

Installing GlassFish

GlassFish is a Java EE server, whereas ordinary Tomcat is a Java SE server. You can also choose TomEE.

Download

Download, then install it.

Startup

Use asadmin in the bin folder of the GlassFish installation. Choose the appropriate .bat script for your platform or double-click it. Run the command in a terminal or DOS window:

start-domain

Start the GlassFish server. The default domain name is domain1.

After successful startup, the administration console defaults to port 4848.

localhost:4848

GlassFish administration console home page and common management entries

Viewing Logs

If startup fails, inspect the logs at:

/domains/domain1/logs under the GlassFish server home.

Shutdown

Stop the server:

stop-domain

Installing H2

Download

Download.

Startup

Run h2 in the bin directory of the H2 installation, choosing .sh or .bat for your platform. The console defaults to port 8082.

localhost:8082

JDBC URL, username, and password settings in the H2 Console login form

Configuring an H2 In-Memory Database

An H2 in-memory database URL looks like this; db1 is a custom database name.

jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1

Documentation.

For other H2 questions or further learning, see the official documentation above.

Configuring a JNDI Data Source

Why configure a JNDI data source? “I normally configure it directly in Spring Boot and have never seen a JNDI data source…” Questions like this need to be answered in the context of Java EE rather than Spring. Some Java EE background is therefore useful.

JNDI Data Source Documentation

JNDI in Java EE 8.

Configuring a JDBC Connection Pool in GlassFish

For Oracle or MySQL, see the GlassFish data-source documentation or this tutorial. This article uses H2, so there are some differences.

Find JDBC Connection Pools in the GlassFish console and click New. GlassFish JDBC Connection Pools page and New button

Enter a name and select javax.sql.DataSource as ResourceType. Selecting javax.sql.DataSource when creating a GlassFish connection pool

The screenshot shows no H2 option, so we need to integrate it. Download the H2 driver JAR and place it in:

/domains/domain1/lib under the GlassFish server home.

Restart GlassFish and follow the steps above to return to this page. Entering H2 as the database vendor for a new GlassFish connection pool Enter H2 under Vendor and click Next. At the bottom of the next page, configure url, user, and password. URL, user, and password properties for a GlassFish H2 connection pool

Note:

This in-memory database actually has no password, but some value still needs to be entered. A Stack Overflow answer suggested entering:

()

However, that did not work for me.

After configuration, click Ping to check whether it succeeds. Ping test button in GlassFish JDBC connection pool details

If there is an error, find its details in the logs described above.

You can also inspect it in the asadmin command line.

list-jdbc-connection-pools

Configuring a JDBC Resource in GlassFish

After configuring the connection pool, configure the JDBC resource. Resources and connection pool mappings on the GlassFish JDBC Resources page

On the creation page, enter a name (this is the JNDI name) and select the DataSource Pool created in the previous step.

After creation, it can also be found through the asadmin command line.

list-jdbc-resources

If the configuration above is correct, you can also find the generated settings in domain.xml, usually located at:

Glassfish Server home/domains/domain1/config

JDBC connection pool and resource configuration in GlassFish domain.xml

Running the Application

Installing the Example Code

Example code.

Clone the code with git clone, then import it into IntelliJ IDEA.

Configuring GlassFish in IntelliJ IDEA

GlassFish server path and startup settings in the IDEA run configuration

Viewing Server Logs

After configuration, the server logs are visible directly in IntelliJ IDEA. IDEA console showing GlassFish deployment and application startup logs

Application Screenshots

Username and password login page of the Java EE sample application Default username: dhb.

Password: 123456.

Click to log in.

Reader Questions

  1. Question: why use Java EE technologies? Aren’t Spring and Spring Boot good enough?

    Answer: technologies are not inherently good or bad; what matters is whether they solve the problem. Choose what suits your organization.

  2. Question: how can I learn more about Java EE?

    Answer: I recommend the Java EE 8 tutorial and running its example applications.

Resources

  1. A diagram of the relationships among JPA components. Relationship diagram of EntityManagerFactory, EntityManager, persistence context, and database in JPA
  2. An article on choosing JTA or RESOURCE_LOCAL for transaction-type.

Share this post:

Previous Post
Practical Elasticsearch: Common Operations, Logstash Integration, Local IP Handling, and ECS Field Mapping
Next Post
Getting Started with Lua Scripts, with Example Code

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.