Professional Documents
Culture Documents
Josh Juneau.
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.
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.
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):
http://download.java.net/glassfish/5.0/release/javaee8-ri-web.zip
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.
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 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:
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.
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:
https://github.com/juneau001/SportsTeamQueryService.git
6. Prepare database.
Open Services tab, expand Servers > GlassFish Server > Resources > JDBC.
(NOTE: You must have GlassFish server running, because JNDI name is runtime
attribute.)
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…”
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:
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) .
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:
1. Query Service
2. Registration Service
3. UI Service (important: depends on first two)
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...”
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...
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.
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:
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!
http://localhost:8080/SportsTeamRegistrationService/rest/teamrosterregistrationservice/a
ddPlayer
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">
</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:
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 !