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.



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



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



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



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