Spring Boot: Sample web app with embedded tomcat

In this post we will see how to create a view page and way to render it. Here we will use embedded tomcat as a container as well.

You can easily understand and run this example if you know Spring MVC, some configuration and dependency related changes are required.

Let’s see files used in this example in detail.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


	<description>Sample project to show use of embedded tomcat as a container.</description>

		<relativePath /> <!-- lookup parent from repository -->








Only 2 new dependency added here is

  • spring-boot-starter-tomcat:

    This starter will add required dependency for embedded tomcat, screen below shows what exactly downloaded by maven.

  • tomcat-embed-jasper:

    This dependency provide JSP support for tomcat. Boot has some limitations related to JSP as per Spring docs. (Read more…)

    When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

    • With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work because of a hard coded file pattern in Tomcat.
    • Jetty does not currently work as an embedded container with JSPs.
    • Undertow does not support JSPs.

    So to get JSP support we need to add this dependency.




Here boot provides built-in properties to set prefix and suffix for view, which is exactly same in Spring MVC like

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/view/" />
	<property name="suffix" value=".jsp" />


package com.kode12.simplewebapptomcat.controller;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

public class WelcomeController {

	public String welcome(ModelMap map) {
		map.put("currentDate", new Date());
		return "welcome";

Here we just created on controller annotated by @Controller annotation and one method which returns view name as a String.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>kode12.com | Welcome</title>
		Welcome koder! Current Date is ${currentDate }

This file created under /WEB-INF/view directory and used to display message and render variable currentDate which is set from WelcomeController.java.


package com.kode12.simplewebapptomcat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SimpleWebAppTomcatApplication {

	public static void main(String[] args) {
		SpringApplication.run(SimpleWebAppTomcatApplication.class, args);

As we see in previous demos file contain main method and used as a starting point of boot application.

Run an application

To run this example execute method from SimpleWebAppTomcatApplication.java file
And open http://localhost:8081/welcome url in your favorite browser., output will be shown in below image.

At Last, i would like to hear from you guys for some mistakes, new blogs, demo, ideas or anything which will help us to make our blog better.

Share current post by copy: http://goo.gl/pVMljU