Getting Started With Jakarta EE 9: Hello World
See what Jakarta brings to the table.
Join the DZone community and get the full member experience.Join For Free
The release of Jakarta EE 9, at the end of 2020, was in many ways a historic event. The Java Enterprise framework is already 20 years old, having its first release in 1999. It has changed names a few times but the main concepts of the first release can still be found in this new release. During all those years, it has adapted itself to keep it up to date but has always adhered to its main principle of stability and backward compatibility.
Regarding backward compatibility, this release was also historic as the namespaces changed (like package names that changed from 'javax' to 'jakarta'). The change is straightforward, no other changes are introduced between Jakarta EE 8 and EE 9. This to make the migration as easy as possible.
Since older technologies are removed like support for CORBA and other specifications will not evolve anymore like SOAP, another major step is taken into the modernisation of this platform. You can see Jakarta EE 9 as the rebirth of Java Enterprise having their experience of 20 years of successful support for Enterprise applications.
Since Payara is dedicated to Jakarta, we've started 'Getting Started with Jakarta EE 9' a blog and video series to introduce those who are not familiar with the platform to the basics of Jakarta EE 9. And as you all probably know, the classic 'Hello World' example must start this series!
Setting Up with Maven
You can use the Maven build tools to compile and package your Jakarta EE 9 application. With the Maven Archetypes, you can bootstrap many types of applications. For the moment, there is no specific archetype for Jakarta EE 9 available. You can start from any other maven project and adopt the pom.xml file or you can use the quickstart archetype as follows
These are the important parts of the maven configuration for your Jakarta EE 9 project:
- Define the
jakartaee-web-apidependency so that your project can use all the frameworks that are part of the Web Profile. For most projects, the Web Profile is sufficient, otherwise, you can use the
jakartaee-apidependency that corresponds with the Full Profile.
- Use the scope provided as all the required code for Jakarta EE 9 is already available within The Payara Platform products. They don't need to be included in your WAR file which makes the file small and efficient.
- Define the JDK version you want to use for your application. In this case, we use JDK 11 as it is the latest LTS version of Java and the recommended version for any new project you start today. Officially, Jakarta EE 9 is only supported on JDK 8 but it works without any issues on JDK 11 with the Payara Platform products. The configuration parameters to specify the Java version are
- Define the name of the WAR file that needs to be created by defining it in the
- Do not forget to define the packaging type as war so that the Maven tool will generate a Web Archive. You also need to specify the configuration
<failOnMissingWebXml>false</failOnMissingWebXml>as we do not need a web.xml file in this simple example. The file is optional for Jakarta but without the configuration property, the Maven WAR plugin will complain.
Your pom.xml could like look this for the Jakarta EE 9 Hello World example.
You are now ready to open the Maven Project in your favorite IDE and start coding your application.
Setting Up with Gradle
You can also use Gradle as an alternative for Maven as your build tool. It doesn't have many templates available, but you can use the following command to generate a basic Gradle project.
It generates the basic files, like a build.gradle file containing the main configuration for your Gradle project, and the required wrapper files for Gradle.
These are the most important parts of the configuration that you should define within build.gradle file:
- Define the plugins java and war so that Gradle can create the Web Archive containing our application and compile the Java source files.
- Define Maven Central as the place where the dependencies of the project should be searched.
- Define the
jakartaee-web-apidependency so that your project can use all the frameworks that are part of the Web profile. For most projects, the Web Profile is sufficient, otherwise, you can use the
jakartaee-apidependency that corresponds with the Full Profile.
- Use providedCompile for the Jakarta EE 9 dependency since the Payara Platform Products already contain all code. It doesn't need to be included in the WAR file which makes it small and efficient.
- Define the JDK version you want to use for your application. In this case, we use JDK 11 as it is the latest LTS version of Java and the recommended version for any new project you start today. Officially, Jakarta EE 9 is only supported on JDK 8 but it works without any issues on JDK 11 with the Payara Platform products. The parameters to set are
Your build.gradle could like look this for the Jakarta EE 9 Hello World example.
You are now ready to open the Gradle Project in your favorite IDE and start coding your application.
Now that we have a project created, we can develop the Hello World Servlet. Servlets are not much used any more in today's development but are still very important as they form the basis for many frameworks that are built on top of it like REST endpoints and server-side applications like the Jakarta Faces (which was called Java Server Faces in Java EE)
In one of the next Jakarta EE 9 blogs, we will go deeper into the Servlet usage for implementing a File download, but today we will use the typical Hello World demo.
Some background information on the example
- The class extends the
HTTPServletabstract class so that basic support for the HTTP requests is available. It allows you to implement methods like
doPost()to respond to the specific HTTP Methods that are sent to the application.
- The link with the URL on which the Servlet responds is defined by the annotation
@WebServlet. It defines the URL that the end-user can use to reach your Servlet functionality. The general pattern is http://localhost:8080/<root>/<@WebServlet-value>.
HttpServletRequestparameter can be used to interact with the information sent by the user.
HttpServletRequest.getQueryString()returns the entire query string appended to the URL.
HttpServletRequest.getPatameter("paramName")returns the vale of the Query parameter paramName.
HttpServletRequest.getRequestURI()returns the URL that the user entered. In our example, we have bound a single URL (/world) to the servlet but it is possible to use wildcards so that multiple URLs are handled by the same class (like @WebServlet("/test/*")
HttpServletRequest.getSession()returns the HTTP Session for the request and allows to set values in the session (with the method .
setAttribute()) so that these values are available the next time the user accesses your application again (depending on session timeout configuration)
HttpServletResponseis a class to send information back to the user. In our cases, just the "Hello World" text.
Getting started with Jakarta EE 9 is easy. You just have to define one single dependency for your application that brings in the entire API of the Java Enterprise framework. The API needs to be defined as provided as the Payara Platform products contain all the required code already, which means that the Web Archive file is small and efficient.
We have shown how a Hello World response can be sent back from the server and in the next parts of this beginner Jakarta EE 9 series, we will see how other frameworks like Jakarta Rest and Jakarta Faces are building on top of this Servlet specification.
Visit our Getting Started with Jakarta EE Beginners series on YouTube for video tutorials:
Published at DZone with permission of Rudy De Busscher, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.