Home > General > Sessions in Wicket

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
   }
//...
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • TwitThis

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, Facebook and iPhone 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"

craig 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!

  1. No trackbacks yet.