2012-06-29 5 views

risposta

7

Consentire alla classe dell'oggetto di implementare HttpSessionBindingListener.

public class YourObject implements HttpSessionBindingListener { 

    @Override 
    public void valueBound(HttpSessionBindingEvent event) { 
     // The current instance has been bound to the HttpSession. 
    } 

    @Override 
    public void valueUnbound(HttpSessionBindingEvent event) { 
     // The current instance has been unbound from the HttpSession. 
    } 

} 

Se si dispone di alcun controllo su codice della classe dell'oggetto e quindi non si può cambiare il suo codice, poi in alternativa è quello di attuare HttpSessionAttributeListener.

@WebListener 
public class YourObjectSessionAttributeListener implements HttpSessionAttributeListener { 

    @Override 
    public void attributeAdded(HttpSessionBindingEvent event) { 
     if (event.getValue() instanceof YourObject) { 
      // An instance of YourObject has been bound to the session. 
     } 
    } 

    @Override 
    public void attributeRemoved(HttpSessionBindingEvent event) { 
     if (event.getValue() instanceof YourObject) { 
      // An instance of YourObject has been unbound from the session. 
     } 
    } 

    @Override 
    public void attributeReplaced(HttpSessionBindingEvent event) { 
     if (event.getValue() instanceof YourObject) { 
      // An instance of YourObject has been replaced in the session. 
     } 
    } 

} 

Nota: quando si è ancora in Servlet 2.5 o più anziani, sostituire @WebListener da una voce di configurazione <listener> in web.xml.

+0

grazie per l'aiuto. Questo è quello che stavo cercando :) – ramoh

Problemi correlati