Setting Up a Spring Web Application
Learn how to create a Spring MVC application that runs using an embedded Tomcat server.
We'll cover the following
Create a Spring Boot project from Spring Initializr. Spring Boot favors convention over configuration and provides default configurations for setting up a servlet container and configuring DispatcherServlet, which is the central servlet in Spring MVC applications.
Choose "Maven" as "Project", "Java" as "Language" and the latest stable version of Spring Boot. For the "Project Metadata" we have used the following:
Group:
io.datajek.springmvc
Artifact:
tennis-player-web
We will add the Spring Web dependency
and the Spring Boot Dev Tools
dependency. Click "Generate" to download the project.
Once the project has been imported in the IDE, we can see a hierarchy of folders as follows:
src/main/java
: This is where the source code is placed.src/main/resources
: This folder is used for resources like property or configuration files.src/test/java
: All test code is placed in this folder.src/main/webapp
: We will create awebapp
folder for the JSP files, web configuration files and assets like CSS or images etc.
Required dependencies
The spring-boot-starter-web
dependency simplifies web development using Spring MVC and an embedded Tomcat server. It includes all the necessary common dependencies, such as the Spring framework, logging, validation, JSON support, and more, with compatible versions pre-configured. This eliminates the need to manage a multitude of dependencies and ensures smoother development compared to traditional Spring MVC applications.
Jasper dependency
To enable JSP compilation and rendering in our application, we must include Jasper which is the JSP Engine for Tomcat. It is responsible for compiling JSP files into Java servlets, which are then executed to generate dynamic web content. It parses JSP files, translates them into Java code, compiles the Java code into servlets, and finally, executes the servlets to produce HTML content dynamically. This makes Jasper a critical component for enabling the use of JSP technology in web applications deployed on Tomcat servers. We will add the tomcat-embed-jasper
dependency, which brings JSP support in Spring Boot as follows.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.