Create a Web Application With Spring Boot and Deploy Into Tomcat

By Yashwant Chavan, Views , Last updated on 19-Apr-2019

In this tutorial, You will learn how to create a simple web application using Spring Boot and deploy it into a Tomcat Server.

tags spring spring boot

Setting up a Spring Boot Application

Spring Boot Project Structure

Tools and Technologies

  1. Eclipse IDE (Kepler)
  2. Apache Maven 3.0.4
  3. JDK 1.8
  4. Spring Boot 2.0.2.RELEASE

Project Dependencies

We’re going to setup a simple Spring Boot web application using available starter templates. Spring Boot Starter Web provides all required dependencies and auto configurations to build web application.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

As we are using JSP to render our view. To enable support for JSP's, we would need to add a dependency on tomcat-embed-jasper

<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
	<scope>provided</scope>
</dependency>

WAR packaging

We need to package a WAR application instead of a JAR.

<packaging>war</packaging>

pom.xml

<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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.technicalkeeda</groupId>
	<artifactId>SpringBoot</artifactId>
	<version>1.0</version>
	<packaging>war</packaging>
	<name>SpringBoot</name>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>repository.spring.release</id>
			<name>Spring GA Repository</name>
			<url>http://repo.spring.io/release</url>
		</repository>
	</repositories>

</project>

Spring Boot Starter Web auto configures

  1. Dispatcher Servlet
  2. Error Page
  3. Web Jars to manage your static dependencies
  4. Embedded Servlet Container (Tomcat)

application.properties

You need to define application specific configurations like application context path and View Resolver configurations inside "application.properties" file which is localted under /resources folder.

server.servlet.context-path=/technicalkeeda
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Configuring a View Resolver

We need to configure the view resolver with the prefix and suffix as we have kept our views inside

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Controller Class (HelloController.java)

Create a controller class called as HelloController and annotate it with @Controller. Define the simple get method as hello() along with @RequestMapping annotation. It simply forwards your matching request to hello.jsp page along with name variable value.

package com.technicalkeeda.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

	@RequestMapping(value = "/hello")
	public ModelAndView hello() {
		return new ModelAndView("hello").addObject("name", "Yashwant");
	}
}

View JSP (hello.jsp)

Simple jsp page with display the ${name} variable value.

<html lang="en">
<html>
<head><title>Spring Boot Example</title></head>
	<body>
		<h1>Hello ${name} !!!</h1>
	</body>
</html>

Boot Strap class (Application.java)

package com.technicalkeeda.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = { "com.technicalkeeda" })
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

Server Startup logs

Web Application deployed into Spring boot embedded tomcat server.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.2.RELEASE)

2019-04-16 11:14:00.915  INFO 15316 --- [           main] com.technicalkeeda.app.Application       : Starting Application on DESKTOP-V839OVQ with PID 15316 (C:\java-projects\SpringBoot\target\classes started by Yashwant in C:\java-projects\SpringBoot)
2019-04-16 11:14:00.915  INFO 15316 --- [           main] com.technicalkeeda.app.Application       : No active profile set, falling back to default profiles: default
2019-04-16 11:14:01.040  INFO 15316 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@35083305: startup date [Tue Apr 16 11:14:01 IST 2019]; root of context hierarchy
2019-04-16 11:14:03.416  INFO 15316 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-04-16 11:14:03.468  INFO 15316 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-04-16 11:14:03.468  INFO 15316 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2019-04-16 11:14:03.499  INFO 15316 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Java\jdk1.8.0_144\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Java/jre1.8.0_144/bin/server;C:/Java/jre1.8.0_144/bin;C:/Java/jre1.8.0_144/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Java\jdk1.8.0_144\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files\Geth;C:\Program Files\nodejs\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\apache-maven-3.3.9\bin;C:\Program Files\Microsoft VS Code\bin;C:\xampp\php;C:\composer;C:\WINDOWS\System32\OpenSSH\;C:\Ruby24-x64\bin;C:\Users\Yashwant\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Git\bin;C:\Program Files\Java\jdk-9.0.1\bin;C:\Program Files\Geth;C:\Program Files\nodejs\node_modules\npm\bin;C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin;C:\Program Files\nodejs;C:\Users\Yashwant\AppData\Roaming\npm;C:\Program Files\Microsoft VS Code\bin;C:\OpenSSL\bin;C:\OpenSSL\include\openssl;C:\sqlite-dll-win64-x64-3240000;C:\Users\Yashwant\AppData\Roaming\Composer\vendor\bin;C:\Program Files\php-7.2.8-src;;C:\eclipse-java-kepler\eclipse;;.]
2019-04-16 11:14:03.827  INFO 15316 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2019-04-16 11:14:03.827  INFO 15316 --- [ost-startStop-1] o.a.c.c.C.[.[.[/technicalkeeda]          : Initializing Spring embedded WebApplicationContext
2019-04-16 11:14:03.827  INFO 15316 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2803 ms
2019-04-16 11:14:04.077  INFO 15316 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2019-04-16 11:14:04.093  INFO 15316 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-04-16 11:14:04.093  INFO 15316 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-04-16 11:14:04.093  INFO 15316 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-04-16 11:14:04.093  INFO 15316 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-04-16 11:14:04.296  INFO 15316 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-04-16 11:14:04.640  INFO 15316 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@35083305: startup date [Tue Apr 16 11:14:01 IST 2019]; root of context hierarchy
2019-04-16 11:14:04.767  INFO 15316 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public org.springframework.web.servlet.ModelAndView com.technicalkeeda.controller.HelloController.hello()
2019-04-16 11:14:04.782  INFO 15316 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-04-16 11:14:04.782  INFO 15316 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-04-16 11:14:04.829  INFO 15316 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-04-16 11:14:04.829  INFO 15316 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-04-16 11:14:05.138  INFO 15316 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-04-16 11:14:05.309  INFO 15316 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/technicalkeeda'
2019-04-16 11:14:05.309  INFO 15316 --- [           main] com.technicalkeeda.app.Application       : Started Application in 4.999 seconds (JVM running for 5.802)

output

Now let’s execute the Application.java and open the URL http://localhost:8080/technicalkeeda/hello in browser to check the results.

Spring Boot war into tomcat
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]