-1

I want to achieve a task whenever session.setAttribute and session.getAttribute is called I will catch that event and append key with some unique value.

For eg : session.setAttribute("userId","123") I will catch this event and append session.setAttribute("userId"+Guid,"123")

and for session.getAttribute("userId") will catch this event and append session.getAttribute("userId"+Guid) and return response accordingly.

Is there any way to do this basically I am doing this because I want to use same session for multiple users using same browser tab.

So I will have some unique value in tab specific cookies and will get it in every request and will fetch user data using session.setAttribute("userId"+Uniqueid from Cookie which I have stored) and same for getAttribute.

I don't want to user url rewriting.

pise
  • 849
  • 6
  • 24
  • 51
  • @balusc I saw that example of HttpSessionBindingListener, please help me do I need to configure in web.xml to get a call to valueBound and ValueUbound method. And also when a session.getAttribute() is called even then this method will be called.Because I am not getting a call to this methods. – pise Jun 10 '15 at 08:28
  • @BalusC I have implement HttpSessionBindingListener in myclass, please help me how session object will implement it.How to notify session about implementation of HttpSessionBindingListener – pise Jun 10 '15 at 08:41
  • @BalusC when I do like this session.setAttribute("key",new MyHttpSessionBindingListener()) I get a call but I want that for each and every session.setAttribute and getAttribute I should get a call. For eg session.setAttribute("userid",'123456") and session.getAttribute("userid") I should get a call. – pise Jun 10 '15 at 08:59
  • @BalusC then I have to use HttpSessionAttributeListener ? Am I correct you mean to say this. – pise Jun 10 '15 at 09:03

1 Answers1

0

You can use an HTTPSessionAttributeListener. Check out this link for an example: http://www.mkyong.com/servlet/a-simple-httpsessionattributelistener-example/

Rabiya
  • 1
  • I tried this but if suppose I append in attributeAdded I set session with append value it again call attributeAdded method which keep on going. `@Override public void attributeAdded(HttpSessionBindingEvent event) { String attributeName = event.getName(); Object attributeValue = event.getValue(); System.out.println("Attribute added : " + attributeName + " : " + attributeValue); session.setAttribute(event.getName()+"UniqueValue", event.getValue()); // this will again call attribute Added which keep continuing }` – pise Jun 10 '15 at 08:16
  • You should check if the unique value is already present in the key. If yes, do not append. – ramp Jun 10 '15 at 10:09
  • @ramp. My first part is complete session.setAttribute but one more is pending session.getAttribute how to catch this event, since I have to update unique id in session.getAttribute(userid+"GUID") also to fetch data. – pise Jun 10 '15 at 10:40
  • You should use the HttpSessionAttributeListener as already mentioned and which I suspect you already have done. Your problem is when you add the attribute again with your GUID, the attributeAdded() fires again, correct? Is your GUID a constant value ? In which case, just check if the attribute name already contains the GUID and then do not add it second time. – ramp Jun 10 '15 at 11:01
  • And btw, there is no event for getAttribute(). You should just know the attribute name when you look it up, – ramp Jun 10 '15 at 11:04