Spring MVC Session Tutorial
Session management is one of essential parts for each web application. Since Spring MVC is a powerfull framework for a web development, it has own tools and API for the interaction with sessions. Today I intend to show you basic ways of session processing within Spring MVC application. That’s mean a processing of forms, adding objects into a session, displaying of 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 a session logic to the existing student-form, create a new one page with a form and a single text field on it. The text from the field will be processed by a some controller
and added to the session. In order to check the session functionality I will display the session objects on the pages using JSTL. The src from the tutorial you can download in the end of tutorial.
Form with the single text field
Firstly I need to create a view and the controller. I will start from the view creation and after that I’ll demonstrate the corresponding controller with the session logic.
... <h2>Adding of a String into the session</h2> <form action="remember.html" method="post"> <table> <tbody><tr> <td>To remember:</td> <td><input name="thoughtParam" type="text"></td> </tr> <tr> <td><input type="submit"></td> <td></td> </tr> </tbody></table> </form> <a href="${pageContext.request.contextPath}/">Main page</a> ...
Now I need to develop the controller to handle the form. There will be two methods for the requests handling: the first on is responsible for navigation to the page, the second one is related to session activity.
@Controller @SessionAttributes("thought") public class SingleFieldController { @RequestMapping(value="/single-field") public ModelAndView singleFieldPage() { return new ModelAndView("single-field-page"); } @RequestMapping(value="/remember") public ModelAndView rememberThought(@RequestParam String thoughtParam) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("thought", thoughtParam); modelAndView.setViewName("single-field-page"); return modelAndView; } }
This is a simple Spring MVC controller with the one extra @SessionAttributes annotation. It indicates that in the controller’s methods can be assigned some values to arguments of the annotation. In this example I declared just one session attribute with the name “thought“. That’s mean I can put some object into modelAndView using addObject() method, and it will be added to the session if the name of the object will be the same as the name of argument in @SessionAttributes. The last thinng what I should to do is to add a link to the new page on the index.jsp:
... <h1>Home page</h1> <p>This is Home page.</p> <p>Don't forget: ${thought}</p> <p> <a href="person-form.html">Person page</a> <br> <a href="single-field.html">Single field page</a> </p> ...
In order, to check that session works properly, you need to add following code in the existing views (single-field-page.jsp, …):
<p>Don't forget: ${thought}</p>
On the screenshots below you can see the result of the code work:
And the results:
And
Adding of 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 custom object will play Person object. Firstly I’ll modify the existing person controller:
@Controller @SessionAttributes("personObj") public class PersonController { @RequestMapping(value="/person-form") public ModelAndView personPage() { return new ModelAndView("person-page", "person-entity", new Person()); } @RequestMapping(value="/process-person") public ModelAndView processPerson(@ModelAttribute Person person) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("person-result-page"); modelAndView.addObject("pers", person); modelAndView.addObject("personObj", person); return modelAndView; } }
Comparing with latest version I added two new strings:
... @SessionAttributes("personObj") ... modelAndView.addObject("personObj", person); ...
The result of the code execution is following:
And
This is the end of Spring MVC session tutorial. And as I promised earlier, I give a link to the sources of project. Everything I mentioned in the post is just a part of things, which you should know about the sessions, later I’ll write a post about different important features.
I could be wrong here, but you can also do this with model.
So my question is what are the pro and cons for using sessions or model.
Or in which situation would you use session and in which would you use model.
As you see from screenshots, values which I put into session are available on the every page of the application. If I put such value just in model it will be available only in respective page in a scope of the particular response
In order to understand this tutorial I recommend to download the source code and launch it or develop it by steps specified in the article
Thanks for the comment
Very helpfull post. Thank you very much. Could you please continue acticle series on advansed session handling (how to delete session attributes, how to invalidate session, how to invalidate session when user leaves page).
Hello, I have problem for killing session attributes. How can I destroy it when I don’t need that session attributes?
Thanks
if you want to kill session attribute then try to use session.removeAttribute(“parameter name”);
Can i create as many as session attibutes as possible,because i want multiple session attributes i.e..
I want multiple values to be available in the complete session.
how can i remove particular custom object from the session
Hi, Thanks for this tutorial but you didn’t include how to get the data from a session in the controller not in the view thanks.
Hi, Thanks for this tutorial but you didn’t include how to get the data from a session in the controller not in the view thanks.
i am creating a project based on booking a cab so iwant to handle multiple user using sessions how can i do it usiing spring framework
In view you are not reading the values from session , its from another Person Object added into the View through ModelAndView , that is not a clear implementation of Spring MVC session.
Scroll down to the section labeled “Adding of a custom object into the session”