• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem Accessing Objects and EL in JSP

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying out EL and on my servlet I wrote



question 1:
on my response.jsp i used:
Street is : ${pojo.street]}
but the street didnt get printed any idea why the output was empty like : Street is:

question 2:
to access the value of Att in setattribute i used
Value is : ${Att} //this okay
but suppose if my request and session bot contained the same name "Att" which one would be called ??
Is there a way i can be more specific request,session doesnt seem to work in EL ...
any help would be appreciated..

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I see nowhere where you create a scoped variable named pojo.

2a) request scope is search before session scope.

2b) Look up the list of EL built-in scoped variables. Your answer lies there.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:1) I see nowhere where you create a scoped variable named pojo.

2a) request scope is search before session scope.

2b) Look up the list of EL built-in scoped variables. Your answer lies there.



I thought when using EL you do not need to create an object unlike when using a scriptlet such as



I would appreciate it if you can give me an example on how this would be accomplished using EL ...

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object is created in the servlet controller, placed in request (or other) scope via setAttribute() and then referenced in the JSP.

You seem to have most of it down, but you must refer to the scoped variable in the JSP via the same name that you assigned to it when placing it in scope. You never created a scoped variable named pojo, so of course you cannot access one in the JSP.

The problem might be more apparent if you'd used better variable names than "obj".
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:The object is created in the servlet controller, placed in request (or other) scope via setAttribute() and then referenced in the JSP.

You seem to have most of it down, but you must refer to the scoped variable in the JSP via the same name that you assigned to it when placing it in scope. You never created a scoped variable named pojo, so of course you cannot access one in the JSP.

The problem might be more apparent if you'd used better variable names than "obj".


what was i thinking... my bad



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But please use better names than obj!
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking up a little more on EL and i read that

for map value

obj.name
=
obj["name"]

so i also wanted to convert
${requestScope.obj.getAddress().getStreet()}


to its corresponding bracket form i tried doing
${requestScope.obj["getAddress"]["getStreet"]} ==> this doesnt work ?? Am i writing it correctly ??


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Zedan wrote:
for map value

obj.name
=
obj["name"]

It's not just for maps. The bracket notation is the general form. The dot notation is a convenience.

so i also wanted to convert ... to its corresponding bracket form i tried doing

Why? If it's just an exercise, fine. Otherwise, the dot form should be used for readability unless the bracket form must be used.

${requestScope.obj["getAddress"]["getStreet"]} ==> this doesnt work ?? Am i writing it correctly ??

No.

Just because you are trying to use the bracket form, why did you forget the correct way to reference bean properties? E.g. would you use ${obj.getStreet}?
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Why? If it's just an exercise, fine. Otherwise, the dot form should be used for readability unless the bracket form must be used.
Just because you are trying to use the bracket form, why did you forget the correct way to reference bean properties? E.g. would you use ${obj.getStreet}


Yeah i am just using it for practise i am going to stick to the convenience operator.. But i was just practising..
BTW what am i doing wrong here

${requestScope.obj["getAddress"]["getStreet"]}

I am telling it to goto object obj then call getAddress and then call the getStreet function but i dont think this is correct..
any hints... or suggestions I think there is something wrong with the parameter
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat:
Bear Bibeault wrote:Just because you are trying to use the bracket form, why did you forget the correct way to reference bean properties? E.g. would you use ${obj.getStreet}?
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:I repeat:
Bear Bibeault wrote:Just because you are trying to use the bracket form, why did you forget the correct way to reference bean properties? E.g. would you use ${obj.getStreet}?


Idont think i am using obj.getStreet since i am using
{requestScope.obj["getAddress"]["getStreet"]}
so i think it was suppose to be translated to
obj.getaddress().getstreet()
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you not answering my question? How would you write it in dot notation?
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Just because you are trying to use the bracket form, why did you forget the correct way to reference bean properties? E.g. would you use ${obj.getStreet}?


Nop i wont use ${obj.getStreet} instead i would use as mentioned above ${obj.getAddress().getStreet()}.

It looks as if i am messing up in the syntax.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know that if we had a property in the obj object we could access it by the following method
${obj.property} or its equivalent ${obj["property"]}

but suppose i wanted to access a method in object i know using the convenience operator in EL i would go like
${obj.method()} ===> this would work

but i dont know how to call a method using the bracket operator
${obj["method()"]} ===>doesnt work
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where all the "get" and parens coming from? Neither is part of EL syntax.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Where all the "get" and parens coming from? Neither is part of EL syntax.


Thanks for the suggestion i finally figured it out (thanks to your clues)
First of all i was'nt aware that EL has nothing like get and () etc..
so i reread head first servl. p344 and realized that EL basically appends the "get" and capitalizes our methods so after making these changes to the methods
and then using
Finally : ${obj["address"]["street"]}
I got it to work
now the methods will be called as getAddress and getStreet
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see that it works... But i''ll definitely stick to the convenience operator "."..
Did that just in case..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The times when the bracket notation comes in most handy:

  • Referencing a Map key that isn't an identifier: ${myMap['some key that has non-identifier characters']}
  • The property name is in a variable: ${myBean[somePropertyName]}

  • You can't use the dot notation in either of these situations.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic