Java Servlet Example with Jakarta EE 10

This tutorial demonstrates how to create a simple Java servlet using Jakarta EE 10. We will build a basic web application that includes a servlet to handle HTTP requests and responses.

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 Jetty

Step 1: Setup Maven Project

First, 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 Servlet API and JSTL.

<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-servlet-example</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>

        <!-- 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-servlet-example</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 Servlet

Next, create a servlet class that will handle HTTP requests.

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: Configure Deployment Descriptor (Optional)

Although Jakarta EE 10 allows configuration using annotations, you can still use web.xml for additional configuration if needed. This step is optional.

web.xml

Create a web.xml file in the src/main/webapp/WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>HelloWorldServlet</servlet-name>
        <servlet-class>com.example.HelloWorldServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorldServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

Step 5: 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-servlet-example.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-servlet-example. 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 10. This tutorial covered 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.

Comments