• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cast problem

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshana,
What does ServletRequest getAttribute returns ?
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's very simple.. Think!
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic