I’m using the Scalatra framework to build a web application.
The application relies on sessions, but I can’t use session cookies (because technically there is only one user, which runs multiple sessions simultaneously).
Each session has a unique session key which I want to use as an identifier. I want this key to be sent as a GET or POST parameter instead of a cookie header.
My question now is: How can I store session information (i.e. a state) in a Scalatra servlet without cookies but just a parameter as identifier?
So far I tried to use the file system to store all session information, but this is too slow and unnecessary because the sessions only last a few seconds.
(Security is not an issue)
I figured out how I can do it.
In every Scalatra servlet, I have access to the global
servletContextwhich implements thejavax.servlet.ServletContextinterface. I can use its two methodssetAttribute(x: String, y: Any)andgetAttribute(x : String)to store information about my sessions, where x is my unique identifier and y is the session information encoded as a case classSession.Effectively I have the following:
This way I can keep a state on the server, without using cookies, only a single unique identifier that the client has to provide as a GET value.
I guess this technique can be applied to any servlet in Java and Scala which provides an instance of
ServletContext, not just Scalatra.