Session management is an essential part of web applications. It allows the server to store and retrieve user-specific data between requests. HttpSession is a built-in mechanism provided by the Java Servlet API to manage sessions. Spring Boot makes it easy to use HttpSession in your web applications. In this blog post, we will demonstrate how to use HttpSession in Spring Boot to manage user sessions.

Creating a Spring Boot Project

To get started, we need to create a new Spring Boot project. Open your preferred IDE and create a new Spring Boot project using the Spring Initializr. Add the following dependencies to your project:

Once you have created the project and added the required dependencies, create a new controller class called “HomeController” that will handle the HTTP requests.

Storing Data in HttpSession

The HttpSession object represents a user-specific session and provides methods to store and retrieve data. In our example, we will create a basic Spring Boot web application that stores and retrieves data from the HttpSession.

In the HomeController, create a method called save() that retrieves the value of the “name” parameter from the HTTP request and stores it in the HttpSession using the setAttribute() method. It also adds a “message” attribute to the model to display a success message.

@PostMapping("/save")
public String save(HttpServletRequest request, HttpSession session, Model model) {
    String name = request.getParameter("name");
    session.setAttribute("name", name);
    model.addAttribute("message", "Data saved successfully");
    return "home";
}

Retrieving Data from HttpSession

Create another method called retrieve() that retrieves the “name” attribute from the HttpSession using the getAttribute() method and adds it to the model to display it on the home template.

@GetMapping("/retrieve")
public String retrieve(HttpSession session, Model model) {
    String name = (String) session.getAttribute("name");
    model.addAttribute("name", name);
    return "home";
}

Removing Data from HttpSession

Create another method called remove() that removes the “name” attribute from the HttpSession using the removeAttribute() method and adds a “message” attribute to the model to display a success message.

@GetMapping("/remove")
public String remove(HttpSession session, Model model) {
    session.removeAttribute("name");
    model.addAttribute("message", "Data removed successfully");
    return "home";
}

Creating a Thymeleaf Template

Create a new template called “home.html” in the “resources/templates” directory. Add the following HTML code to the template:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HttpSession Example</title>
</head>
<body>
    <h1>HttpSession Example</h1>
    
    <form action="/save" method="post">
        <label for="name">Enter your name:</label>
        <input type="text" id="name" name="name">
        <button type="submit">Save</button>
    </form>
    
    <br>
    
    <h2>Stored data:</h2>
    <p th:text="${message}"></p>
    <p th:text="'Name: ' + ${name}"></p>
    
    <br>
    
    <a href="/retrieve">Retrieve data</a>
    <br>
    <a href="/remove">Remove data</a>
</body>
</html>

In the above template, we have created a form that allows the user to enter their name and submit it to the server. We have also added two links that allow the user to retrieve and remove the stored data.

Testing the Application

Now we can run our application and test it in a web browser. Navigate to http://localhost:8080 and enter your name in the input field and click the “Save” button. You should see a success message below the input field.

Now, click the “Retrieve data” link, and you should see your name displayed on the page. Finally, click the “Remove data” link, and the stored data should be removed from the session, and a success message should be displayed.

HttpSession - Spring Boot Session Management

Conclusion

In this blog post, we have demonstrated how to use HttpSession in Spring Boot to manage user sessions. We created a basic web application that stores and retrieves data from the HttpSession. We also added an example of removing an attribute from the HttpSession. HttpSession is a powerful tool for managing user sessions in web applications, and Spring Boot makes it easy to use.