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 ?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With