Running Your Java EE 8 Application in the Cloud
How to use Oracle Cloud, Docker, and GlassFish 5 to run an application using Java EE 8 features.
by Elder Moraes

Are you a Java server-side developer? Do you work with Java EE, microservices, Spring, or cloud computing? If so, you have many reasons to consider the powerful new features of Java EE 8. HTML5, HTTP/2, JSON-P, Contexts and Dependency Injection (CDI), cloud support, and other cool innovations are reason enough for a big celebration.

I will show you how to use Oracle Cloud, Docker, and GlassFish 5 to run an application using Java EE 8 features. Want to try the new model-view-controller (MVC 1.0) and JAX-RS, or are you curious about the performance of Servlet 4? You can explore all of this today—with confidence.

A Quick Look at Java EE

The Java EE platform has been, for a long time, a standard for enterprise applications. It is a community-driven specification.

Millions of developers around the world use its APIs in their projects. Thousands of companies rely on solutions based on this specification. Dozens of vendors providers applications servers built on its standards.

Java EE 8 is a set of Java Specification Requests (JSRs), including the following:

  • JSR 365: CDI 2.0
  • JSR 366: Java EE 8
  • JSR 367: Java API for JSON Binding (JSON-B)
  • JSR 369: Servlet 4.0
  • JSR 370: JAX-RS 2.1
  • JSR 371: Model-View-Controller (MVC) 1.0
  • JSR 372: JavaServer Faces (JSF) 2.3
  • JSR 374: Java API for JSON Processing (JSON-P) 1.1
  • JSR 380: Bean Validations 2.0

For a complete overview, see Figure 1, which was presented by Oracle's David Delabassee:

Figure 1. Java EE 8 components

Now that you understand the Java EE 8 context, we are ready to build our first Java EE 8 application.

Building a Java EE 8 Application
  • GlassFish 5, the reference implementation for Java EE 8. It gives you the latest code of Java EE 8, including all runtime dependencies. Download the last GlassFish 5 release.
  • For our example, we will use the implementation of three JSRs:

    - JSR 371: MVC 1.0
    - JSR 365: CDI 2.0
    - JSR 370: JAX-RS 2.1

I will show here only the important parts of the code. You should follow along; the full source code is here.

So, clone the repository and have fun!

First, we need our dependencies. We will use two:

Java EE API:


MVC 1.0:


This MVC dependency is a reference implementation and is not part of the Java EE umbrella. Maybe it will be in a future EE4J release.

Our MVC example uses the controller shown in Listing 1:

public class DateTimeController {

   Models models;

   DateTime dateTime;

   public String current(){
      this.models.put("current", new Current(dateTime.getCurrent()));
      return "/current.jsp";

Listing 1. Controller code

Listing 1 shows three of our target technologies:

  • MVC 1.0: The class uses the @Controller annotation. We also have the Models class provided by the MVC API.
  • CDI 2.0: The class uses the @Inject annotation to bring the dependencies that are needed.
  • JAX-RS 2.1: The class uses the @Path and @GET annotations.

Another rock star in our demo is Servlet 4. The class in Listing 2 shows a simple use of it:

@WebServlet(value = {"/serverpush"})
public class ServerPushServlet extends HttpServlet {

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       PushBuilder pb = request.newPushBuilder();

       if (pb != null) {
           .addHeader("content-type", "image/png")

       try (PrintWriter writer = response.getWriter();) {
           StringBuilder html = new StringBuilder();
           html.append("<img src='images/javaee-logo.png'><br>");
           html.append("<h3>Image pushed by ServerPush</h3>");

Listing 2. Example use of Servlet 4

Listing 2 uses HTTP/2 to push the image from the server to a client. In other words, the client doesn't need to send another request from "img src" to have the image for rendering. The image will be available at almost the same time as the page. This gives much better performance for the client, with little work on the server side.

Once you build your application, you can deploy it on GlassFish 5.

You can see the result of the working demo through the images below:

Figure 2. Welcome page
Figure 3. Page that is displayed if the "Show me JAX-RS in action" button is clicked
Figure 4. Page that is displayed if the "What? Http2 here?" button is clicked
Bringing Docker to the Party

There are plenty of reasons for running your application in containers. Containers bring features such as isolation, density, immutability, portability, scalability, and high availability. You can attach them to an orchestration solution and bring it to an even higher level.

Another reason is that we are living in the containers revolution. Containers are changing the way we deliver and use software. Developers everywhere are changing the way they work on their projects. All the big vendors are investing in containers.

And we are at the beginning of the revolution. In a few years, the entire technology industry will be completely changed by containers. So don't miss the chance, or you will stay behind.

Take a look at two other articles that I've written with Bruno Souza: "Step-by-Step High Availability with Docker and Java EE" and "Modular and Reusable Java EE Architecture with Docker," which was written with our friend Andre Carvalho. Those articles explain the relationships among Docker, Java EE, application servers, and services.

For this article, we only need to deploy our application on GlassFish 5 running with Docker. That's easy!

First, let's build a Docker image based on GlassFish 5. I've used a cool example from Adam Bien and made a few changes for our purposes. So our Dockerfile is like Listing 3:

FROM airhacks/java




ENTRYPOINT asadmin start-domain --verbose ${DOMAIN_NAME}

EXPOSE 4848 8009 8080 8181

Listing 3. Contents of our Dockerfile

To build the Docker image, you only have to run the following command (in the same directory as the Dockerfile):

docker build -t glassfish5 .

(The dot at the end is very important!)

This process will download the latest GlassFish 5 release and build a Docker image with it. Now, based on this image, we create another with our own application.

So, the other Dockerfile is like Listing 4:

FROM glassfish5



COPY javaee8-app-web.war ${DEPLOYMENT_DIR}

Listing 4. Our second Dockerfile

Then we build the image based on this Dockerfile:

docker build -t glassfish5-app .

And, finally, we run a new container based on this image (built in with our application):

docker run -d --name glassfish5-app -p 8080:8080 glassfish5-app

Wait a few seconds and open it in your browser using this URL: http://localhost:8080/javaee8-app-web/.

Great! You have built your first Java EE 8 application, using three brand-new APIs and running it on GlassFish 5. You rock!

But you know that your application is quite useless if it runs only on your laptop. You should take it higher. Let's move it to the cloud.

Moving the Application to the Cloud

Moving your application to the cloud will make it easier to experiment with technologies. It will also help you move forward faster.

First, we need our container to be available on the cloud. We will move it to Docker Hub.

We need to prepare our container to go to the cloud, so we will run this command:

docker commit -a "Elder Moraes" -m "Moving to the cloud" glassfish5-app eldermoraes/glassfish5-app:1.0

If you run docker images now, you should see something like this:

eldermoraes/glassfish5-app 1.0 45d0649eaffb 9 seconds ago 624.2 MB

You tagged your image and pointed it to your Docker Hub repository.

I encourage you to change the commands above to use your own repositories. Play around, do your customizations, and have fun, for example:

docker commit -a "Your Name" -m "Commit message" glassfish5-app your_Docker_Hub_repository/glassfish5-app:1.0

To push your image to Docker Hub, run the following command:

docker login

After the authentication is complete, run this command:

docker push eldermoraes/glassfish5-app

If you go to your Docker Hub account, you will see your brand-new image shining. Congratulations!

We are almost ready for the cloud. I've based the example in Oracle Cloud. So in case you've never tried it before, I will guide you.

Getting Started with Oracle Cloud

Here is the step-by-step procedure to have your instance up and running in Oracle Cloud.

1. Visit the Oracle Cloud page to begin your journey:

2. Scroll down the page and click Get Started.

3. In the page that appears, click Get started for free.

You will see the Oracle single sign-on (SSO) page, which looks like Figure 5:

Figure 5. Oracle SSO page

If you don't have an Oracle account, you can create one here.

4. After logging in, fill in the registration form for Oracle Cloud.

Figure 6. Registration form

Then you get the confirmation page:

Figure 7. Confirmation page

You should also receive a confirmation email.

Now you are good to go!

5. Log in to Oracle Cloud; you will see this dashboard page:

Figure 8. First page in Oracle Cloud

6. Click the Oracle Compute Cloud Service button. You will then see this page:

Figure 9. Service Details for Oracle Compute Cloud Service page

7. To create your instance, click the Oracle Compute Cloud Service link. You will then see this page:

Figure 10. My Services page

8. Click the Create Instance button and then, in the page that appears, click Marketplace:

Figure 11. Page for selecting an image

9. For this article, we will use the Ubuntu 14.04 LTS, so type ubuntu in the search field to search for the right image:

Figure 12. Search results

10. Select the image. Once the image is available to your account, you can continue the instance creation.

11. First, choose the amount of CPU and memory you want to use:

Figure 13. Choosing the amount of CPU and memory

12. Then add your SSH public key to the instance so you can access it later:

Figure 14. Adding the SSH key

13. Don't forget to reserve a public IP address so your instance can have public access:

Figure 15. Reserving an IP address

14. Before you finish, review all options you selected and then click Create:

Figure 16. Reviewing the selections

On the right side of the next page you can see the history of all actions that were done:

Figure 17. History of operations

15. Now, configure some security rules for accessing your instance through SSH and HTTP:

Figure 18. Configuring security rules

16. Now, go to the command prompt and run the following command:

ssh -i ssh_private_key_file ubuntu@instance_public_ip

If you can connect to the instance, voilá! You are ready to use your instance.

17. Before start using Docker on your instance, don't forget to install it! Follow the directions on this page, which provides the directions used for the Docker Community Edition for Ubuntu. If you are running another operating system, find the proper instructions.

18. Once Docker is up and running, run your container with this command:

docker run -d --name glassfish5-app -p 8080:8080 eldermoraes/glassfish5-app:1.0

If everything went fine, you will see your application running here: http://instance_public_ip:8080/javaee8-app-web/

You've made it! Now you have your own Java EE 8 application running in the cloud before everyone else! Now you have bragging rights (a few).


See how easy is to have your Java EE 8 application running right now? Everything you need is already available. Code examples, application servers, container images, and cloud services are out there. What you are waiting for?

Get your GlassFish 5 Docker image. Write some Java EE 8 code. Set up your instance in Oracle Cloud. Begin your journey to the next frontier!

About the Authors
Elder Moraes helps server-side developers to create and deliver secure, available, and fast applications so they can work on great projects. Passionate about cloud, systems architecture and Java EE, he shares his experience through online content and international events such as JavaOne, The Developer's Conference, Oracle Code, and Campus Party. He is also a Cloud Evangelist at Oracle, blogger, book author, and one of the leaders of the SouJava user group.
Join the Java Community Conversation
Experience Oracle Cloud —Get up to 3,500 hours free.