Over a million developers have joined DZone.

Deploying Spring Apps to Tomcat (Without web.xml)

DZone's Guide to

Deploying Spring Apps to Tomcat (Without web.xml)

For those out there who haven't made the jump to Servlet's annotations, see how to deploy a simple sample Spring app to Tomcat without a web.xml file.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

Since the Servlet 3 specification, web.xml is no longer needed for configuring your web application and has been replaced by annotations. In this article, we will look at how to deploy a simple Spring-based application without web.xml to Tomcat 8.5.x.

Creating an Empty Application

Use the following command to create an empty web application using the Maven webapp archetype:

mvn archetype:generate -DgroupId=info.sanaulla -DartifactId=spring-tomcat-sample 
   -Dversion=1.0 -DarchetypeArtifactId=maven-archetype-webapp

Delete the web.xml created in src\main\webapp\WEB-INF. Then, we need to update the maven-war-plugin not to fail if web.xml is missing. This can be done by updating the plugin information in the build tag as shown below:


Setting Up Dependencies

We would need the following dependencies added to the pom.xml:


And their corresponding version properties are given below:


Configuring Our H2 Embedded DB

Let us create a configuration bean for configuring our DataSource, which is an Embedded H2, and create a Bean of type NamedParameterJdbcTemplate:

public class DBConfiguration {

    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()

    public NamedParameterJdbcTemplate namedParamJdbcTemplate() {
        NamedParameterJdbcTemplate namedParamJdbcTemplate =
            new NamedParameterJdbcTemplate(dataSource());
        return namedParamJdbcTemplate;

Then we need a SQL script schema.sql that will set up the tables while data.sql will seed them with data each time the application is run. Let us create the corresponding files in src/main/resources:

-- schema.sql

  first_name VARCHAR(512) NOT NULL,
  last_name VARCHAR(512) NOT NULL,
  date_of_birth TIMESTAMP NOT NULL,
  place_of_birth VARCHAR(512)

-- data.sql
INSERT INTO person(first_name, last_name, date_of_birth, place_of_birth) 
VALUES ('First', 'Last', DATE '1990-02-21', 'Bangalore');
INSERT INTO person(first_name, last_name, date_of_birth, place_of_birth) 
VALUES ('First2', 'Last2', DATE '1987-02-21', 'Mumbai');
INSERT INTO person(first_name, last_name, date_of_birth, place_of_birth) 
VALUES ('First3', 'Last3', DATE '1996-02-21', 'Chennai');
INSERT INTO person(first_name, last_name, date_of_birth, place_of_birth) 
VALUES ('First4', 'Last4', DATE '1978-02-21', 'Delhi');

Creating an API Controller

Lets us create a model class Person:

public class Person {
    private Integer id;
    private String firstName;
    private String lastName;
    private Date dateOfBirth;
    private String placeOfBirth;

And a corresponding DAO class PersonDAO:

public class PersonDAO {

    @Autowired NamedParameterJdbcTemplate jdbcTemplate;

    public List < Person > getPersons() {
        return jdbcTemplate.query("SELECT * FROM person",
            (ResultSet rs, int rowNum) - > {
                Person p = new Person();
                return p;

The API controller is as shown below:

public class ApiController {

    @Autowired PersonDAO personDao;

    public ResponseEntity << ? > getPersons() {
        return ResponseEntity.ok(personDao.getPersons());

Deploying Our App to Tomcat

I assume that you have downloaded Tomcat and extracted it to your file system. For development purposes, I prefer to update server.xml to configure the context and point to the exploded WAR directory as shown below:

<Context path="/sample" reloadable="true" docBase="G:\samples\spring-tomcat-sample\target\spring-tomcat-sample" />

For production-related deployments, you can upload the WAR to the server.

So once you start the server, you can verify if the app has been deployed correctly by visiting the API URL http://localhost:8080/sample/api/persons in the browser to get the data as shown below:

Configuring Thymeleaf

Let us now configure Thymeleaf as our templating engine to serve the UI for our application. Configuring it requires the following bean definitions:

  • Technique for resolving the templates. There are many possibilities, and we will use Classpath based template resolver

  • Create an instance of SpringTemplateEngine and set the template resolver technique

  • Setup Spring’s ViewResolver to use the SpringTemplateEngine

public class ViewConfiguration {

    public ClassLoaderTemplateResolver templateResolver() {
        ClassLoaderTemplateResolver templateResolver =
            new ClassLoaderTemplateResolver();
        return templateResolver;

    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine =
            new SpringTemplateEngine();
        templateEngine.addDialect(new LayoutDialect());
        return templateEngine;

    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver =
            new ThymeleafViewResolver();
        return viewResolver;


Creating a View Controller

Let us create a simple controller that will serve our UI templates:

public class ViewController {

    @Autowired PersonDAO personDao;

    public String index(Model model) {
        model.addAttribute("persons", personDao.getPersons());
        return "index";

Creating a Thymeleaf Based Template

We need to create a template index.html, which is returned from the above controller:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
        <link rel="stylesheet" href="https://bootswatch.com/4/cerulean/bootstrap.min.css" />
        <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
            <a class="navbar-brand" href="#">Spring on Tomcat</a>
        <div class="container">
            <div class="page-header">
                <h1>Spring on Tomcat</h1>
            <div class="row">
                <div class="col">
                    <ul class="list-group">
                        <li class="list-group-item" th:each="p : ${persons}">
            [[${p.firstName}]] [[${p.lastName}]], 
            Date Of Birth: [[${#dates.format(p.dateOfBirth, 'dd/MMM/yyyy')}]]
            Place: [[${p.placeOfBirth}]]
        <script src="https://bootswatch.com/_vendor/jquery/dist/jquery.min.js"></script>
        <script src="https://bootswatch.com/_vendor/popper.js/dist/umd/popper.min.js"></script>
        <script src="https://bootswatch.com/_vendor/bootstrap/dist/js/bootstrap.min.js"></script>


In this article, we looked at configuring a simple Spring MVC-based application without web.xml (without using Spring Boot) and deploying it to Tomcat. We also configured Thymeleaf to render our UI templates. The complete code for this can be found here.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

java ,servlet 3.0 annotations ,tomcat 8 ,spring app ,tutorial

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}