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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads