Home > General > Sessions in Wicket

Sessions in Wicket

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
   } 
//...
}

About the Author

Craig Tataryn started his career as a Visual Basic programmer, but don't hold that against him! The system he helped create was a cutting edge, n-tiered revenue collection system whose transactions per second metric was off the charts! Around the year 2000 he discovered Struts and never looked back. A professional Java developer for close to a decade, Craig has worked on a wide variety of projects ranging from Recruiting, Purchase order, Revenue collection and Mortgage systems. More recently Craig has written web services for children's educational toys and has honed his skills on Wicket, SOA and iOS application development. "I love to learn and more importantly I love to teach. Hoarding knowledge is one of the most petty and selfish things you can do, especially as a contractor. This is why I always make it a point to document and share my knowledge with my client's employees"

General

  1. March 9th, 2010 at 12:48 | #1

    Thank you. Thank you, very much. I was stuck the whole day ripping-off all my code. This helped.

    - Nishant

  2. Bernard
    April 30th, 2010 at 22:07 | #2

    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?

  3. May 1st, 2010 at 23:23 | #3

    @Bernard it’s possible that in newer incantations of Wicket things work differently, if I have time I’ll check to see!

  4. Piotr
    September 20th, 2011 at 07:18 | #4

    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.

  5. September 20th, 2011 at 09:11 | #5

    Great! Glad I could help

  6. November 18th, 2011 at 10:09 | #6

    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

  7. December 25th, 2011 at 09:27 | #7

    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.

  1. No trackbacks yet.
Support Wikipedia