Servlet Maven Configuration Example

Maven is the Project Management Tool, that helps developers to build the project easily (make create, add, update, compile, test, and deploy work easily).

  You can download Maven from Official Site

You can find the servlet dependency file here

Technologies used here are as below

  1. Apache Tomcat IDE 14
  2. Tomcat Server apache-tomcat-9.0.68

Here we will start a new project with maven

Create a new project in NetBeans
Create a new project in NetBeans
Netbeans Wep Application Project
Netbeans Web Application Project

Select Java with Maven -> Web Application then next

Project Names and group id
Project Names and group id

Give the Project name Here ServletMavenExample and group id com.ebhor

Select Server in Netbeans
Select Server in Netbeans

If you have tomcat downloaded then select its location and provide the username and password.

If the user name and password don’t exist then it will create for you.

Select Java version in NetBeans
Select the Java version in NetBeans

Here Our Server is Apache Tomcat and Java EE version in Java EE 7 Web.

Project Explorer

On the left side of the window, you will find project explorer.

Right-click on project Explorer and run it.

on running it will download its dependency.

Run Maven Project in Netbeans
Run Maven Project in Netbeans

On successfully downloading it will show BUILD SUCCESS.

Many times it will show errors like the below:

Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war)

To Resolve this error go to the pom.xml file and update the plugin version of org.apache.maven.plugin from 2.2 to 3.3.1.

Again build and run the project.

This will resolve your problem.

Complete pom.xml as below.

servlet dependency in pom.xml

servlet maven dependency is as below

pom.xml

Create a Servlet in NetBeans

Right-click on package com.ebhor.servletmavenexample -> new ->servlet

this will as class name as below.

Create Servlet in Netbeans
Create Servlet in Netbeans

This will create a new servlet named HelloServlet and URL pattern /HelloServlet

index.html

This file is already created here we are not creating JSP file just testing with index.html

This HTML page contains a text box and submits button.

On clicking on submit form will submit to /HelloServlet action.

HelloServlet.java

This will get a request parameter named name and display its value in the browser.

After completion all work execute the program it will ask name to user and show his/her name in next browser.

Result

ServletMaven Example1
ServletMaven Example1
ServletMaven Example2
ServletMaven Example2

Read More

Servlet Annotation WebServlet Example

ServletConfig to access multiple initial parameter value