Jakarta EE Tutorial

Introduction to Jakarta EE

Jakarta EE is a set of specifications that extend Java SE with specifications for enterprise features such as distributed computing and web services. Jakarta EE, formerly known as Java EE (Enterprise Edition), provides a robust, scalable, and secure platform for developing and deploying web and enterprise applications. Jakarta EE is maintained by the Eclipse Foundation, which took over the project from Oracle in 2017.

Key Features of Jakarta EE

  1. Modular Architecture: Jakarta EE provides a modular architecture, which allows developers to use only the parts of the platform they need.

  2. Scalability: The platform is designed to scale from small web applications to large enterprise applications.

  3. Security: Jakarta EE provides a comprehensive set of security features, including authentication, authorization, and encryption.

  4. Web Services: Jakarta EE supports both RESTful and SOAP-based web services, making it easy to create and consume web services.

  5. Persistence: Jakarta EE includes Java Persistence API (JPA) for object-relational mapping, making database interactions seamless.

  6. Dependency Injection: Contexts and Dependency Injection (CDI) is a core feature of Jakarta EE, allowing for loose coupling and easier testing of components.

  7. Transaction Management: Jakarta EE provides robust transaction management, ensuring data integrity and consistency across distributed systems.

  8. Messaging: Jakarta EE includes Java Message Service (JMS) for reliable messaging between components.

  9. Asynchronous Processing: Jakarta EE allows for asynchronous processing, improving application performance and responsiveness.

  10. Compatibility and Portability: Jakarta EE applications are compatible with any Jakarta EE-compliant application server, ensuring portability across different environments.

Jakarta EE Release and History

Jakarta EE 10 is the current release, which includes updates and improvements over previous versions.

The transition from Java EE to Jakarta EE

Jakarta EE originated from Java EE, which was developed by Sun Microsystems and later maintained by Oracle. In 2017, Oracle donated Java EE to the Eclipse Foundation, and it was rebranded as Jakarta EE. This transition involved renaming packages from javax.* to jakarta.*, which required some changes in application code but paved the way for faster development and innovation in the enterprise Java space.

Step-by-Step Tutorial: Building a Java Servlet Example with Jakarta EE 10

Prerequisites

  • Java Development Kit (JDK) 11 or higher
  • Apache Maven
  • An IDE such as IntelliJ IDEA or Eclipse
  • A servlet container such as Apache Tomcat or an application server like Payara or WildFly

Step 1: Setting Up the Maven Project

Create a new Maven project. You can do this from your IDE or by using the command line.

pom.xml

Update your pom.xml to include the necessary dependencies for Jakarta EE.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>jakarta-ee-tutorial</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!-- Jakarta Servlet API -->
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Jakarta JSP API -->
        <dependency>
            <groupId>jakarta.servlet.jsp</groupId>
            <artifactId>jakarta.servlet.jsp-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSTL API -->
        <dependency>
            <groupId>jakarta.servlet.jsp.jstl</groupId>
            <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
            <version>3.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>jakarta-ee-tutorial</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step 2: Create the Servlet

Create a servlet class that will handle HTTP requests and responses.

HelloWorldServlet.java

Create a new Java class HelloWorldServlet in the src/main/java/com/example directory.

package com.example;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html><body>");
        out.println("<h1>Hello, World!</h1>");
        out.println("</body></html>");
    }
}

In this example, the servlet is mapped to the URL pattern /hello using the @WebServlet annotation.

Step 3: Create JSP Page

Create a JSP page to interact with the servlet.

index.jsp

Create an index.jsp file in the src/main/webapp directory.

<!DOCTYPE html>
<html>
<head>
    <title>Jakarta EE Servlet Example</title>
</head>
<body>
    <h1>Welcome to Jakarta EE Servlet Example</h1>
    <form action="hello" method="get">
        <input type="submit" value="Say Hello">
    </form>
</body>
</html>

This JSP page contains a form that sends a GET request to the HelloWorldServlet.

Step 4: Build and Deploy the Application

  1. Build the Project: Use Maven to build the project. Run the following command in the project directory:

    mvn clean install
    
  2. Deploy to Servlet Container: Copy the generated WAR file (target/jakarta-ee-tutorial.war) to the webapps directory of your servlet container (e.g., Apache Tomcat).

  3. Start the Servlet Container: Start your servlet container. For Apache Tomcat, you can start it using the following command:

    catalina.sh start
    
  4. Access the Application: Open your web browser and navigate to http://localhost:8080/jakarta-ee-tutorial. You should see the JSP page with a form. Click the "Say Hello" button to invoke the servlet.

Conclusion

You have successfully created a simple Java servlet using Jakarta EE. This tutorial covered an introduction to Jakarta EE and the basics of setting up a Maven project, creating a servlet, and deploying it to a servlet container. You can extend this example by adding more servlets, JSP pages, and other Jakarta EE features to build a full-fledged web application.

Additional Resources

Comments