6

I'm working on a fairly simple web app using JSPs and a Java servlet, running on tomcat. I've been able to set attributes in the session from the servlet in order to pass information to the JSPs which then gets presented to the user. I've done this with several different objects of different classes and it worked fine. Suddenly though when I set a particular kind of object (containing configuration information), the attribute isn't showing up in the JSP at all. The other attributes that I set are still there, but the configuration object is totally missing. I printed off the list of attribute names and the name I was using isn't even there (though the other names for the other attributes I set are present).

What could cause this? There's nothing different or weird about my configuration class. I'd really appreciate any ideas on what sorts of things might cause this behavior. I've googled and searched and can't find anything.

ETA: If it matters the name of the attribute is "configuration". I couldn't find anything on that being a reserved word or anything... I set this attribute in the servlet in the same function as a few others such as "user". I then redirect to a JSP which attempts to get both user and configuration. So everything's going on at the same time. user is fine while configuration doesn't even appear in the list of attribute names.

ETA2: Here's the exception that keeps occurring in the logs:

java.lang.Exception
    at pms.SessionListener.printStackTrace(Unknown Source)
    at pms.SessionListener.attributeAdded(Unknown Source)
    at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1498)
    at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1390)
    at org.apache.catalina.session.StandardSessionFacade.setAttribute(StandardSessionFacade.java:154)
    at PMS.getTaskInfo(Unknown Source)
    at PMS.doGet(Unknown Source)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:282)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1687)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4
  • 1
    I've answered a similar question yesterday. You may find it useful. stackoverflow.com/questions/5495361/…
    – BalusC
    Commented Mar 31, 2011 at 20:30
  • Thanks but that isn't the issue at all. The session is absolutely there, and I can retrieve other attributes that were set in the servlet at the same time from the session in the JSP. It's just this one particular attribute that won't show up. I found lots of stuff on the session not being there, but not why one attribute won't work when others do.
    – Maltiriel
    Commented Mar 31, 2011 at 20:37
  • I see. Well, I posted an answer then :)
    – BalusC
    Commented Mar 31, 2011 at 20:43
  • Please implement either the listener that BalusC or myself recommended and let use know the results.
    – hooknc
    Commented Mar 31, 2011 at 21:32

2 Answers 2

6

As per the comments on the question:

The session is absolutely there, and I can retrieve other attributes that were set in the servlet at the same time from the session in the JSP. It's just this one particular attribute that won't show up. I found lots of stuff on the session not being there, but not why one attribute won't work when others do.

Then something has removed or nulled-out the attribute.

session.removeAttribute("name");

or

session.setAttribute("name", null);

or even in JSP

<c:set var="name" value="${null}" scope="session" />

Or it can be yourself who has set null instead of the fullworthy object.

To naildown the one and other better, I'd let the attribute implement HttpSessionBindingListener and dump the stack on valueUnbound().

public class Foo implements HttpSessionBindingListener {

    @Override 
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println("Value bound"); // Logger?
    }

    @Override 
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.err.println("Value unbound"); // Logger?
        Thread.dumpStack();
    }

    // ...
}
4
  • I'm definitely not setting it to null anywhere (as I said this is a very simple app; there's not enough code for me to have not noticed that). I just tried setting the attribute to the string "testing" and it still doesn't show up in the list of attribute names.
    – Maltiriel
    Commented Mar 31, 2011 at 21:27
  • Aren't you assigning the HttpSession as an application wide instance variable and using a browser on a different session to test it? You should be setting it by request.getSession().setAttribute("foo", "bar") and you should be accessing it by ${foo} or, more explicitly, ${sessionScope.foo} in JSP or just request.getSession().getAttribute("foo") in servlet.
    – BalusC
    Commented Mar 31, 2011 at 21:40
  • I'm using HttpSession session = request.getSession(true); session.setAttribute("configuration", "testing"); then a bit later session.setAttribute("user", user) and in the JSP session.getAttribute("configuration") and session.getAttribute("user")
    – Maltiriel
    Commented Mar 31, 2011 at 21:51
  • Just let the attribute implement HttpSessionBindingListener and dump the stack on valueUnbound(), exactly as suggested in my answer. In the stack you should be able to see the one responsible for removing the attribute. Or just run the IDE's debugger instead of this "poor man's way" of debugging, if you are already familiar with the IDE's debugger.
    – BalusC
    Commented Mar 31, 2011 at 23:53
5

There are many possibilities on what could be going wrong.

  • There could be two different sessions.
  • There could be some code removing your configuration object from the session.
  • Others?

Here is a way you can test to see what exactly is going on.

There are two separate listener interfaces that you can implement to listen for specific session events: javax.servlet.http.HttpSessionListener and javax.servlet.http.HttpSessionAttributeListener

I would implement these two interfaces with a class that will log what is happening during each event and which session the event is coming from.

You should be able to easily add your listeners to your web.xml file so that they will actually be set as listeners on your sessions by tomcat.

EDIT

Here is a class you can place into your web.xml as a listener to your sessions. Both BalusC and myself have recommended that you try this approach to debug the issue. Please just humor us and let us know if you see anything interesting in the way your "configuration" attribute is being set?

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener, HttpSessionBindingListener, HttpSessionActivationListener {

public void valueBound(HttpSessionBindingEvent event) {
    System.out.println("valueBound: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printStackTrace();
}

public void valueUnbound(HttpSessionBindingEvent event) {
    System.out.println("valueUnbound: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printStackTrace();
}

public void attributeAdded(HttpSessionBindingEvent event) {
    System.out.println("attributeAdded: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printStackTrace();
}

public void attributeRemoved(HttpSessionBindingEvent event) {
    System.out.println("attributeRemoved: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printStackTrace();
}

public void attributeReplaced(HttpSessionBindingEvent event) {
    System.out.println("attributeReplaced: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printStackTrace();
}

public void sessionCreated(HttpSessionEvent event) {
    System.out.println("sessionCreated: " + event.getSession().getId());
    this.printStackTrace();
}

public void sessionDestroyed(HttpSessionEvent event) {
    System.out.println("sessionDestroyed: " + event.getSession().getId());
    this.printStackTrace();
}

public void sessionDidActivate(HttpSessionEvent event) {
    System.out.println("sessionDidActivate: " + event.getSession().getId());
    this.printStackTrace();
}

@Override
public void sessionWillPassivate(HttpSessionEvent event) {
    System.out.println("sessionWillPassivate: " + event.getSession().getId());
    this.printStackTrace();
}

private void printStackTrace() {
    try {
        if (true) {
            throw new Exception();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

Please add the above class to your code then add the following to your web.xml file between your filter mappings and your servlet mappings:

<listener>
    <listener-class><your.package.name>SessionListener</listener-class>
</listener>
5
  • Can you explain what you mean by two different sessions? I'm not sure that's possible in this case. In the Java servlet, I set several different attributes in the session all at the same time (same call to the servlet). Then in a JSP I read these different attributes back from the session, again all at the same time, yet one of them isn't set. Is it possible for there to be two different sessions in that case?
    – Maltiriel
    Commented Mar 31, 2011 at 21:29
  • Updated my answer to give you the exact code that you will need to debug what is going on. Most likely no one will be able to give you a reasonable answer until you see what is happening to your session with the above code. Your output will be quite verbose, but you should be able to do searches for "configuration" and the session id that is being used when you first add your "configuration".
    – hooknc
    Commented Mar 31, 2011 at 22:37
  • Thanks for providing the exact code. I was actually going to implement the listeners myself, I swear, I just had to step away from my computer for a bit. Anyway I'm seeing the configuration attribute getting removed, but I can't tell how or why. The session is remaining the same throughout. There are a bunch of exceptions in the log, I assume that's not normal?
    – Maltiriel
    Commented Mar 31, 2011 at 23:41
  • Thanks, using the code you provided I finally figured out where things were going wrong. I was indeed overwriting with something null, though I still don't know why the object in question is null..
    – Maltiriel
    Commented Apr 1, 2011 at 16:26
  • That happens (the null object that is...). Having tools at your disposal like these listeners are really great. Best of luck.
    – hooknc
    Commented Apr 4, 2011 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.