• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL, request.getAttribute issue...

 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm getting the following error:
"...setUser(java.lang.String) in foo.AdvisorTagHandler cannot be applied to (java.lang.Object)... " when I try to do "<mine:advice user='<%=request.getAttribute("userName") %>'/>" on my jsp page. But when I did <mine:advice user='<%=(String)request.getAttribute("userName") %>'/>, it works fine. Is this suppose to be the norm?

Thanks so much,
Carmen
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got what is expected. The return type for Attributes is Object and for Parameters is String. For clear understanding look at pg 186 of HFSJ.

Thanks
Chandu
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and definitely yes.

This is one of the most evil but common traps that can be found in the exam.

getAttribute() returns Object
getParameter() returns String

Watch out exam question where, in the middle of the code, you see :



There, even if the question focuses on something else (say, for example, on getSession() boolean attribute), the trap is the whole thing does not compile, because an Object is put in a String variable with no cast.

Beware of this.
 
Carmen Brianick
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your responses Chandra and Frederic! Now this clears things up.

Regards,
Carmen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic