-->
Skip to main content

How to Create Maven Web Project in Eclipse in 2021

How to Create Maven Web Project in Eclipse in 2021

In this tutorial, we will see how to create maven web project in eclipse and how to run a maven web project in eclipse. In an earlier post, we have seen how to create new maven project in the eclipse.

How to Create Maven Web Project in Eclipse
How to Create Maven Web Project in Eclipse

Tools Used: 

Apache Tomcat Server, JDK 14, Eclipse 2020-06, Maven.

How to Create Maven Web Project in Eclipse Step by Step

Step 1:  Create a New Web Project using Maven

In this step, we will create a dynamic web project in eclipse with maven. Open Eclipse in your system and then Go to File > New > Maven Project as shown below.

Create Maven Web Project in Eclipse
Create Maven Web Project in Eclipse

Step 2: Select Maven Project Location and Archetype 

In the Step, we need to select only Use Default Workspace Location checkbox and untick Create a Simple Project checkbox as we need to select archetype for creating a web project on the next screen.

Project Workspace location and Archetype
Project Workspace location and Archetype

Step 3: Archetype Selection for Creating a web app

In this step, we will select an archetype that is used to create a web app that is maven-archetype-webapp as shown below in the image, and click on the Next button.

Maven Archetype Webapp
Maven Archetype Webapp

Step 4: Maven Web Project Archetype Configuration

In this step, we will provide basic information like ArtifactId, GroupId, Version, Package, and parameters if any, and then click on the Finish button.

Maven Web Project Configuration
Maven Web Project Configuration

Our Project Architecture looks like below 

Maven Web Project Architecture
Maven Web Project Architecture


In the above image, we can see the project is giving us an error on the src folder so when we expand that it is showing error in index.jsp file with error message:  The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path.

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

To resolve the above issue we have two ways
  1. Add a dependency in pom.xml
  2. Provided Jar from the tomcat server.
We will see both the ways

1. By Adding dependency in pom.xml

We can copy the latest maven dependency from the maven repository. At the time of writing this article, latest is 4.0.1 as shown below
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

We can simply add that in pom.xml as shown below. After saving pom.xml jar dependency gets downloaded and error goes away.

Adding Dependency in Pom.xml of Maven web Project in Eclipse
Adding Dependency in Pom.xml

2. Provided Jar from Tomcat Server

In this, we just point our project to use existing jar present in the Apache tomcat server. Here below I have shown how to do that.

Right-click on Project then Select Properties (Or Press Alt+Enter) this window will pop up then select Project Facets. In Project Facets Select Dynamic Web Module and then click on checkbox Apache Tomcat under Runtimes if not checked already.

Jar Solution using Tomcat
Jar Solution using Tomcat


Once this is done "javax.servlet.http.HttpServlet" was not found will be fixed.

Step 5:  Build the Maven Project

Now we will see how to build a maven project in eclipse. Right Click on Project > Run As > Maven build..

Run As Maven Build
Run As Maven Build

A new window popup comes as shown below where we need to define the maven goal in eclipse and select skip tests so that test cases don't run when the build is executed so build is created faster and finally click on Run Button.

Maven Web Project Goal Configuration
Maven Web Project Goal Configuration

And Build starts it will download required jars defined in pom.xml and create a war file in the target directory.

Maven Build Logs
Maven Build Logs

Step 6: Run Application

To run this application we need a server so we are simply running it on Apache Tomcat. We need to add Apache Tomcat Server before we can run an application on Tomcat.
We can download the latest from Apache Tomcat Website and configure it.
Here are steps to run the application on the Tomcat server.
Right Click on Project > Run As > Run on Server as shown below

Run on Apache Tomcat Server
Run-on Apache Tomcat Server

Next windows popup gives us the option to define a new server or choose an existing server.

Choose Existing Apache Tomcat Server
Choose Existing Apache Tomcat Server

Now click on Next where we see the option to add our current project to tomcat so that we can run it and then click on the Finish button.

Add Project to Tomcat Server
Add Project to Tomcat Server

As we click on Finish our server starts and when starts successfully it will show logs like this below

Apache Tomcat Server Logs
Apache Tomcat Server Logs



Horray!! we are done now we can access our application on URL : http://localhost:8080/{Projectname}/

which in our case looks like http://localhost:8080/MavenWebProjectUsingEclipse/ and output will show us Hello World! written in our index.jsp file.

Hello World Output
Hello World Output

Conclusion

In this tutorial, we have seen How to Create Maven Web Project in Eclipse and how to run a maven web project in eclipse using the Apache Tomcat server.

Eclipse used it this Project creation is Eclipse IDE 2020-06 With JDK 14. We can simply download eclipse from Eclipse official site.

Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

Happy Learning!!!

Comments

  1. Ho I can create with java 1.8?? always its create with 1.7 java build path

    ReplyDelete
  2. you can always change your build path after going to

    Your project--> Right click on JRE System Library-->Build Path-->Configure Build Path-->Edit(At right side) --> Execution Environment (Choose from dropdown which build path you want to select) Or use Alternate JRE.

    Please let me know if you have any other issue.

    ReplyDelete
  3. Hi, In my Maven Web Project, I have only sr/main package. I don't have 'java' package in src/main. So where to place the Servlets. Please guide me.
    Thanks.

    ReplyDelete
  4. Have you created the project as speciiied in the above article as it will definately create java folder in project, otherwise you can manually create folder in src/main directory.

    ReplyDelete

Post a Comment

If you have any doubts, Please let me know.