जब मेरा ऑब्जेक्ट HTTP के सत्र ऑब्जेक्ट पर बाध्य/असंबद्ध हो जाता है तो मुझे अधिसूचित कैसे किया जा सकता है।HTTP सत्र में बाध्य/असंबद्ध होने पर अधिसूचना प्राप्त करना
6
A
उत्तर
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
बदलें।
सहायता के लिए धन्यवाद। यही वह है जिसे मैं ढूंढ रहा था :) – ramoh