Setting Up a Web Application

Learn how to create a simple web application that runs using the Tomcat server.

Before we delve into Spring MVC, we will lay down the foundational knowledge and prerequisites needed to understand and work with it. We will learn the essential steps in setting up a web application environment. These include understanding how to create a simple Maven project, include dependencies like the Java EE Web API, configure plugins for compiling code and deploying to a servlet container, and configure the web.xml file. This knowledge provides a basis for learning more advanced topics like Spring MVC, which is a framework for building web applications using the Model-View-Controller pattern.

Creating a simple Maven project

We will create a web application using Maven as it makes development easier. Maven is a Java build tool which manages all the dependencies of an application. Create a simple Maven project using the IDE of your choice. Maven provides a set of sample projects which are called archetypes with predefined configuration.

We will skip the archetype selection for this project as we want to configure the application ourselves. The name of a Maven project has two parts, group Id and artifact Id. We have supplied the following values:

Group id: io.datajek.springmvc

Artifact id: tennis-player-web

Packaging: War (web archive)

The packaging type is set to WAR because we are creating a web project. A WAR file contains servlet, JSP, XML, CSS, HTML and JS files that can be deployed on a servlet container while a JAR file contains Java classes and associated metadata as a single file.

Once the project is created, we can see a hierarchy of folders.

  • The source code is placed in src/main/java.

  • Any resources like a property file or XML file goes in src/main/resources.

  • All test code is placed in src/test/java.

  • Any resources for the test code reside in src/test/resources.

Java EE Web API dependency

The pom.xml shows the basic information about the group Id and artifact Id that we provided. To run a web application, we need a number of dependencies or jars. Maven downloads those jars, saving us the time to manually download them. We will add the Java EE Web API dependency to the pom.xml file as follows:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.