newbie question : HttpSessionBindingListener Interface

Discussion in 'Java' started by pincopallo_it@yahoo.it, Feb 6, 2006.

  1. Guest

    I got a servlet and and I d like to use the interface
    HttpSessionBindingListener to check when session expires and give a
    message to the user
    I did not find examples about how to use it...
    Anyone can help ?
    thats what I do:
    1) public class GUIControllerRead extends HttpServlet
    public class GUIControllerRead extends HttpServlet implements
    HttpSessionBindingListener // I add the interface in this way
    2)I added this code
    HttpSessionBindingEvent firstry= new HttpSessionBindingEvent;

    public void valueUnbound(firstry) {
    System.out.println ("Expired session " );
    }
    public void valueBound(firstry) {
    System.out.println ("Active session " );
    }

    protected void processRequest(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException
    {

    int id_eins;

    session = request.getSession();
    request.setAttribute("guicontroller",
    this.getClass().getName());

    Enumeration en = request.getParameterNames();
    while (en.hasMoreElements()) {
    ...

    Thanks
    Gianni
    , Feb 6, 2006
    #1
    1. Advertising

  2. stefanomnn Guest

    Hi, Gianni, it's simple! you make a class like this

    package mypack;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpSessionBindingEvent;

    public class MySessionListener implements
    javax.servlet.http.HttpSessionListener,
    javax.servlet.http.HttpSessionAttributeListener
    {
    private String name = null;
    private HttpSession session = null;

    public void sessionCreated(HttpSessionEvent event)
    {
    session = event.getSession();
    }

    public void sessionDestroyed(HttpSessionEvent event)
    {
    session = event.getSession();
    /* handle event */
    }

    public void attributeAdded(HttpSessionBindingEvent event)
    {
    name = event.getName();
    session = event.getSession();
    }

    public void attributeRemoved(HttpSessionBindingEvent event)
    {
    name = event.getName();
    session = event.getSession();
    }

    public void attributeReplaced(HttpSessionBindingEvent event)
    {
    name = event.getName();
    session = event.getSession();
    }
    }

    then, in web.xml, add this (before declaring your servlet):

    <listener>
    <listener-class>mypack.MySessionListener</listener-class>
    </listener>

    i hope i helped you!
    stefanomnn, Feb 6, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wildepiet
    Replies:
    0
    Views:
    1,850
    Wildepiet
    Jun 14, 2004
  2. cyberco
    Replies:
    8
    Views:
    480
    cyberco
    Feb 25, 2006
  3. Xah Lee
    Replies:
    15
    Views:
    573
    Sherm Pendley
    Mar 23, 2007
  4. Xah Lee
    Replies:
    15
    Views:
    620
    Sherm Pendley
    Mar 23, 2007
  5. Replies:
    4
    Views:
    336
    timjowers
    Nov 2, 2007
Loading...

Share This Page