Open In App

Deployment of Spring MVC Application on a Local Tomcat Server

Last Updated : 05 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Spring MVC is a Web Framework under the group of projects by Spring Team using Java EE technologies. It is an open source and fairly used to create robust and dependable web applications with Java Programming Language. Spring MVC is designed across the Model-View-Controller (MVC) Architecture. To run the Spring MVC web application, we want to install it on our local Tomcat Server. In this article, we will discuss the methods to installation our Spring MVC application on our local Apache Tomcat server.

Prerequisite

  • Java
  • Maven & Eclipse IDE
  • Local Apache Tomcat

Project Structure:

project_structure

Creating Spring MVC Application

Create a basic Spring MVC project to deploy it in our Local Tomcat server.

Step 1: Create a basic Maven starter project in Eclipse by going to New -> Maven Project and select web-app archetype.

Step 2: In the pom.xml file add the dependencies for Spring MVC and Maven War Plugin.

XML




    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>springmvcdeploy</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springmvcdeploy Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.1.1</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>springmvcdeploy</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
            </plugin>
        </plugins>
    </build>
</project>


Note: maven-war-plugin is used to generate war build file using maven command line tool.

Step 3: Open the web.xml under the /main/webapp/WEB-INF/ folder and configure the DispatcherServlet to handle every incoming request.

XML




<!-- web.xml -->
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>


  • In Spring MVC, each request may be served through the DispatcherServlet so configure the DispatcherServlet to intercept every request to the “/” root context direction.
  • Create a servlet with the <servlet> tag, map the magnificence DispatcherServlet magnificence to it the usage of <servlet-class> child tag, and also offer a name to it the use of <servlet-name> tag for servlet-mapping.
  • Map “/” URL- pattern to the DispatcherServlet the usage of its servlet-name, right here dispatcher is used as servlet-name.

Step 4: Configure the servlet created above “dispatcher” using dispatcher-servlet.xml file under /main/webapp/WEB-INF/ folder.

XML




<!-- dispatcher-servlet.xml -->
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="  
  
    <mvc:annotation-driven />
    <context:component-scan
        base-package="com.example"></context:component-scan>
</beans>


  • A dispatcher-servlet.Xml is created to configure the servlet and upload an annotation-driven tag to use the annotation-primarily based configuration in the servlet.
  • Configure the context’s component-scan base package for scanning for controllers and configurations.

Step 5: Create a simple response file under “/main/webapp/WEB-INF/” named hello.jsp. This jsp will be returned by our Spring Controller.

HTML




<!-- hello.jsp -->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>
        Hello there your Spring MVC is working!!! 
    </h1>
</body>
</html>


Step 6: Create a controller under “/main/java” directory inside a basic package called com.example.

HelloWorld.java:

Java




package com.example;
  
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
  
@Controller
public class HelloWorld {
    @GetMapping("/hello")
    public String hello() {
        return "/WEB-INF/hello.jsp";
    }
}


  • Create a Spring controller by annotating the class with @Controller annotation.
  • A Get request is created at /hello and mapped to the hello() method using GetMapping annotation.
  • Return the view hello.jsp along with its path starting from WEB-INF folder.

Creating War for Spring MVC

To deploy the Spring MVC application on our local Tomcat server, we need to build the project in war format. War stands for Web Application Archive which is similar to Jar where the entire resources necessary for the application is self-contained in the archive like the JAR files, Java servlets, Java classes, XML files, static resources.

  • Open a new Terminal and go to the project directory.
  • Use the Maven war plugin to generate the War file for the project.
mvn clean install

Below is the terminal where maven is being installed.

Maven Installed

  • This will remove the target folder and generate the fresh war file for the project.
  • We can also use Eclipse IDE feature to directly create the war, right click on the Project and Go to Run as -> Maven Install.
  • This will generate project_name.war in your target folder under your project.

war_generated_target

Deploying Spring MVC Application

There are two different ways to easily deploy the Spring MVC app to our Local Tomcat server.

Approach 1:

  • Open the target folder and copy the generated war file.
  • Go to the tomcat installation folder and paste it into the “/webapp/” folder.
  • After that just start the tomcat server using bin/startup.bat or bin/startup.sh based on your system.
  • On starting the server, it will automatically take the war file and deploy it in the webapp folder.
  • To view the output, open the browser and go to http://localhost:8080/context_path/hello,
    • where context_path is the project name.

Deploy Using Copying War

Approach 2:

  • Start your tomcat server and login to the Tomcat manager GUI by opening http://localhost in your browser.
  • In the manager app, at the bottom Deploy area, select the file and click on the deploy button.
  • Verify whether the app is deployed by visiting the context_path of the app and the hello.jsp page.

Deploy using Manager GUI



Similar Reads

Spring Boot - Project Deployment Using Tomcat
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 foc
5 min read
Difference Between Spring Boot Starter Web and Spring Boot Starter Tomcat
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 of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
3 min read
Spring vs Spring Boot vs Spring MVC
Are you ready to dive into the exciting world of Java development? Whether you're a seasoned pro or just starting out, this article is your gateway to mastering the top frameworks and technologies in Java development. We'll explore the Spring framework, known for its versatility and lightweight nature, making it perfect for enterprise-level softwar
8 min read
Configuring a Tomcat Connection Pool in Spring Boot
The Spring Boot is a powerful layer of abstraction placed on the Spring platform. It makes developing standalone and production-ready web applications easy. Spring Boot provides a few starter dependencies for handling the application. In this article, we discuss configuring a Tomcat Connection Pool in Spring Boot. We can use any database for this p
3 min read
Create and Run Your First Spring MVC Controller in Eclipse/Spring Tool Suite
Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. The model object can be passed between view and controller using maps. In this article, we will see how to set up a Spring
5 min read
Difference Between Spring MVC and Spring WebFlux
Spring MVCSpring MVC Framework takes on the Model-View-Controller design pattern, which moves around the Dispatcher Servlet, also called the Front Controller. With the help of annotations like @Controller and @RequestMapping, the by-default handler becomes a robust(strong) tool with a diverse set of handling ways. This kind of dynamic kind of featu
5 min read
Spring MVC vs Spring Web Flux: Top Differences
For creating Java-based projects and applications, developers usually have to choose between Spring MVC and Spring WebFlux when using the Spring framework to create web apps. To pick the best framework for their project, developers need to know how each one is different and what features they have. Each framework has its own benefits compared to th
11 min read
Configuration of Apache Tomcat Server with Eclipse IDE
In this article, we will discuss a step by step guide to setup Apache Tomcat server in Eclipse IDE. Eclipse IDE: Eclipse is an open-source Integrated Development Environment that is popular for Java application development (Java SE and Java EE) and Android apps. It also supports C/C++, PHP, Python, Perl, and other web project developments via exten
2 min read
Creating JSP in Eclipse IDE with Tomcat Server
Eclipse IDE is an open-source Integrated Development Environment that is popular for Java application development (Java SE and Java EE) and Android apps. It also supports C/C++, PHP, Python, Perl, and other web project developments via extensible plug-ins. JSP stands for java server pages. It is a server side technology been used for creating web a
3 min read
Setup Apache Tomcat Server in IntelliJ IDE for Java J2EE Development Projects
In this article, we will discuss a step-by-step guide to set up Apache Tomcat server in IntelliJ IDE. IntelliJ IDEA is an intelligent, context-aware IDE for working with Java and other JVM languages like Kotlin, Scala, and Groovy on all sorts of applications and other web project development. For this IDE we need to add the server externally so tha
3 min read