Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Spring Boot - Project Deployment Using Tomcat
Next article icon

Spring Boot - Project Deployment Using Tomcat

Last Updated : 09 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because it’s a rapid production-ready environment that enables the developers to directly focus on the logic instead of struggling with the configuration and setup.

And Tomcat is a very popular Java Servlet Container. Tomcat is the default spring boot server which can manage multiple applications within the same application which avoids multiple setups for each application in a single application.

In this article, we will create a simple spring boot application in which we will deploy the application using the Tomcat server.

Process for Project Deployment on Spring Boot 

Spring Boot application deployment on the Tomcat Server involves three steps:

  1. Create a sample Spring Boot Application
  2. Ways of creation of a Spring Boot WAR
  3. Deploying the WAR to Tomcat - Preferably higher versions of Tomcat are required.

To use the appropriate Tomcat version and Java version, it will be helpful to visit the HTML file. 

Step 1: Creating a sample Spring Boot Application for Tomcat

This is a spring boot web application project, i.e. the project needs to be deployed on Tomcat. The project can be created as a maven-based project and hence required dependencies we can specify in the pom.xml file.

pom.xml->Configurations can be specified in a Maven project via pom.xml 

As the project needs to be deployed using Tomcat, it has to be packaged as "WAR"(Web Application Resource or Web Application Archive).

Basically, pom.xml should have spring boot related dependencies like

  • spring-boot-starter-parent
  • spring-boot-starter-web
  • spring-boot-starter-tomcat and its scope is set to be provided "geeks-web-services" should be the name of "WAR" file as per pom.xml

Example 1:

XML
<?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">     <modelVersion>4.0.0</modelVersion>      <groupId>com.geeksforgeeks</groupId>     <artifactId>spring-boot-war-deployment-example-on-tomcat</artifactId>     <version>0.0.1-SNAPSHOT</version>     <!-- As we need to deploy the project as WAR, this is needed -->     <packaging>war</packaging>      <name>SpringBootWarDeploymentOnTomcatServer</name>     <description>Demo project for Spring Boot deployable on Tomcat</description>      <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.2.2.RELEASE</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>      <properties>         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>         <java.version>1.8</java.version>     </properties>     <dependencies>         <!-- Spring web dependency -->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>             </dependency>         <!--Servlet container has to be set as provided -->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-tomcat</artifactId>             <scope>provided</scope>         </dependency>            <build>     <!-- Our final war file name is geeks-web-services -->     <finalName>geeks-web-services</finalName>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>                   </build>   </project> 

A controller class is required through which we can able to invoke the services either as a GET method or  POST method. As a sample, let us see "SampleRestControllerExample.java" which contains two methods as "GET" methods

Two methods are written which provide a static text as output. 

As this is a simple example, just let us show as on calling "geeks-web-services/hello", displays Hello Geek, this is a simple hello message to take care and have a nice day.

on calling "geeks-web-services/greet", displays Hello Geek, Fast and easy development can be possible on Spring-based applications by reducing source code.

@RestController is a specialized version of the controller. It includes the @Controller and @ResponseBody annotations, and as a result, simplifies the controller implementation.

Example 2: SampleRestControllerExample.java

Java
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;  // Annotation @RestController // Class public class SampleRestControllerExample {     @GetMapping("/hello")     // Easy method just to print encouraging and consoling     // words     public String hello()     {         return "Hello Geek, this is a simple hello message to take care and have a nice day.";     }      @GetMapping("/greet")     // Easy method just to print greeting message by saying     // spring-based applications     public String greet()     {         return "Hello Geek, Fast and easy development can be possible on Spring-based applications by reducing source code;.";     } } 

Step 2: Ways of creation of a Spring Boot WAR

There are 3 ways of creating a Spring Boot WAR:

  • "Main" class should contain with "extends SpringBootServletInitializer" class in the main class.
  • "Embedded servlet container should be marked as provided.
  • Packaging should be WAR

SpringBootWarDeploymentOnTomcatServer.java is our Main class

Example:

Java
// Java Program to Illustrate SpringBoot WarDeployment // On Tomcat Server  // Importing required classes import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; // Annotation @SpringBootApplication  // Class public class SpringBootWarDeploymentOnTomcatServer       extends SpringBootServletInitializer {       @Override       // Configuring method has to be overridden       protected SpringApplicationBuilder         configure(SpringApplicationBuilder application){             return application.sources(                   SpringBootWarDeploymentOnTomcatServer.class);       }          // Method 2       // Main driver method       public static void main(String[] args){             SpringApplication.run(                   SpringBootWarDeploymentOnTomcatServer.class,                   args);       } } 


We can run the maven steps using the command line,

mvn clean install -X

As an image let us see the project structure and the target folder:

project structure and the target folder

When the WAR file created successfully, it will show the WAR File path with a message i.e. "BUILD SUCCESS" in the console, as shown in the below image:

warfile_successful_creation
WAR File has created

Step 3: Deploying the WAR to Tomcat 

Apache Tomcat Server has to be installed if not installed earlier. Preferably higher versions of tomcat will be helpful. As an example, We are using tomcat version 9.0.x.

To use the appropriate tomcat version and java version, it will be helpful to check the HTML file. In our previous step, we have got "geek-web-services.war" and it has to be copied under the "webapps" folder of tomcat.

Copied geek-web-services.war

Now, open the command prompt and point to the tomcat location's bin folder, 

From bin folder "startup.bat" needs to be given
From bin folder "startup.bat" needs to be given


SpringApplicationStarted-ouput-screen
Image shows Spring Boot has started

And also our war file is deployed in tomcat too. The below screenshot confirms that:

geek-web-services war file deployed
geek-web-services war file deployed

We can test the same by executing the below URL in a browser:

geek-web-services/hello output
geek-web-services/hello output

2nd Output:

geek-web-services/greetoutput
geek-web-services/greetoutput

Project Explanation: It is as justified via video provided below as follows:

Conclusion

As explained above, a spring boot project can be deployable in Tomcat. Do remember certain important points as listed:

  • By default, Spring Boot 1.4.4.RELEASE requires Java 7 and Spring Framework 4.3.6.RELEASE or above
  • Higher versions of Tomcat will be helpful to deploy spring-boot applications.

Next Article
Spring Boot - Project Deployment Using Tomcat

P

priyarajtt
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Java-Spring-Boot
Practice Tags :
  • Java

Similar Reads

    Spring Boot JPA Sample Maven Project With Query Methods
    In this article, let us see a sample maven project in Spring Boot JPA with Query methods. Spring Boot + JPA removes the boilerplate code and it will be enhanced much if we use query methods as well. Let us discuss this project with MySQL Connectivity for geeksforgeeks database and table name as "Con
    6 min read
    Spring Boot - Service Class Example for Displaying Response Codes and Custom Error Codes
    Sometimes the error or status messages in webpages would be different from the default error message thrown by Tomcat (or any other server). An important reason for this is websites want to look unique in their approach to their users. So, a customized page for displaying status codes is always bett
    6 min read
    Spring Boot - Create a Custom Auto-Configuration
    Usually, we might be taking a maven project or grade project for Spring Boot related projects. We will be adding the dependencies in pom.xml (in case of a maven project). In a spring application, Spring Boot auto-configuration helps to automatically configure by checking with the jar dependencies th
    4 min read
    Spring Boot - Consume Message Through Kafka, Save into ElasticSearch, and Plot into Grafana
    In this article, we are going to make a program to produce some data using Kafka Producer which will consume by the Kafka Consumer and save into the elastic search DB, and later on, plot that JSON data into the Grafana dashboard. Start with configuring all the required software and tool. Requirement
    7 min read
    How to Implement AOP in Spring Boot Application?
    AOP(Aspect Oriented Programming) breaks the full program into different smaller units. In numerous situations, we need to log, and audit the details as well as need to pay importance to declarative transactions, security, caching, etc., Let us see the key terminologies of AOP Aspect: It has a set of
    10 min read
    Spring Boot - AOP(Aspect Oriented Programming)
    The Java applications are developed in multiple layers, to increase security, separate business logic, persistence logic, etc. A typical Java application has three layers namely they are Web layer, the Business layer, and the Data layer. Web layer: This layer is used to provide the services to the e
    4 min read
    Spring Boot - Cache Provider
    The Spring Framework provides support for transparently adding caching to an application. The Cache provider gives authorization to programmers to configure cache explicitly in an application. It incorporates various cache providers such as EhCache, Redis, Guava, Caffeine, etc. It keeps frequently a
    6 min read
    Spring Boot - AOP Around Advice
    Aspect-oriented programming(AOP) as the name suggests uses aspects in programming. It can be defined as the breaking of code into different modules, also known as modularisation, where the aspect is the key unit of modularity. Aspects enable the implementation of crosscutting concerns such as transa
    6 min read
    Spring Boot - Auto-configuration
    Spring Boot is heavily attracting developers toward it because of three main features as follows: Auto-configuration - such as checking for the dependencies, the presence of certain classes in the classpath, the existence of a bean, or the activation of some property.An opinionated approach to confi
    5 min read
    Spring Boot - EhCaching
    EhCache is an open-source and Java-based cache. It is used to boost performance. Its current version is 3. EhCache provides the implementation of the JSR-107 cache manager. Features of EhCache are given below: It is fast, lightweight, Flexible, and Scalable.It allows us to perform Serializable and O
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences