I figured out how I can do this.
In every Scalatra servlet, I have access to a global servletContextone that implements the interface javax.servlet.ServletContext. Can I use two methods setAttribute(x: String, y: Any)and getAttribute(x : String)to store information about my sessions, where x - my unique ID and y - session information encoded as a case class Session.
In fact, I have the following:
def storeSession(key : String, session : Session) {
servletContext.setAttribute(attributePrefix + key, session)
}
def loadSession(key : String) : Session = {
val session = servletContext.getAttribute(attributePrefix + key)
if (session != null) {
session match {
case s : Session => s
case _ => null
}
} else {
null
}
}
Thus, I can save the state on the server without using cookies, only one unique identifier, which the client must provide as a GET value.
I assume this method can be applied to any servlet in Java and Scala that provides an instance servletContext, not just Scalatra.
source
share