Spring Boot Web with JSP

Tags: JSP Spring Boot JSP

In this Spring Boot web tutorial we learn step by step how to setup a Spring Boot project with JavaServer Pages (JSP) and Tomcat embedded.

Table of contents

  1. Create New Spring Boot Web Project
  2. Add Dependencies to the Spring Boot Web Project
  3. Configure View Resolver
  4. Add Controller Class and JSP View File
  5. Complete Application Source Code
  6. Download Source Code

Create New Spring Boot Web Project

Open IntelliJ IDEA, select the menu File > New > Project. (or click on New Project button at IntelliJ Welcome dialog)

On the New Project dialog, select Spring Initializr and click Next button.

Spring Boot Web with JSP

On the Spring Initializr Project Settings dialog input the new project information as below and click Next button.

Notes: to use JSP view you need to choose War packaging for the project.

  • Group: dev.simplesolution
  • Artifact: spring-boot-web-jsp
  • Type: Gradle
  • Language: Java
  • Packaging: War
  • Version: 1.0.0
  • Name: spring-boot-web-jsp
  • Description: Spring Boot Wev JSP
  • Package: dev.simplesolution

Spring Boot Web with JSP

On the Dependencies dialog, select below dependencies and click Next button.

  • Spring Web

Spring Boot Web with JSP

Select the location for your project and click Finish button to create new Spring Boot project.

Spring Boot Web with JSP

Add Dependencies to the Spring Boot Web Project

To use the JSP view in Spring Boot project we need to add Tomcat Embed Jasper and JSTL dependencies as below.

If you use Gradle build project, add the following dependencies to the build.gradle file.

providedRuntime group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper'
implementation group: 'javax.servlet', name: 'jstl' , version: '1.2'

If you use Maven build project, add the following dependencies to the pom.xml file.

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Configure View Resolver

At this step we configure Spring MVC view prefix and Spring MVC view suffix for JSP views in application.properties file as below.

src/main/resources/application.properties

spring.mvc.view.prefix:/WEB-INF/JSP/
spring.mvc.view.suffix:.jsp

Add Controller Class and JSP View File

At this step we implement a simple Controller class to handle the request and render view using JSP view.

Add new Java package named dev.simplesolution.controller and create a new Controller class named HomeController as following Java code.

src/main/java/dev/simplesolution/controller/HomeController.java

package dev.simplesolution.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("message", "Welcome to Simple Solution home page!");
        return "index";
    }
}

Create new directories webapp/WEB-INF/JSP/ under src/main directory and add a new JSP view file named index.jsp as below.

src/main/webapp/WEB-INF/JSP/index.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
    <head>
        <title>Spring Boot Web with JSP - Simple Solution</title>
    </head>
    <body>
        <h3>${message}</h3>
    </body>
</html>

Complete Application Source Code

At this step we have finished implement a simple Spring Boot web application which using JSP view, the source code structure of the project as following screenshot.

Spring Boot Web with JSP

Run the above Spring Boot application and open the URL http://localhost:8080/ on your browser, we have a web page as below.

Spring Boot Web with JSP

Download Complete Source Code

The source code in this article can be found at: github.com/simplesolutiondev/spring-boot-web-jsp

or clone at:

git clone https://github.com/simplesolutiondev/spring-boot-web-jsp.git

or download at:

Download Source Code

Happy Coding 😊

Spring Boot Console Application using CommandLineRunner

Spring Boot Web Get Client IP Address

Spring Boot Get Base URL