Deploying a Java Web Application to Tomcat Using Visual Studio Code

Deploying a Java web application to an Apache Tomcat server can seem daunting, especially if you're used to working with an IDE like Eclipse or IntelliJ IDEA. However, with Visual Studio Code (VS Code) and a few extensions, the process becomes quite manageable. This guide will walk you through the steps to deploy your Java web application to Tomcat using VS Code.

Prerequisites

Before we begin, ensure you have the following installed on your machine:

  1. Java Development Kit (JDK) - Ensure you have JDK 8 or later installed.

  2. Apache Tomcat - Download and install the latest version of Tomcat.

  3. Visual Studio Code - Download and install VS Code.

  4. Apache Maven - Install Maven for project management and build automation.

Step 1: Set Up Your Development Environment

Install Necessary Extensions in VS Code

To work with Java and deploy to Tomcat, you need to install several extensions in VS Code. Open VS Code and go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X.

Search for and install the following extensions:

  • Java Extension Pack (includes Language Support for Java(TM) by Red Hat, Debugger for Java, Java Test Runner, Maven for Java, and Visual Studio IntelliCode)

  • Tomcat for Java (by Adashen)

Configure Java and Maven in VS Code

  1. Open the Command Palette (Ctrl+Shift+P).

  2. Type Java: Configure Java Runtime and select it.

  3. Ensure the path to your JDK is correct. If not, update it accordingly.

Step 2: Create a New Java Web Application

  1. Open a new terminal in VS Code (`Ctrl+``).

  2. Navigate to the directory where you want to create your project.

  3. Run the following Maven command to generate a new web application:

     mvn archetype:generate -DgroupId=com.example -DartifactId=my-web-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
    

    This command will create a new Maven web application project in a directory named my-web-app.

  4. Navigate into your project directory:

     cd my-web-app
    

Step 3: Develop Your Application

Project Structure

Your Maven web application project should have the following structure:

my-web-app
├── src
   └── main
       ├── java
       ├── resources
       └── webapp
           └── WEB-INF
               ├── web.xml
├── pom.xml

Add a Servlet

  1. In the src/main/java directory, create a package and a new Java class for your servlet. For example, create com.example.servlet and a HelloServlet.java file:

     package com.example.servlet;
    
     import java.io.IOException;
     import javax.servlet.ServletException;
     import javax.servlet.annotation.WebServlet;
     import javax.servlet.http.HttpServlet;
     import javax.servlet.http.HttpServletRequest;
     import javax.servlet.http.HttpServletResponse;
    
     @WebServlet("/hello")
     public class HelloServlet extends HttpServlet {
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
             resp.setContentType("text/html");
             resp.getWriter().println("<h1>Hello, World!</h1>");
         }
     }
    
  2. Ensure your web.xml file (located in src/main/webapp/WEB-INF) is correctly configured for your servlet. If you used the @WebServlet annotation, you might not need to add anything here.

Update pom.xml

Ensure your pom.xml has the necessary dependencies and plugins:

<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>com.example</groupId>
    <artifactId>my-web-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</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>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

Step 4: Build Your Application

  1. Open the terminal in VS Code and run the following command to build your project:

     mvn clean package
    
  2. This command will compile your code and package it into a .war file located in the target directory of your project.

Step 5: Deploy to Tomcat

Configure Tomcat Server in VS Code

  1. Open the Command Palette (Ctrl+Shift+P).

  2. Type Tomcat: Add Tomcat Server and select it.

  3. Browse to the location where you installed Tomcat and select the folder.

Deploy Your Application

  1. In the Explorer view, right-click the target directory and select Deploy to Tomcat.

  2. Choose the local Tomcat server you configured earlier.

VS Code will deploy your .war file to the Tomcat server.

Step 6: Run and Test Your Application

  1. Open your web browser and navigate to http://localhost:8080/my-web-app/hello.

  2. You should see a page displaying "Hello, World!".

Conclusion

You've successfully deployed a Java web application to an Apache Tomcat server using Visual Studio Code. This setup provides a lightweight and efficient development environment, leveraging the powerful extensions available in VS Code. Happy coding!

Feel free to leave a comment if you have any questions or run into any issues.

Did you find this article valuable?

Support Nikhil Soman Sahu by becoming a sponsor. Any amount is appreciated!