How to create a dynamic web project using Maven in Eclipse IDE

  •  
  •  
  •  
  •  
  •  
  •  

Hi friends, in this tutorial, I will show you How to create a dynamic web project using Maven in Eclipse IDE. Maven is a very powerful tool and if you know how to use it then you can achieve maximum output with minimum effort. I have seen many people who used Maven as a build tool only and for local setup, they stored the jar files in the lib folder. But you can eliminate these dependencies of storing files in the lib folder and leave everything on Maven, Maven will handle all the dependencies itself. In general, the Maven tool will help you to create project structure, resolve all dependencies, build and so on.

Learn how to create dynamic web project using Maven in Eclipse IDE

Create a dynamic web project using Maven

Here is a simple tutorial which will help you to create a dynamic web project using Maven in Eclipse IDE. I will guide you step by step to achieve the desired goal. Just follow the steps below.

Required Tool

Step 1: Create a Maven Project in Eclipse IDE

First, create a new Maven project in Eclipse. Click on File->New->Maven Project

Create a Maven project in Eclipse IDE

If you can’t find Maven Project option then click on File->New->Project->Maven Project from the list.

Choose Maven Project from the Project list in Eclipse IDE

Step 2:

After clicking on Next button, you will get a new window. Do not change anything, just click on the Next button.

Maven project name and location settings in Eclipse IDE

Step 3:

Next, select maven-archetype-webapp from the Artifact Id list and click Next.

Select archetype during the maven web project creation

Step 4:

When you are creating a dynamic web project using Maven in Eclipse IDE, you have to keep some basic knowledge in your mind such as,

Group Id: It is a unique identifier which identifies you project among all others, so we need to force a naming schema and it should follow the Java’s package name rule. This means it must start with a reversed domain name you control. For example, com.mtb.web

Artifact Id: It is the name of jar file without any version. For example, mtn_dynamic_web_project

Version: If you distribute it, it should have a version. You can specify any version of your own.

So, these are the basic things you have to remember when you are creating a dynamic web project using Maven in Eclipse IDE.

Specify archetype parameters while you create a dynamic web project using maven in Eclipse IDE

That’s all friends. Your new dynamic web project using maven has just created. The default project folder structure is like below –

Project folder structure of newly created dynamic web project using maven in Eclipse IDE

Step 5:

Now, let’s create a servlet class. Right-click on the project name and choose New->Class and name it HelloWorldServlet and store it within the servlets package as shown in the image below.

Creating a new Servlet class

Now, put the below code within the newly created servlet class.

HelloWorldServlet.java

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Mitra
 *
 */

@WebServlet("/hello-world")
public class HelloWorldServlet extends HttpServlet {
  
  private static final long serialVersionUID = 1L;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.println("<h1 align='center'>Welcome to <a href='http://www.mitrajt.com/'>Mitrajit's Tech Blog</a><h1>");
    out.println("<h2 align='center'>This is my first dynamic web project using Maven in Eclipse IDE<h2>");
    out.flush();
    out.close();
  }
}

Also, put the below code in the default index.jsp

index.jsp

<html>
<body>
<h1 align='center'>Welcome to <a href='http://www.mitrajt.com/'>Mitrajit's Tech Blog</a><h1>
<h2 align='center'>
  Please click on the following link to see the example of 
  <a href="http://localhost:8080/mtb_dynamic_web_project/hello-world">Dynamic web application using Maven in Eclipse IDE</a>
</h2>
</body>
</html>

Step 6:

Let’s build the project using the Maven command – mvn clean install. Click on Run As->Maven build and write clean install in the Goals column. Click on Run button.

Maven clean install setup for project build

Maven build success message from Eclipse console.

Successful maven build from the console after click on Run button

Step 7:

Let’s deploy the maven dynamic web project in the browser and for that just click on the project name and click on Run As->Run on Server or just copy and paste the following URL in the browser address bar — http://localhost:8080/mtb_dynamic_web_project/

Index page running on the browser

Hello world page running on the browser

That’s all friends. You can see and realize that it is really very easy and simple to create a dynamic web project using Maven in Eclipse IDE.

If you face any problem feel free to inform me in the comment section below. Please like and share this tutorial to create a dynamic web project using Maven in Eclipse IDE with others. Kindly subscribe Mitrajit’s Tech Blog to keep yourself updated with the upcoming article.

About Mitrajit

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove you are a human *