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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.