Enterprise Java

Spring Boot & JSP

This guide shows how to use Spring Boot to create MVC web application with JSP.


  • Eclipse IDE (neon release)
  • Maven 4
  • Java 1.8

1. Create maven web project

Open eclipse then create a new maven web project and name it as SpringBootWebJsp.

The structure of the generated projects look like the following:

2. pom.xml

After creating the web project, the first step is to configure Spring Boot inside pom.xml, so we add the following as a parent dependency:


Spring Boot exposes a starter called spring-boot-starter-web which automatically imports all the required jars needed to setup a typical Spring MVC application and automatically configures the view resolver and the servlet dispatcher of the application so that the developer focuses on the development rather than the configuration, so we add the starter as a dependency:


Since we’re using JSP as the front end technology, we need to add the following dependency in order to be able to compile JSP and make use of its features:


That’s all, just 2 dependencies can make your MVC application up. Below are the auto-imported jars:

This is the whole pom.xml for reference:

<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/maven-v4_0_0.xsd">

P.S: when using JSP as a front end technology, you should make sure to set the “packaging” attribute as war not jar, since Spring Boot team claimed that currently there is limitations for supporting JSP inside jar file ( the view resolver wouldn’t map correctly).

3. Application.java

The second step is to create the Spring Boot initializer class, this is the entry point of our application. When annotating a class with @SpringBootApplication, we’re configuring our application to run over the embedded servlet container provided by Spring Boot (tomcat by default).


package com.programmer.gate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class Application{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

4. application.properties

Spring Boot auto configures the view resolver and the dispatcher servlet of the web application and provides us a way to define our own configuration using application.properties.

So we create application.properties under src/main/resources and define the following properties:

home.message= Programmer Gate

Here we’re configuring the view resolver to map the views to files of type jsp under “WEB-INF/jsp/”. We also change the default port of the embedded tomcat to be 9093, as well as defining other business message properties to be used later on inside our application.

5. home.jsp

In this tutorial, we’re creating a very simple MVC application which displays a welcome message to the end user, so we create a very basic jsp file called home.jsp under WEB-INF/jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <h1>Welcome to ${message} </h1> 

6. HomeController.java

Now we create our main controller named HomeController under com.programmer.gate and we define it to serve requests coming from the root path as the following:

package com.programmer.gate;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
public class HomeController {
    private String message;
    public String welcome(Map<String, Object> model) {
        model.put("message", this.message);
        return "/home";

Our controller simply reads the welcome message from application.properties and then redirects to home.jsp.

7. Deploy the application

Following are the steps to deploy our application:

  • Right click pom.xml -> run-as -> Maven install
  • Maven generates a war file called SpringBootWebJSP-0.0.1-SNAPSHOT.war inside target folder
  • Open cmd, then run the war using: java -jar SpringBootWebJSP-0.0.1-SNAPSHOT.war

Here we go, our application is up and ready to serve requests at port 9093.

That’s it, i hope you like it. For clarifications please leave your thoughts in the comments section below.

Published on Java Code Geeks with permission by Hussein Terek, partner at our JCG program. See the original article here: Spring Boot + JSP

Opinions expressed by Java Code Geeks contributors are their own.

Hussein Terek

Hussein is a senior software engineer with 5 years of experience in software design, development and integration. He is the author and founder of Programmer Gate (www.programmergate.com) blog.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
6 years ago

Hello Hussein. Thank you for interesting material. Just one question – is it possible to run such application unpacked like exploded war? Usually it is very convenient to be able to run it exploded for have fast development cycles. How do you run such projects from your IDE? Can you show sample run configuration? Thank you!

Back to top button