• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cast problem

 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why we need a cast here?

<%Person p=(Person)request.getAttribute("person"); %> // return the Person class know..so do we need a cast?
<%= p.getName() %>


assume Person is a javabean class and and we set Person class in the servlet as attribute name "person"
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Harshana,
What does ServletRequest getAttribute returns ?
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very simple.. Think!
 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balu Sadhasivam wrote:
Harshana,
What does ServletRequest getAttribute returns ?


Spec says,
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

So it means if we set an object as a attribute we need to cast because return type is Object.

Do we still need to cast as (String) if we set a attribute as a string?

Thank you guys.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do we still need to cast as (String) if we set a attribute as a string?

If you remember your SCJP studies (you passed it, didn't you ? it's a prerequisite for SCWCD), you should know that you have to cast in any case. The method signature is Object getAttribute(String name). If you want to retrieve a String, you cast it to String. If you want to retrieve a Cowboy, you cast it to Cowboy.
 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
Do we still need to cast as (String) if we set a attribute as a string?

If you remember your SCJP studies (you passed it, didn't you ? it's a prerequisite for SCWCD), you should know that you have to cast in any case. The method signature is Object getAttribute(String name). If you want to retrieve a String, you cast it to String. If you want to retrieve a Cowboy, you cast it to Cowboy.


thankx christopher..i do remebering those when some one remeberd me so. i ve done scjp 1.4 in 1.5 year back. any way new SE concepts are lot more changed isnt it..i have recently go through some 1.6 notes for an interview..and figured like Collection Framework stuff seems much tuffer than previos versions
 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im really sorry guys i have accendtly post same thing..by the way cant we delete our post mesege..i couldnt fine it
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any way new SE concepts are lot more changed isnt it.

Make sure you know about some 1.5 concepts like Generics and enhanced for loops, because I've heard some ranchers saying that they've seen some at their exam.

by the way cant we delete our post mesege..

No. I've deleted them.
 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Make sure you know about some 1.5 concepts like Generics and enhanced for loops, because I've heard some ranchers saying that they've seen some at their exam.


oh really Chris..ive didnt thought of it...thankx for remeber it..:-) seems hav to go through them too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic