Maven Creates A Web Project

In this tutorial, we'll walk you through creating a simple Java web project using Maven. We'll use the Maven Archetype Plugin to generate the project structure, add a basic servlet, and package the project as a WAR (Web Application Archive) file.

1. Install Maven

Before you start, make sure you have Maven installed. If you haven't, follow the Maven installation guide: https://iditect.com/guide/maven/maven-install-configure.html

2. Generate the web project

Open a terminal (command prompt) and navigate to the directory where you want to create the project. Use the following Maven command to generate the project structure using the Maven Archetype Plugin:

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

This command creates a new web project with the specified group ID (com.example) and artifact ID (my-web-app).

3. Open the project in your favorite IDE

Import the newly created project into your favorite IDE (Eclipse, IntelliJ, etc.) as a Maven project. This will allow the IDE to recognize the project structure and manage the dependencies automatically.

4. Edit the pom.xml file

By default, the generated pom.xml file doesn't include the necessary dependencies and plugins for building a Java web application. Open the pom.xml file and update it with the following content:

<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>
  <name>My Web App</name>

  <dependencies>
    <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-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

This configuration adds the necessary Java Servlet API dependency and sets the Java source and target versions to 1.8.

5. Create a servlet

In the src/main/java directory, create a new Java class named HelloServlet in the package com.example:

package com.example;

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(name = "HelloServlet", urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.getWriter().append("Hello, World!");
  }
}

This servlet extends HttpServlet and overrides the doGet() method to respond with "Hello, World!" when accessed at the /hello URL. The @WebServlet annotation maps the servlet to the URL pattern specified.

6. Update the web.xml (optional)

If you prefer not to use the @WebServlet annotation or if your application server does not support it, you can map the servlet using the web.xml file instead.

Open the src/main/webapp/WEB-INF/web.xml file and add the following servlet and servlet-mapping elements:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.example.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

Make sure to remove or comment out the @WebServlet annotation in the HelloServlet class if you choose to use the web.xml configuration.

Now, you have created a simple servlet that responds with "Hello, World!" when accessed at the /hello URL. In the next steps, you would build the web application and deploy it to a servlet container like Apache Tomcat or Jetty to test the servlet.

  1. Maven web application archetype:

    • Maven archetypes provide project templates. For web applications, use the maven-archetype-webapp archetype:
    mvn archetype:generate -DgroupId=com.example -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
    
  2. Adding dependencies to Maven web project:

    • Declare dependencies in the <dependencies> section of the pom.xml.
    <!-- Example dependency in POM -->
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Additional dependencies... -->
    </dependencies>
    
  3. Configuring web.xml in Maven project:

    • Place the web.xml file in the src/main/webapp/WEB-INF directory.
    <!-- Example web.xml -->
    <web-app>
        <!-- Servlet and other configurations... -->
    </web-app>
    
  4. Maven WAR plugin usage for web projects:

    • Use the Maven WAR plugin to package the web application.
    <!-- Example Maven WAR plugin configuration -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
                <!-- Additional configurations... -->
            </plugin>
        </plugins>
    </build>
    
  5. Using Maven for servlet and JSP projects:

    • Declare dependencies for servlets and JSP in the pom.xml.
    <!-- Example servlet and JSP dependencies in POM -->
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!-- Additional dependencies... -->
    </dependencies>