Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store session in Spring MVC

What's the best way of storing session related data of a user (like, for example a log of recent actions a user has done) in a Spring MVC (2.5) web application ?

Using the classic javax.servlet.http.HttpSession or by specifying scope="session" in controller beans, and storing the data in a session object ?

like image 378
dakull Avatar asked Sep 02 '09 13:09

dakull


People also ask

Where session is stored in Spring MVC?

You can save/retrieve it to/from session object if you pass HttpSession into your controller. Since you have a heavy object, you can store them in a NoSQL like db or use some caching mechanism if your app needs to support many number of concurrent sessions/users.

How session is maintained in Spring MVC?

If the controller is a singleton (which it is by default) and you store the session in a private variable it'll be available to all requests for all users. You want to access a specific users session for the scope of a request i.e. for the scope of a single call to a controller method.


1 Answers

Session-scoped beans (using scope="session") is the cleanest approach. This removes the need to interact with the session yourself.

If you want to autowire a session-scoped bean in to the controller, you either need to make the controller session-scoped itself, or use a scoped-proxy to wire it into a singleton controller, as described here. Either approach is valid.

like image 129
skaffman Avatar answered Sep 23 '22 23:09

skaffman