2012-06-29 2 views

답변

7

개체의 클래스를 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. 
    } 

} 

는 다음 대안은 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. 
     } 
    } 

} 

참고 : 서블릿 2.5 또는 그 이상에 아직있을 때, web.xml에서 <listener> 구성 항목에 의해 @WebListener를 교체합니다.

+0

도움을 주셔서 감사합니다. 이것은 내가 찾고있는 것입니다. :) – ramoh

관련 문제