Session management is one of the essential parts for each web application. Since Spring MVC is a powerful framework for web development, it has its own tools and API for the interaction with sessions. Today I intend to show you the basic ways of session processing within Spring MVC application. That is: how to process forms, add objects into a session, and display objects from the session on JSP. I will try my best, so let’s start.

This Spring MVC Session tutorial will be based on one of the previous posts on my blog, related to the form handling. I’m going to extend the application by adding some session logic to the existing student-form, and create a new page with a form and a single text field on it. The text from the field will be processed by a controller and added to the session.
In order to check the session functionality I will display the session objects on the pages using JSTL. You can download the source in the end of the tutorial.

Form with a single text field

Firstly I need to create a view and a controller. I will start from the view creation and after that I’ll demonstrate the corresponding controller with the session logic.


Adding of a String into the session

To remember:
Main page ...

Now I need to develop the controller to handle the form. There will be two methods for the requests handling: the first one is responsible for navigation to the page, the second one is related to the session activity.

public class SingleFieldController {

	public ModelAndView singleFieldPage() {
		return new ModelAndView("single-field-page");
	public ModelAndView rememberThought(@RequestParam String thoughtParam) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("thought", thoughtParam);
		return modelAndView;

This is a simple Spring MVC controller with the one extra @SessionAttributes annotation. It indicates that in the controller’s methods some values can be assigned to the arguments of the annotation. In this example I have declared just one session attribute with the name “thought“. That means I can put some object into modelAndView using the addObject() method, and it will be added to the session if the name of the object will be the same as the name of the argument in @SessionAttributes.

The last thing I should to do is to add a link to the new page on the index.jsp:


Home page

This is Home page.

Don't forget: ${thought}

Person page
Single field page


In order, to check whether that session works properly, you need to add the following code to the existing views (single-field-page.jsp, …):

Don't forget: ${thought}

On the screenshots below you can see the result of the code execution:

Spring MVC session processing

And the results:

Spring MVC session result JSTL


Spring MVC session result JSTL 1

Adding a custom object into the session

In this section I’m going to show you how to add a custom object into the session, and how to display еру object’s properties on JSP. The role of the custom object will play the Person object. Firstly I’ll modify the existing person controller:

public class PersonController {

	public ModelAndView personPage() {
		return new ModelAndView("person-page", "person-entity", new Person());
	public ModelAndView processPerson(@ModelAttribute Person person) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("pers", person);
		modelAndView.addObject("personObj", person);
		return modelAndView;

Comparing with the latest version I have added two new strings:

modelAndView.addObject("personObj", person);

The result of the code execution is following:

Spring MVC session object


Spring MVC session object result

This is the end of the Spring MVC session tutorial. And as I promised earlier, I give a link to the sources of the project. Everything I have mentioned in the post constitutes just a small part of the things, you should know about the sessions, later I’ll write a post about their different important features.

About The Author

Mathematician, programmer, wrestler, last action hero... Java / Scala architect, trainer, entrepreneur, author of this blog

  • Kevin

    Nice work! Can you add those ‘different important features’?

    • Fruzenshtein

      Yes, sure
      I’ll publish the article in nearest time
      I hope in 30 days =)

  • Jon

    Thanks. Post more info.
    I guess there is more than one way to handle sessions in Spring MVC. I would like to see all session handling stuff like Login / Logout, checking sessions to make sure the session is valid, etc.

    • Fruzenshtein

      Ok, I understood what you mean
      In the future posts I’ll write about Spring Security (Login / Logout). And session management

  • localhost

    在这里的我完全看不出来session的作用,不加@SessionAttributes这个注释,也完全可以实现提交,进入Controller,最后跳转到展示页面的的功能啊。 不能理解这里加Session标示符的作用? ——from china

    • Rooney Wayne


      • khdaskdfgag

        gdkhsadgasdgkggfgasfk##&&$$$$ gfdhvfhjvdfhvdsjfvdjhk “session”

  • hjuhjlljkljghjgl

  • jgjhdhj

  • srtyrturtu

    • fgjurfyuytugrtruytuytuytu

      • yjhjjkghkghkgkghkghkhkhgk

        • gggggggggggggggggggggggg

          • khdaskdfgag

            nice explaination

          • khdaskdfgag


            dhfvdsfvjhaghksdhg fdsfgddsgjsdkjjhdfhbsdlskjhfbsdf

  • Eder Weiß


  • Yoni

    просто шик.