2012-06-29 14 views

Odpowiedz

7

Niech klasa obiektu implementuje 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. 
    } 

} 

Jeśli nie masz kontroli nad kod klasy obiektu, a tym samym nie można zmienić swój kod, a alternatywą jest wdrożenie 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. 
     } 
    } 

} 

Uwaga: gdy jesteś jeszcze w Servlet 2.5 lub starszej, wymienić @WebListener przez wejściem <listener> konfiguracyjnego w web.xml.

+0

dzięki za pomoc. Tego właśnie szukałem :) – ramoh

Powiązane problemy