You are on page 1of 31

Steps to run Jakarta EE projects from the September/October 2018 Java Magazine article by

Josh Juneau.

Author: Mikalai Zaikin (mzaikin@gmail.com)

1. Prerequisite: you should have Java SE 8 installed and available in PATH.

SIDENOTE​​: GlassFish 5.0 requires ​Oracle JDK 8 Update 144 or later​​. You won’t be
able to run GlassFish 5.0 with Java SE 9 or higher. Details here:
https://javaee.github.io/glassfish/doc/5.0/release-notes.pdf

2. Install NetBeans 9.

Since version 9, NetBeans is hosted at apache.org web site. Goto


https://netbeans.apache.org/​, then download binary release of version 9 (latest as of the
moment of writing).

The direct link is:


https://www.apache.org/dyn/closer.cgi/incubator/netbeans/incubating-netbeans-java/incu
bating-9.0/incubating-netbeans-java-9.0-bin.zip

Then unzip the file, rename the folder to ​netbeans9​​ and copy to ​C:\​​ location. You should
have the following:
3. Install GlassFish 5.

I use GlassFish, as it is Reference Implementation (RI) for Java EE and has good
integration with NetBeans IDE.

Go to GlassFish web site: ​https://javaee.github.io/glassfish/downloads/ri/README

You need Java EE 8 RI, Web Profile will be enough (it has smaller set of supported
specification compared to full profile, and smaller runtime footprint):

The direct link is:

http://download.java.net/glassfish/5.0/release/javaee8-ri-web.zip

Unzip the file, move folder ​glassfish5​​ to C


​ :\​​ location.

You should have on the disk as follows:


4. Configure GlassFish Application Server in NetBeans.

Now, start the NetBeans by launching ​bin/netbeans64.exe

Agree to install required plugins.

By default NetBeans 9 does not support Java EE. You need to add support, including
integration with GlassFish server. Also, NetBeans 9 has not all Java EE plugins, so we
will use (unofficially allowed) plugins from NetBeans 8.2.

Open ​Tools > Plugins​​ menu, go to ​Settings​​ tab.


Click the ​Add​​ button and add new Update Center location as follows:

Name: NetBeans 8.2 Plugins


​ ttp://updates.netbeans.org/netbeans/updates/8.2/uc/final/distribution/catalog.xml.gz
URL: h

Now, open ​Available Plugins​​ tab, sort by ​Category​​ column, and select all ​Java Web
and EE​​ plugins:
Click the ​Install​​ button, agree with license and install the plugins. The process may take
some time, depending on network bandwidth.

Click ​Continue​​ button on certificate verification step and select to restart IDE. The first
start after update normally takes longer as IDE unpacks all downloaded modules.

Now, on the ​Services​​ tab, right click the ​Servers​​ node and select “​Add Server…​​”

Choose the GlassFish server:


Click the ​Next​​ button.

Choose installation location of the server (if you followed conventions, it should be
C:\glassfish5​​)
Click the ​Next ​button. Keep all by default and click ​Finish​​ button:
The new GlassFish server created and added to NetBeans:

Start the server by right click and check it started OK:


Once it started you can see that it has no applications, and has some pre-configured
resources (e.g. JDBC):

5. Clone demo projects.

Since the code for the article is provided in GitHub repository, you do not need to use
create new project wizards, rather clone Git project. NetBeans 9 has Git support out of
box and can recognize Maven (pom.xml based) projects.

For each of these three repositories do the same steps:


https://github.com/juneau001/SportsTeamQueryService
https://github.com/juneau001/SportsTeamRegistrationService
https://github.com/juneau001/SportsTeamUIService

Open repository URL in web browser and click the “​Clone or download​​” green button:
Then copy the .git URL into clipboard either via Ctrl+C, or via clicking on the button on
the right from the text field:

The URL in clipboard looks like that:

https://github.com/juneau001/SportsTeamQueryService.git

Switch to NetBeans 9, click menu ​Team > Git > Clone...​​:

Paste the .git URL and click ​Finish​​:


After project is cloned click the ​Open Project​​ to open the project in the IDE. Otherwise
you will need to open it manually from the ​NetBeansProjects ​directory.
In the ​Projects​​ tab you have 1 of 3 projects available:

Repeat the same steps for other 2 projects:

6. Prepare database.

You need to create new table to be used by demo applications.


Two demo projects (​SportsTeamQueryService​​ and ​SportsTeamRegistrationService​​)
access database to persist or retrieve team members. Java EE uses JPA Persistence
Units (PU) for this. Persistence unit is provided JNDI name of DataSource to connect to
Derby DB. The JNDI name of DataSource is part of Application Servers configuration. If
you open existing pre-configured data source JNDI names in server you will notice the
names.

Open ​Services ​tab, expand ​Servers > GlassFish Server > Resources > JDBC​​.
(NOTE: You must have GlassFish server running, because JNDI name is runtime
attribute.)

The “​jdbc/__default​​” JNDI name is the name of default DataSource pre-configured in


GlassFish out of box. We need to create new table in the database which linked with
datasource named “​jdbc/__default​”​ .

Right click JNDI name and check that datasource connection pool name is: ​DerbyPool​​.

Right click ​DerbyPool ​and check that database name is: ​sun-appserv-samples
Now, we need to create a database with ​sun-appserv-samples ​name.
Connect to database: right click on ​sun-appserv-samples​​ and select “​Connect…​​”

And the screen changes for DB connection :

The default schema name is ​APP​​. Right click on the DB connection and select “​Execute
Command...​​”
Copy-paste this DDL to the text editor on the right side:

CREATE TABLE TEAM_ROSTER (


ID NUMERIC,
FIRST_NAME VARCHAR(150),
LAST_NAME VARCHAR(150),
POSITION VARCHAR(150),
REGISTRATION_DATE DATE,
PRIMARY KEY (ID)
);

And click the ​Run SQL​​ button as shown below:

Expand the ​APP ​schema on the left and make sure new table was created:
7. Edit persistence units for two demo projects

The persistence units in the projects use “​jdbc/__derby​​” JNDI name, while the default
pre-configured JNDI name is “​jdbc/__default​​”. The simplest approach is to edit
persistence units (​META-INF/persistence.xml​)​ .

Open ​persistence.xml​​ and edit, the switch to ​Source​​ editing mode

The line:

<jta-data-source>jdbc/__derby</jta-data-source>

must read as

<jta-data-source>jdbc/__default</jta-data-source>
Repeat for the second project:

Save the changes!

8. Run the demo projects and test application

Run three projects in the specified order:

1. Query Service
2. Registration Service
3. UI Service (important: depends on first two)

Right click the project and select “​Run​​”

Select which server to deploy to in drop down:


If you succeeded, then stub will be opened:

Proceed to next project.

The third project will show you UI of the application:


Add a new player:

Click ​Register​​:
To validate that actually a new row was added in the correct database and correct table,
do the following:

Open ​Services ​tab, expand the ​TEAM_ROSTER​​ table, right click and select “​View
Data...​​”

As you can see - new player was added in DB:


9. Further steps.

Further steps not closely related to Java EE 8, but provide some helpful technique to
work with REST applications (including those which written with Java EE 8 ;-)).

The Java EE 8 sample in the article contains 3 web projects:two REST projects and one
JSF, where the JSF project depends (uses) two REST projects. Imagine we develop the
application and have REST project (microservice), but not yet JSF UI. How can we make
sure REST project work properly? The simplest option is to use standalone REST client
and connect directly to REST web application. This is what we will do next...

I prefer in my work ​Advanced REST Client​​, and standalone installer can be


downloaded here: ​https://install.advancedrestclient.com/#/install

Make sure ​SportsTeamQueryService ​is running, open the REST client and enter the
URL:

http://localhost:8080/SportsTeamQueryService/rest/teamrosterqueryservice/findAll

Where:
- ​localhost:8080​​ - default host and port for unsecured web applications in GlassFish 5.

- ​/SportsTeamQueryService​​ - context path (context root) of application, by default for


GlassFish it is the same as the project name, but can be customized in
META-INF/application.xml​​ deployment descriptor for Enterprise Application.
- ​/rest​​ - is defined in ​ApplicationConfig.java​​ as the root for the all REST resources.
Any URL under this path will be treated as REST resource.
@javax.ws.rs.ApplicationPath("​rest​​")
public class ApplicationConfig extends Application {
...

- ​/teamrosterqueryservice​​ - root path to ​TeamRosterFacadeREST.java​​ REST


resource:
@javax.ejb.Stateless
@Path("​teamrosterqueryservice​​")
public class TeamRosterFacadeREST {

- ​/findAll​​ - URI path to the method which returns all players:


@GET
@Path("​findAll​​")
@Produces({MediaType.APPLICATION_XML,
MediaType.APPLICATION_JSON})
public TeamRosterObjects findAll() { … }

Make sure you selected ​GET​​ HTTP method (it was defined by ​@GET​​ annotation in the
code). You will see on the screen something like that:
The application by default returns data in XML format, however, Java REST API
supports multiple formats output based on client’s preferences, and this already added in
the code:

@GET
@Path("findAll")
@Produces({MediaType.APPLICATION_XML,
MediaType.APPLICATION_JSON​​})
public TeamRosterObjects findAll() { … }

In order to get response as JSON (which is more natural for REST application), let’s add
one HTTP header in request:

Header name​​: ​Accept


Header value​​: ​application/json

New output will be a JSON structure (note - we did not touch web application, it made
decision based on client’s HTTP headers):
So far so good!

Let’s play with the second ​SportsTeamRegistrationService​​ REST application at low


level.
Based on explanations above, the URL to add new player will be:

http://localhost:8080/SportsTeamRegistrationService/rest/teamrosterregistrationservice/a
ddPlayer

The HTTP method will be ​POST​​:

The ​@FormParam​​ annotations bind HTML form parameters to Java variables. Sample
HTML fragment:

<form
action="​http://localhost:8080/SportsTeamRegistrationService/res
t/teamrosterregistrationservice/addPlayer​" method="post">

First name: <input type="text" name="​firstName​​">


Last name: <input type="text" name="​lastName​​">
Position: <input type="text" name="​position​​">

<input type="submit" value="Send">

</form>

We can emulate HTML form in REST client. First, we need new HTTP header which tell
that form data is URL-encoded:

Content-Type: application/x-www-form-urlencoded

Values are passed in HTTP request body (which follows HTTP headers block and
separated by extra blank line). At low level it looks like that when passed over wire:

firstName=Ryan&lastName=Arcidiacono&position=Guard
Configure the REST client:

add form parameters:

And click the ​SEND ​button.

The response with status ​HTTP 200 OK​​ says that request was successfully processed.
Check the second player was added:
This ends our exploring of Java EE 8 applications !

You might also like