2012-06-29 18 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