Spring Boot + Session Management Hello World Example | JavaInUse

Spring Boot + Session Management Hello World Example

In this post we will be implementing Session Management using Spring Boot.
First let us have a look at what is session management and how can it be accomplished.

What is Session Management?

We all know that HTTP is a stateless protocol. All requests and responses are independent. The server cannot distinguish between new visitors and returning visitors. But sometimes we may need to keep track of client's activity across multiple requests. This is achieved using Session Management. It is a mechanism used by the Web container to store session information for a particular user.
Session management can be achieved in one of the following ways-
  • Cookies
  • Hidden form field
  • URL Rewriting
  • HttpSession
In this example we will be making use of HttpSession to achieve Session management. Also we will be using the Spring Session module
Spring Session consists of the following modules:
  • Spring Session Core - provides core Spring Session functionalities and APIs
  • Spring Session Data Redis - provides SessionRepository and ReactiveSessionRepository implementation backed by Redis and configuration support
  • Spring Session JDBC - provides SessionRepository implementation backed by a relational database and configuration support
  • Spring Session Hazelcast - provides SessionRepository implementation backed by Hazelcast and configuration support
In this post we will be using jdbc to store spring session information.

Lets Begin-

Maven Project will be as follows-

In the Maven we need the Spring Session dependency.Maven will be as follows-
<?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">



		<relativePath /> <!-- lookup parent from repository -->




Create the SpringBootHelloWorldApplication.java as below-
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootSessionApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootSessionApplication.class, args);
Create the Employee model class as follows-
package com.javainuse.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

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

public class SpringSessionController {

	public String process(Model model, HttpSession session) {
		List<String> messages = (List<String>) session.getAttribute("MY_SESSION_MESSAGES");

		if (messages == null) {
			messages = new ArrayList<>();
		model.addAttribute("sessionMessages", messages);

		return "index";

	public String persistMessage(@RequestParam("msg") String msg, HttpServletRequest request) {
		List<String> messages = (List<String>) request.getSession().getAttribute("MY_SESSION_MESSAGES");
		if (messages == null) {
			messages = new ArrayList<>();
			request.getSession().setAttribute("MY_SESSION_MESSAGES", messages);
		request.getSession().setAttribute("MY_SESSION_MESSAGES", messages);
		return "redirect:/";

	public String destroySession(HttpServletRequest request) {
		return "redirect:/";

The application.properties will be as follows-



The index.html will be as follows-
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<title>Spring Boot Session Example</title>
		<form th:action="@{/persistMessage}" method="post">
			<textarea name="msg" cols="40" rows="2"></textarea>
			<br> <input type="submit" value="Save Message" />
		<ul th:each="message : ">
			<li th:text="">msg</li>
		<form th:action="@{/destroy}" method="post">
			<input type="submit" value="Destroy Session" />
Compile and the run the SpringBootHelloWorldApplication.java as a Java application.
Go to localhost:8080
We can add the items to be stored in the session. The Spring session created 2 tables for storing session related information.
The spring_session table is as follows-
The spring_session_attributes table is as follows-

Download Source Code

Download it -
Spring Boot + Session Management

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions