How to build WAR file for Jakarta EE Servlet Container?

To build a WAR file for your Jakarta EE servlet application, the steps will vary depending on the build tool you are using. In this post I will show you how to use Apache Maven, build it in the IDE, or create it manually. Here’s how you can build a WAR file:


1. Using Maven

Maven is a widely used build tool in Java projects. If your project uses Maven, follow these steps:

a) Add WAR Packaging to pom.xml

Your pom.xml file should include these configurations:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.kodejava</groupId>
    <artifactId>helloworld-servlet</artifactId>
    <version>1.0</version>
    <packaging>war</packaging> <!-- Specify that the packaging is WAR -->
    <name>HelloWorldServlet</name>

    <dependencies>
        <!-- Jakarta Servlet dependency -->
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.1.0</version>
            <scope>provided</scope> <!-- mark as provided since the container will provide the API -->
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- WAR Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml> <!-- Optional for Servlet 3.0+ -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

b) Directory Structure for WAR Projects

Ensure your project follows the standard Maven directory structure:

src/
└── main/
    ├── java/                     --> Your Java source files
    │   └── org/kodejava/servlet/HelloWorld.java
    ├── resources/                --> (Optional) Resource files
    └── webapp/                   --> Web application files
        ├── WEB-INF/              --> Configuration files
        │   └── web.xml           --> web.xml file (if required)
        └── index.html            --> Static files (Optional)

Important Notes:

  • webapp/WEB-INF/ is the location for configuration files. For modern servlets with annotations like @WebServlet, a web.xml file may not be required.
  • Additional static resources (like HTML, CSS, or JS files) can be placed under webapp/.

c) Build the WAR File

Run the following Maven command:

mvn clean package

Once the build is successful, the WAR file will be generated in the target/ directory, named as <artifactId>-<version>.war.

For instance: target/helloworld-servlet-1.0.war


2. Using an IDE (IntelliJ IDEA, Eclipse)

Some IDEs let you build a WAR file without using Maven explicitly. For IntelliJ IDEA:

a) Configure Your Project as a WAR

  1. Open your project in IntelliJ.
  2. Go to File > Project Structure > Artifacts.
  3. Click on the + button and choose Web Application: Archive > From Modules with Dependencies.
  4. Select your module and specify the location of your web and META-INF folders.
  5. Apply and save changes.

b) Build the WAR File

  1. Go to Build > Build Artifacts > Build.
  2. The WAR file will be created in the specified output folder.

3. Manually Create a WAR

If you’re not using a build tool and want to manually create a WAR file:

a) Prepare the Required Directory Structure

Manually create a structure similar to this:

project/
└── WEB-INF/
    ├── web.xml           --> Optional (for configurations)
    └── classes/          --> Compiled `.class` files (Java code goes here)
        └── org/kodejava/servlet/HelloWorld.class

Copy all your .class files, resources, and configuration files into their respective folders.

b) Create the WAR File

Navigate to the folder containing the root project directory and run:

jar -cvf HelloWorldServlet.war *

This command will package your project into a .war file.


By following any of these methods, you’ll have a .war file ready for deployment in your Tomcat 11 server.

How to Use the Java 10 JDK Command Line Tools

The Java 10 JDK offers several command-line tools for developers to use. Here’s an overview of some useful tools and related features:

1. JShell (Interactive Java REPL)

The jshell tool allows developers to experiment interactively by evaluating Java expressions, statements, and code snippets without the need to set up a complete program. It was introduced as part of JDK 9 but is also available in Java 10.

You can use JShell via:

  • Command line: Just type jshell in your terminal/command prompt.
  • IntelliJ IDEA: Open the JShell console through Tools > JShell Console in the IDE. This allows trying smaller snippets of code and experimenting interactively [1].

2. Java Compiler (javac)

The javac tool is the standard way to compile Java source code into bytecode. In Java 10, the --release flag can be used to ensure compatibility with earlier JDK releases.

Command:

javac --release <version> FileName.java

To compile your code:

  1. Open a terminal.
  2. Navigate to the directory containing the .java file.
  3. Run the javac command followed by the file’s name.

3. Java Runner (java)

The java tool is used to execute compiled Java applications or scripts. Java 10 also supports temporary files and improved APIs for startup optimizations.

Command example:

java FileName

4. Java Flight Recorder and Other Tools

Java Flight Recorder is useful for profiling and analyzing runtime performance. In JDK 10, you need to enable UnlockCommercialFeatures if using the Oracle JDK.

For example:

java -XX:+UnlockCommercialFeatures -XX:StartFlightRecording=duration=60s,filename=myrecording.jfr MyApplication

This is useful for monitoring or debugging [4].


5. JLink

The jlink tool lets you create runtime images that include all the modules your application requires (introduced in JDK 9). With Java 10, improvements were made for better custom image creation.

Command example:

jlink --module-path <modules-path> --add-modules <module-name> --output <destination-folder>

This tool is handy when distributing lightweight application bundles. IDEs like IntelliJ IDEA also provide options to integrate it into Maven or Gradle builds [5].


6. Managing Executable Scripts

Create and run Java commands or files directly as scripts without needing to compile them. This concept started gaining traction with JDK 11’s “shebang” support but can also apply lightly to Java 10 for executable bundling purposes [6].


7. General IntelliJ IDEA Features for Java 10

IntelliJ IDEA helps users by automatically configuring and detecting Java 10 features, including modular programming. Several integrations for JDK tools like javac, java, and jlink make development smoother [7].


How to Set Up Java 10 and Compile Your First Program

To set up Java 10 and compile your first program, follow these steps. Additionally, you’ll learn about Java 10’s var keyword feature once your setup is complete.


Steps to Set Up Java 10

  1. Download Java 10 JDK:
  2. Install Java 10:
    • Follow the installation wizard to install the JDK.
    • Don’t forget to note the installation path (e.g., C:\Program Files\Java\jdk-10).
  3. Set up the Environment Variables:
    • Add the JDK bin directory to the PATH variable:
      1. Go to System PropertiesAdvancedEnvironment Variables.
      2. Under System Variables, find the Path variable and add the JDK’s bin directory (e.g., C:\Program Files\Java\jdk-10\bin).
    • Verify the setup:
      • Open the command prompt or terminal and type:
        text
        java -version

        You should see the version as Java 10.
  4. Install an IDE or Use a Text Editor:
    • Download and install an IDE like IntelliJ IDEA, Eclipse, or Visual Studio Code (or use a simple text editor).

Compile and Run Your First Java Program

  1. Write the Java Program:
    Create a file named HelloWorld.java with the following content:

    public class HelloWorld {
       public static void main(String[] args) {
           System.out.println("Hello, World! Welcome to Java 10!");
       }
    }
    
  2. Compile Your Program:
    From the command prompt, navigate to the directory containing HelloWorld.java, and run:

    javac HelloWorld.java
    

    This will create a compiled HelloWorld.class file.

  3. Run Your Program:
    Execute the compiled program using:

    java HelloWorld
    

    You should see the output:

    Hello, World! Welcome to Java 10!
    

Using the var Keyword in Java 10

To explore Java 10’s var keyword for local variable type inference, you can enhance your program. Update the HelloWorld class as follows:

package org.kodejava.basic;

import java.util.List;

public class HelloWorld {
   public static void main(String[] args) {
      var message = "Hello, Java 10!";
      var numbers = List.of(1, 2, 3);

      System.out.println(message);
      for (var number : numbers) {
         System.out.println("Number: " + number); // Using 'var' in loop
      }
   }
}
  • Save the changes as HelloWorld.java.
  • Recompile and run using the steps above.

You’ll see the output:

Hello, Java 10!
Number: 1
Number: 2
Number: 3

The var keyword simplifies variable declarations without compromising type safety, ensuring better code readability and brevity.


Conclusion

You’ve successfully set up Java 10, compiled, and executed your first program. Additionally, you explored how to use Java 10’s var keyword for type inference. Keep experimenting with these features to leverage Java 10’s capabilities!

How do I create a basic servlet using the HttpServlet class?

To create a basic servlet using the HttpServlet class in Jakarta Servlet API, follow these steps:

Steps to Create a Basic Servlet:

  1. Create a Java Class Extending HttpServlet:
    • Your servlet class should extend the HttpServlet class provided in the Jakarta Servlet API.
  2. Define Servlet Annotations or Configuration:
    • Use the @WebServlet annotation to define the servlet and map it to a URL pattern. Alternatively, you can configure it in the web.xml file if annotations are not used.
  3. Override doGet or doPost Methods:
    • Override doGet for handling GET requests and doPost for POST requests (based on the type of request you expect).
  4. Set the Content Type in Response:
    • The response content type can be set using response.setContentType().
  5. Write the Response to Output:
    • Use a PrintWriter object received from response.getWriter() to write the response content.

Code Example:

Here’s a sample implementation for a basic servlet:

package org.kodejava.servlet;

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(name = "HelloWorldServlet", urlPatterns = {"/hello", "/helloworld"})
public class HelloWorld extends HttpServlet {

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
      response.setContentType("text/html");

      PrintWriter writer = response.getWriter();
      writer.println("<html>");
      writer.println("<head><title>Hello World Servlet</title></head>");
      writer.println("<body>Hello World! How are you doing?</body>");
      writer.println("</html>");
      writer.close();
   }
}

Explanation of Code:

  1. @WebServlet Annotation:
    • name: Specifies the servlet name.
    • urlPatterns: Maps the servlet to specific URL patterns (in this example, /hello).
  2. doGet Method:
    • Handles GET requests sent to /hello.
    • Sets the content type for the response to text/html so that the client knows the returned content is HTML.
    • Dynamically writes an HTML page to the response using the PrintWriter.
  3. Response Lifecycle:
    • Any request to http://<server>:<port>/hello is routed to the doGet method based on the @WebServlet mapping.

Deploying the Servlet in Jakarta EE:

  • Place your servlet class in the Java package structure under src/main/java.
  • Ensure your project is configured to use a Jakarta Servlet container like Apache Tomcat or Jakarta EE compatible application server.
  • Deploy the .war file or run directly if your IDE supports it.

To build the .war file you can follow in steps in this post: How to build WAR file for Jakarta EE Servlet Container?.

Maven dependencies

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>6.1.0</version>
    <scope>provided</scope>
</dependency>

Maven Central

How to Use the var Keyword for Local Variable Type Inference in Java 10

In Java 10, the var keyword was introduced to allow local variable type inference. This means that when you declare a local variable, the compiler automatically infers its type based on the initialization value. This improves readability and reduces boilerplate code, especially when working with complex types, without compromising type safety.

Here’s a guide on how to use the var keyword:


1. Declaring and Initializing Local Variables with var

The var keyword replaces explicitly specifying the type while declaring local variables. However, you must initialize the variable at the time of declaration, as the compiler needs an expression to infer the type.

// Example of using var keyword
var message = "Hello, Java 10!";  // Inferred as String
var number = 10;                 // Inferred as int
var list = List.of("apple", "banana", "orange"); // Inferred as List<String>

Here, the type of each variable is deduced by the Java compiler:

  • message: String
  • number: int
  • list: List<String>

2. Scopes Where var Can Be Used

The var keyword can only be used in specific contexts:

a. Local Variables in Methods

public void demoVarUsage() {
    var name = "John";      // Inferred as a String
    var age = 30;           // Inferred as an int

    System.out.println(name + " is " + age + " years old.");
}

b. Loop Variables (for-each or traditional for-loops)

// for each loop
var items = List.of("A", "B", "C");
for (var item : items) {
    System.out.println(item);  // item inferred as String
}

// traditional for loop
for (var i = 0; i < 10; i++) {
    System.out.println(i);     // i inferred as int
}

c. Local Variables in Lambda Expressions

var lambda = (String x, String y) -> x + y; // Explicit lambda types
System.out.println(lambda.apply("Java", "10"));

3. Restrictions on var Usage

While var is versatile, there are limitations:

  1. Cannot Be Used Without Initialization
    var name; // Compilation error: cannot infer type
    name = "John";
    
  2. Cannot Be Used with Null Initializer
    var something = null; // Compilation error
    
  3. Cannot Be Used as a Method Parameter, Field, or Return Type
    The var keyword is limited to local variables inside methods and blocks, as well as loop variables. It cannot be used:

    • As a return type of method.
    • As a field in a class.
    • As a method parameter.
    public var getName() {    // Compilation error: 'var' is not allowed here
       return "Java";
    }
    
  4. Cannot Mix Explicit Types and var
    var name = "John", age = 30; // Compilation error
    var name = "John";
    var age = 30;               // Declare separately
    
  5. Cannot Infer Ambiguous Types
    var result = process();  // If `process()` returns Object, type can't be narrowed.
    

4. Advantages of Using var

  • Improved Code Readability: Reduces verbosity for complex types.
    var map = new HashMap<String, List<Integer>>(); // Cleaner than HashMap<String, List<Integer>>
    
  • Consistent with Type Inference: Makes Java more modern and closer to languages like Kotlin, Scala, or C#.


5. Best Practices

  • Avoid overusing var to ensure code remains understandable.
  • Use meaningful names for variables to compensate for the lack of explicit type.
  • Use var only when the type is obvious from the context.

Example Code:

package org.kodejava.basic;

import java.util.List;

public class VarExample {
   public static void main(String[] args) {
      // Using var for various local variable declarations
      var name = "Alice";                        // String
      var age = 25;                              // int
      var fruits = List.of("Apple", "Banana");  // List<String>

      System.out.println(name + " likes " + fruits);

      for (var fruit : fruits) {
         System.out.println(fruit);  // Inferred as String
      }

      var sum = add(10, 20);  // Inferred as int
      System.out.println("Sum: " + sum);
   }

   private static int add(int a, int b) {
      return a + b;
   }
}

Output:

Alice likes [Apple, Banana]
Apple
Banana
Sum: 30

The var keyword is a helpful addition, especially for simplifying local variables with inferred types, keeping code concise and readable while retaining type safety!