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 do I obtain ServletContext of another application?

The ServletContext.getContext(String uripath) enable us to access servlet context of another web application deployed on the same application server. A configuration need to be added to enable this feature.

In the example below we will forward the request from the current application to the /otherapp/hello.jsp page. We place a string in the request object attribute of the current application and going to show it in the hello.jsp page.

package org.kodejava.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {"/context"})
public class GetAnotherContextServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Get ServletContext of another application on the same Servlet
        // container. This allows us to forward request to another application
        // on the same application server.
        ServletContext ctx = request.getServletContext().getContext("/otherapp");

        // Set a request attribute and forward to hello.jsp page on another 
        // context.
        request.setAttribute("MESSAGE", "Hello There!");
        RequestDispatcher dispatcher = ctx.getRequestDispatcher("/hello.jsp");
        dispatcher.forward(request, response);
    }
}

To enable this feature in Tomcat we need to enable the crossContext attribute by setting the value to true, the default value is false. Update the server.xml file to add the following configuration inside the <Host> node.

<Context path="/webapp" debug="0" reloadable="true" crossContext="true"/>

Maven dependencies

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

Maven Central