2

I'm creating application using Spring MVC and Hibernate where people can register and store some data in their accounts. I created registration and login processes. And now i have a question, how can I keep user session? I don't want to keep user id in each page of my application. For example: once the user has logged he arrives at page

someurl/overview/1

Where id is user's id. I would like it to looks like this for each logged user:

someurl/overview/

What are the best practices how to save user data in session using Spring and save it if user closed site and then opened it?

Thanks in advance.

user3127896
  • 6,323
  • 15
  • 39
  • 65
  • possible duplicate of [How to use Session attributes in Spring-mvc](http://stackoverflow.com/questions/18791645/how-to-use-session-attributes-in-spring-mvc) – dabadaba Jul 13 '14 at 13:46
  • dabadaba, thank you, i was looking for duplicates but for some reason didn't see this one – user3127896 Jul 13 '14 at 13:48

1 Answers1

4

You can save user data in session by a number of ways.. simplest would be:

public ModelAndView yourMethod(HttpSession session){

    session.setAttribute("user", 1); 
    return new ModelAndView("home");

}

you will be available with session all the time if you are using spring components, so just add this parameter and use it.

EDIT: can also use Spring's session attributes: http://fruzenshtein.com/spring-mvc-session/ if it satisfies your requirements.

EDIT(Thanks to @Serge Ballesta for putting it in comments, Just updating the post FYI)

even if API does not enforce it, specification requires that all session attributes be Serializable. Think of that if storing more than an id.

nsthethunderbolt
  • 2,059
  • 1
  • 17
  • 24
  • How can i get this session attribute in the another Controller? – user3127896 Jul 13 '14 at 13:46
  • if you are using that controller is annotated with controller then just add HttpSession in the parameters, you will have it. No extra work. – nsthethunderbolt Jul 13 '14 at 13:47
  • Spring maintains the session itself, and provides you when you need it within the scope of it's components. – nsthethunderbolt Jul 13 '14 at 13:48
  • i'm thinking in another controller i will not be able get session attribute without creating new object of HttpSession class. – user3127896 Jul 13 '14 at 13:49
  • You will get it. that's why we use this framework, you do not have to the overhead of repeated tasks like requests, response and sessions. Just give it a try.. It will work. :) – nsthethunderbolt Jul 13 '14 at 13:51
  • 1
    BEWARE : even if API does not enforce it, specification requires that all session attributes be `Serializable`. Think of that if storing more than an id. – Serge Ballesta Jul 13 '14 at 13:55
  • nsthethunderbolt, i got nullpointer exception when i get session attribute from another cotroller. All that i've done in another controller is declared session like this: private HttpSession session; – user3127896 Jul 13 '14 at 14:02
  • No, you don't have to declare it anywhere, just add it as arguments to the function, in which you are using the session like mentioned above, you do not need to pass it from anywhere, it is readily available for use. – nsthethunderbolt Jul 13 '14 at 14:04
  • One thing could be to create your own custom object. Say it is Context. Then, you can set the scope for the bean as Session. https://stackoverflow.com/a/29414676/631965 – Olgun Kaya Sep 27 '18 at 13:17