You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Ernesto Garcia <Eg...@genesyslab.co.uk> on 2005/11/03 15:59:30 UTC

Inner class session attributes not supported?

I define a static inner class inside my JSP, I create an instance of it and then I keep the resulting object as a session attribute. When getting the attribute again I will get a ClassCastException when downcasting to my inner class. Is this a bug or standard behaviour?

Thanks,
Ernesto GarcĂ­a
 
<html>
  <%!
  private static class MyInnerClass {
    private int number;
    public MyInnerClass(int number) { this.number = number; }
    public void increment() { this.number++; }
    public String toString() { return Integer.toString(number); }
  }
  %>
 
  <%
  Object instanceObj = session.getAttribute("instance");
  if (instanceObj == null)
    session.setAttribute("instance", new MyInnerClass(1000));
  else {    
    // Uncomment the following line to get ClassCastException:
    //MyInnerClass instance = (MyInnerClass)instanceObj;
    //instance.increment();
  %>
    <p>instanceObj.getClass() = <%= instanceObj.getClass() %>
    <p>instanceObj = <%= instanceObj %>
  <%
  }
  %>
</html>
 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Inner class session attributes not supported?

Posted by GB Developer <gb...@globallyboundless.com>.
> -----Original Message-----
> From: Ernesto Garcia [mailto:Egarcia@genesyslab.co.uk] 
> Sent: Thursday, November 03, 2005 9:00 AM
> To: users@tomcat.apache.org
> Subject: Inner class session attributes not supported?
> 
> 
> I define a static inner class inside my JSP

... and then put it in the session. So even if you *could* cast it back out
to the right type (which in my own testing, I haven't managed to do)... you
now have a situation where any class can now access your private inner class
of another class? (Because any other JSP page can call
session.getAttribute).  If that's the usage you want, then why bother making
it a "private inner class" in the first place? 
 
Also, the classname for inner classes has the $ in it, and for JSP pages,
has the name of the jsp page prepended as well.  In my own test, I put a
page at the root of the webapp named test.jsp and the classname of the inner
class was:

instanceObj.getClass() = class org.apache.jsp.test_jsp$MyInnerClass 
 
none of these work:
(org.apache.jsp.test_jsp$MyInnerClass)foo
(test_jsp$MyInnerClass)foo
(test_jsp.MyInnerClass)foo
 
In fact, the nice little "perchance you meant" in the stack trace tells me
someone has a sense of humour about these sorts of situations.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org