In this blog post, I’ll guide you step by step through building, configuring, and deploying a Java-based web application using Java 17, Maven, and Apache Tomcat on Ubuntu 24.04 LTS. You’ll learn how to set up a project using Maven, create a servlet, build a WAR file, and finally deploy it on Tomcat. Each command and configuration step will be explained in detail to help you understand the “why” behind each action.
Prerequisites
Make sure you have the following installed:
- Java 17 (JDK 17): This tutorial uses Java 17, which is one of the newer versions of Java.
- Maven (3.6.3 or newer): Maven is a popular tool for managing Java projects.
- Apache Tomcat (9.x or newer): Tomcat is the servlet container that will host our application.
- An IDE (e.g., IntelliJ IDEA, Eclipse, VS Code): While you can work with a text editor, an IDE provides a more convenient way to manage and edit Java projects.
To get started, you will also need to install Java, Maven, and Tomcat on your Ubuntu 24.04 LTS system.
Install Java 17
sudo apt update
sudo apt install openjdk-17-jdk -y
Install Maven
sudo apt install maven -y
Install Tomcat
To download and set up Tomcat, you can follow the official Apache Tomcat download page to get the latest version suitable for your needs.
Step 1: Create a Maven Project
The first step is to create a new Maven web application. We’ll use a Maven archetype, which provides a template to get us started quickly.
Command:
sudo mvn archetype:generate -DgroupId=com.example -DartifactId=simple-webapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Explanation:
mvn archetype:generate
: This command generates a new Maven project.-DgroupId=com.example
: Specifies the group identifier, usually the domain name of your company in reverse. Here, it’scom.example
.-DartifactId=simple-webapp
: Defines the project name.-DarchetypeArtifactId=maven-archetype-webapp
: Uses themaven-archetype-webapp
template to generate a basic web project.-DinteractiveMode=false
: Runs the command without prompts, using default settings for everything else.
Outcome:
This command will create a new directory called simple-webapp
containing the basic structure of a Maven web application.
Step 2: Navigate to the Project Directory
Now that we have created our project, we need to move into the newly generated directory.
Command:
cd simple-webapp
Explanation:
This command changes the current working directory to simple-webapp
, which is the folder where our project files are stored.
Step 3: Understand the Project Structure
The generated project has the following structure:
simple-webapp/
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── HelloServlet.java
│ │ ├── webapp/
│ │ │ ├── WEB-INF/
│ │ │ │ └── web.xml
│ │ │ └── index.jsp
Explanation of Folders and Files:
pom.xml
: Maven configuration file. This file manages dependencies and build plugins.src/main/java
: Contains all Java source files, including our servlets.src/main/webapp
: Contains web resources, such as JSP files and configurations likeweb.xml
.
Step 4: Update the pom.xml
File
We need to update pom.xml
to ensure our project can work with Java 17 and use the required servlet API.
Updated pom.xml
:
Replace the content of your pom.xml
file with the following:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>simple-webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>
Explanation:
- Dependencies: The
javax.servlet-api
allows us to create servlets. The<scope>provided</scope>
means it will be provided by the servlet container (Tomcat) at runtime. - Compiler properties: The
maven.compiler.source
andmaven.compiler.target
properties ensure the application is compiled using Java 17.
Step 5: Create the HelloServlet
Java Class
We will create a Java servlet to handle requests and respond with a simple message.
File Location:
src/main/java/com/example/HelloServlet.java
HelloServlet.java
Content:
package com.example;
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("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().write("<h1>Welcome to the Simple Web Application!</h1>");
}
}
Explanation:
@WebServlet("/hello")
: Maps the servlet to the/hello
URL. It means that when/hello
is accessed in a browser, this servlet will handle the request.doGet()
Method: This method processes the GET request and writes an HTML response.
Important: Ensure there are no servlet mappings in web.xml
if you are using @WebServlet
to avoid conflicts.
Step 6: Create a Simple web.xml
File
web.xml
is a deployment descriptor that tells Tomcat about the application configuration.
File Location:
src/main/webapp/WEB-INF/web.xml
web.xml
Content:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0">
</web-app>
Explanation:
Since we’re using annotations like @WebServlet
, there is no need to define servlets in web.xml
, so this remains minimal.
Step 7: Create index.jsp
for the Application Home Page
Now, we create a JSP file that acts as a landing page.
File Location:
src/main/webapp/index.jsp
index.jsp
Content:
<!DOCTYPE html>
<html>
<head>
<title>Simple Web Application</title>
</head>
<body>
<h1>Hello, Welcome to the Simple Web Application!</h1>
<p><a href="hello">Click here to view the servlet response</a></p>
</body>
</html>
Explanation:
This page serves as the landing page for the application, and it provides a link that points to the servlet.
Step 8: Build and Package the Application
To deploy our application to Tomcat, we need to package it as a WAR (Web Application Archive).
Command:
mvn clean package
Explanation:
mvn clean
: Cleans the project by deleting thetarget
directory to remove old build files.mvn package
: Compiles the source code and packages it as a WAR file (simple-webapp-1.0-SNAPSHOT.war
) located in thetarget/
directory.
Step 9: Deploy the WAR File to Tomcat
We will now deploy the WAR file to Tomcat.
- Copy the WAR File to Tomcat:
cp target/simple-webapp-1.0-SNAPSHOT.war <TOMCAT_HOME>/webapps/
- Restart Tomcat:
<TOMCAT_HOME>/bin/shutdown.sh <TOMCAT_HOME>/bin/startup.sh
Explanation:
- WAR Deployment: Copying the WAR file to Tomcat’s
webapps
directory triggers the deployment of the application. - Restarting Tomcat: Restarting Tomcat ensures the new application is properly loaded and deployed.
Conclusion
Congratulations! You’ve successfully created and deployed a simple Java-based web application using Maven on Ubuntu 24.04 LTS. You learned how to generate a Maven project, create a servlet, update configurations, and deploy your application on Tomcat.
Feel free to expand this project by adding more servlets, JSPs, or even integrating a backend database to make your web application more dynamic. If you encounter issues, refer to the Tomcat logs (catalina.out
) for detailed error messages.
Any queries pls contact us @Rushi-InfoTech
Happy coding! 🚀