How do I deploy a WAR file in Tomcat Servlet Container?

In this post I will show you the step-by-step instructions to deploy a WAR file in Tomcat 11:

1. Build Your WAR File

A WAR file (.war) is a packaged web application. To build it:

  • If you’re using Maven, run the following command in your project directory:
    mvn clean package
    

    This generates a WAR file under the target/ folder.

  • If you’re using other tools (e.g., Gradle, IDE), export the WAR file using the packaging/export option.

2. Install and Configure Tomcat 11

  • Download Tomcat 11 from the official website: Apache Tomcat Downloads
  • Extract the downloaded ZIP/TAR file to a desired directory.
  • Navigate to the conf/ directory and optionally configure the file server.xml to customize ports, define your hostname, or adjust server parameters (optional for default usage).

3. Start Tomcat

  • Navigate to the bin/ directory.
  • Run the startup script as per your operating system:

    • On Windows:
      startup.bat
      
    • On Linux/Mac:
      ./startup.sh
      
  • By default, Tomcat runs on port 8080. Check if it is running by accessing:
    http://localhost:8080/
    

    If Tomcat is running, its welcome page should appear.

4. Deploy Your WAR File

You have two primary ways to deploy the WAR file:

a) Manual Deployment

  1. Copy the WAR file to Tomcat’s webapps/ directory:
    • Navigate to the webapps/ directory inside your Tomcat installation path.
    • Copy your WAR file (e.g., HelloWorld.war) into this folder.
      cp /path/to/HelloWorld.war /path/to/tomcat/webapps/
      
  2. Restart Tomcat:
    • Stop and restart Tomcat for new deployments to take effect, using:
      shutdown.bat/startup.bat (Windows)
      ./shutdown.sh && ./startup.sh (Linux/Mac)
      
  3. Access Your Application:
    • By default, the application will be available at:
      http://localhost:8080/<war-file-name>/
      
    • For example:
      http://localhost:8080/HelloWorld/
      

b) Upload via Tomcat Manager (Web UI)

  1. Go to the Tomcat Manager application at:
  2. Log in with your username and password:
    • Default username: admin
    • Default password: admin (or use the one you set in tomcat-users.xml file under conf/).
      To set new credentials, update the conf/tomcat-users.xml file by adding:

      <role rolename="manager-gui"/>
      <user username="admin" password="admin" roles="manager-gui"/>
      
  3. Scroll to the Deploy section:
    • Under “WAR file to deploy,” choose the WAR file from your system.
    • Click the Deploy button.
  4. Access Your Application:
    • The application URL will be:
      http://localhost:8080/<war-file-name>/
      

5. Check Logs for Errors (if any)

If the application is not working, check Tomcat logs:

  • Log files are located in the logs/ directory of your Tomcat installation.
  • The most relevant logs include:
    • catalina.<date>.log – Main log file for Tomcat.
    • localhost.<date>.log – Logs specific to deployed applications.

6. Stop Tomcat (if needed)

If you wish to shut down Tomcat:

  • Navigate to the bin/ directory.
  • Use the shutdown script:
    • On Windows:
      shutdown.bat
      
    • On Linux/Mac:
      ./shutdown.sh
      

Summary of Key Directories in Tomcat

Directory Purpose
bin/ Contains scripts to start and stop Tomcat.
conf/ Stores configuration files like server.xml.
webapps/ Holds deployed WAR files.
logs/ Contains log files for troubleshooting.

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