Sessions in Wicket
October 13th, 2008
Found out a valuable lesson about session management in Wicket today. Wicket tries to be as stateless as long as possible, I believe it takes some hints from how your pages are built to know if it needs to keep a Session around for longer than a Request.
So, if you find that newSession(Request request, Response response) is being called in your WebApplication class for each and every request (not somethng you want happening if you're trying to keep state in the session between requests!) what you have to do is use the bind() method for the current session.
Using bind() can be done within the constructor of your custom Session class itself, or if you want more control over when Sessions persist, you can call bind selectively (i.e. only in the constructor of pages which use the Session to store/retrieve information intended to live between sessions).
e.g.
public class MyPage extends WebPage {
//...
public MyPage(String id) {
getSession().bind();
//setup wicket components
}
//...
}



Thank you. Thank you, very much. I was stuck the whole day ripping-off all my code. This helped.
- Nishant
Thank you.
I tried to utilize this but I did not find a case for it. In 1.4, even with stateless pages, I could not see that getSession().bind(); was required anywhere. Could you show us a testcase that demonstrates where this code is advantageous?
@Bernard it’s possible that in newer incantations of Wicket things work differently, if I have time I’ll check to see!
Thanks, that saved my day
I’m using Wicket 1.5RC7 and it works the same, for some reason the session wouldn’t persist between redirects.
Great! Glad I could help
Have to say thank you! Had this problem for few hours now and finally I finally I found the right place.
As you point, wicket keeps some rules to persist sessions but don’t do it always. This is the way to persist it
Thank you very much for this. I’ve just learned about wicket this 2 weeks, already bought the book ‘wicket in action’, then create my first project and 4 hours of my life are wasted because of this “stateless session” thing. I’m so lucky to notice in the eclipse debugger that the session object always changes, then search the problem with the right keywords.
Once again, thanks man.