How to track session elements with HttpSessionBindingListener

Discussions

Web tier: servlets, JSP, Web frameworks: How to track session elements with HttpSessionBindingListener

  1. Hello,
    I'd like to track values put in the session with HttpSessionBindingListener
    interface. So I have built my class that implements this interface.

    - - - - - - - - - - - - - - - - - - - - - - - -
    package bean;

    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.util.*;

    public class MyListener implements HttpSessionBindingListener
    {
    ServletContext context;
    public MyListener(ServletContext context)
    {
    this.context = context;
    }
    public void valueBound(HttpSessionBindingEvent event)
    {
    System.out.println(" *** [valueBound] *** Someone just bound my listener to a session!");

    }
    public void valueUnbound(HttpSessionBindingEvent event)
    {
    System.out.println(" *** [valueBound] *** Someone just unbound my listener to a session!");
    }
    }


    - - - - - - - - - - - - - - - - - - - - - - - - - -

    When I bind an istance of this class to the session the listener signals a valueBound to the session...

    session.putValue("bindings.listener",new bean.MyListener(getServletContext()));

    *** [valueBound] *** Someone just bound my listener to a session!

    but other elements added to the session aren't tracked....

    session.putValue("key1","value1");
    session.putValue("key2","value2");

    I need to issue again:

    session.putValue("bindings.listener",new bean.MyListener(getServletContext()));

    but this is not what I want...I just want to capture in the listener every value that's added to the session.
    How can I achieve it?
    Thanks
    Francesco
  2. If you want to track each object bound to the session, then each object should implement this interface.

    The other solution is you can get session.getAttributeNames().
    This will give you the names of all objects bound to the session at the time of the call.
    Regards,
    Sneha
  3. Take a look at the follwoing article:
    http://www.onjava.com/pub/a/onjava/2001/04/12/listeners.html

    Nicky