3

I googled and stackoverflowed a lot but cant get this to work. Here is my code. I set a session attribute "topic" in subscribe method but in sessionDestroyed I get it as null. This question on SO seems relevant to mine but doesn't solve the issue.

@Path("/jpubsub/{topic}")
    public class JMSSubscribe implements HttpSessionListener, ServletContextListener, MessageListener, ExceptionListener {
    
        @GET
        public subscribe(@javax.ws.rs.core.Context HttpServletRequest request) {
            HttpSession session = request.getSession();
            session.setAttribute("topic", "1");
        }
    
        @Override
        public void sessionCreated(HttpSessionEvent hse) {
            HttpSession session = hse.getSession();
                System.out.println("Created Session - ID : " + session.getId());
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent hse) {
                System.out.println("Destroyed Session - ID : " + hse.getSession().getId());
                System.out.println("Topic ID sessionDestroyed - " + hse.getSession().getAttribute("topic"));
        }

Please help.

PS: When I set an attribute in sessionCreated(), I get it in sessionDestroyed(). Is it because I am using diferent session objects? Also, when I print session ID. I get the same session ID in all 3 methods.

Please ask if any other piece of code is required.

Community
  • 1
  • 1
aBhijit
  • 5,261
  • 10
  • 36
  • 56

2 Answers2

0

After sessionDestroyed() is called, all the objects in the session are already cleared. Hence, you get null value. Instead, you should implement HttpSessionBindingListener interface.

And do no use raw String object to store in session, instead, create a simple object that implements above interface. You will get the value when it is unbound (removed) from session. Assuming no one else removed it, it will only be called before session is actually destroyed.

Bimalesh Jha
  • 1,464
  • 9
  • 17
  • When I call session.setAttribute("topic", "1"); in subscribe(), valueBound() method is not getting called – aBhijit Sep 27 '13 at 10:55
  • Because "1" is not the object which implements `HttpSessionBindingListener`. Refer my answer, you have to create a custom object which implements this interface. You have to set this custom object in Session. – Bimalesh Jha Sep 27 '13 at 10:58
  • By "object which implements HttpSessionBindingListener interface" do you mean any object that is created in the class which implements HttpSessionBindingListener? Can you please give an example? – aBhijit Sep 27 '13 at 11:23
  • Just google... one such example is http://jamonapi.sourceforge.net/httpsession_sample.html – Bimalesh Jha Sep 27 '13 at 11:54
  • 2
    @Bimlesh - 'After sessionDestroyed() is called, all the objects in the session are already cleared.' --> This is not true. When I set the attribute in sessionCreated method, I get it in sessionDestroyed. – aBhijit Oct 03 '13 at 12:35
  • Refer your original question. Yourself said the value you get is null in sessionDestroyed(). This is expected. But, now if you are getting non null value pls check your code. Null value means attribute is not in session scope. – Bimalesh Jha Oct 04 '13 at 02:08
  • 1
    @BimaleshJha: The `sessionDestroyed()` method is called just `before` the session is invalidated not after. – me_digvijay Oct 27 '13 at 08:12
  • 1
    In tomcat 5.5 embedded in jboss 4.0, accessing session objects returns null in sessionDestroyed(). I think it is based on servlet 2.3 specs. I am unsure if something has changed later. – Bimalesh Jha Oct 29 '13 at 04:24
-1

set the session attribute in session created method

@Path("/jpubsub/{topic}")
public class JMSSubscribe implements HttpSessionListener, ServletContextListener, MessageListener, ExceptionListener {

    @GET
    public subscribe(@javax.ws.rs.core.Context HttpServletRequest request) {
        HttpSession session = request.getSession();
    }

    @Override
    public void sessionCreated(HttpSessionEvent hse) {
        HttpSession session = hse.getSession();
        session.setAttribute("topic", "1");

            System.out.println("Created Session - ID : " + session.getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent hse) {
            System.out.println("Destroyed Session - ID : " + hse.getSession().getId());
            System.out.println("Topic ID sessionDestroyed - " + hse.getSession().getAttribute("topic"));
    }
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • 1
    I dont have data available when sessionCreated is called. Check my edit. – aBhijit Sep 27 '13 at 09:15
  • "1" is actually a dynamic data to be stored which is available only in subscribe method. – aBhijit Sep 27 '13 at 09:15
  • This is exactly what the OP has already said doesn't work. There are no attributes of a destroyed session. -1 – user207421 Sep 30 '13 at 10:11
  • @EJP session attributes will be available just before the session destoyed is called. you can test it.create a session attribute.try to print it in session destroyed method.then call session.invalidate().You will 100% get session attribute value.or else i will give you code with screenshot – SpringLearner Sep 30 '13 at 10:16
  • 1
    'Just before' isn't much use. They aren't available *when* it is called. This is what the question is *about.* You don't appear to have even read it. – user207421 Sep 30 '13 at 10:23
  • @EJP this question was not as it is now.It wa edited one hour before and i answered on 2 sep as per the question – SpringLearner Sep 30 '13 at 10:25
  • Nevertheless your answer contradicts the question as it now is. Have you really tested this code? – user207421 Sep 30 '13 at 10:38
  • @EJP What can be done if user changes question later.Suppose you answer a question today and after 1 year user changes changes it.You are not going to check all the questions that you have answered whether it is edited or not.As i said you before you can get attribute values just before calling session.invalidate() which is mainly used for logout types – SpringLearner Sep 30 '13 at 10:42
  • Your answer is incorrect. There are no attributes of a destroyed session, regardless of what question the OP was or is now asking. – user207421 Sep 30 '13 at 22:10
  • I said, if I set the attribute in sessionCreated method, I get it in sessionDestroyed, if you read the question carefully. @EJP – aBhijit Oct 03 '13 at 12:27
  • @aBhijit I can see a delete vote in my answer,if you do not find this answer useful then i can delete it – SpringLearner Oct 03 '13 at 12:29
  • @javaBeginner- Do not delete the answer. Though it doesn't solve the issue. It was a genuine attempt to solve it. It gives other people idea of "What can't be done to solve the issue...". – aBhijit Oct 03 '13 at 12:34