1
public class Foo extends Properties {
    public String getVal(){
         return "2";
    }
}

In my HttpServlet class' doGet(..) method I am doing this,

Foo foo = new Foo();
request.setAttribute("key", foo);

Then in the .jsp is this code,

1  ${key}
2  ${key.val}
3  <%=request.getAttribute("key")%>
4  <%=((Foo)request.getAttribute("key")).getVal()%>

And this is the output,

1  {}
2  
3  {}
4  2

Could anyone tell my why ${key.val} doesn't work?

[Edit] I am only interested on the one property from the foo class, since it seems there is no way to access the getVar() call using EL(Right?), would a viable alternative be to jsut put?

request.setAttribute("key_val", foo.getVal());

Foo is a sub class of a subclass of the Properties class so there is no way for me to decouple them easily.

Andrew
  • 13,757
  • 13
  • 66
  • 84

2 Answers2

2

Maybe you have EL disabled? Check these two answers for possible reasons and solutions:

Another possibility is that you have key bound to something different in your other contexts. This code:

${key}

Is equivalent to:

<%= page.findAttribute("key") %>

and not <%= request.getAttribute("key") %>. It searches for in pageContext first, then request, session and application context at last.

Community
  • 1
  • 1
Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
  • What would be the correct syntax for getting foo from the request object then? – Andrew Sep 17 '10 at 20:44
  • If you only want to use object from request scope, you need to use `${request.key}` or `${request['key']}`. But typically, `${key}` is what you want. I just wrote that answer as a possibility. – Peter Štibraný Sep 17 '10 at 20:47
  • Both ${request.key} and ${request['key']} return nothing when I use them. When you say ${key} is what I want, do you mean that my syntax should be correct? – Andrew Sep 17 '10 at 20:57
  • @Andrew: yes, your syntax looks OK. I'm sorry, it should have been ${requestScope.key} or ${requestScope["key"]}. – Peter Štibraný Sep 17 '10 at 20:59
1

Your Foo class has a toString() method which prints like {} and the val probably called the wrong getter. Add some debug lines/breakpoints to the getter calls. If in vain, post more detail of how the Foo class look like.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This would indeed explain the behaviour. – Peter Štibraný Sep 17 '10 at 20:49
  • 1
    If Foo implements Map, ${key.val} could be translated to Foo.get("val") too. Alternatively, JavaBeans info may define different method for "val" property, although I've never seen that in practice. – Peter Štibraný Sep 17 '10 at 20:52
  • I overrode the toString() method and added a break point to the getter, The toString() method is indeed what produces the "{}", however the getVal method is only called for the call on line 4. – Andrew Sep 17 '10 at 20:53
  • Is it a `public` method? Really, we need more detail about the `Foo` class. – BalusC Sep 17 '10 at 20:56
  • It extends Properties, but the getVal() method was implemented by me, it is public, and it is able to return the right value, you can see that in line 4 of my example. – Andrew Sep 17 '10 at 21:02
  • 2
    `Properties` in turn implements `Map`. Its `get()` method is preferred over this in EL. That's why you're seeing this behaviour. I'd suggest to have `Foo` compose instead of inherit it. – BalusC Sep 17 '10 at 21:04