• 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

Doubt in use bean

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class abstract Person {
String getName()
void setName(String)
}

class Employee extends Person {
int getEmpId()
void setEmpId(int)
}


servlet code:
Person p =new Employee();
p.setName("Evan");
request.setAttribute("person",p);

jsp:
<jsp:UseBean id="person" type="Employee" scope="request" >
<jsp:setProperty name="person" property="name" value="fred" />
</jsp:useBean>
Name is:<jsp:getProperty name="person" property="name" />


In Head first its given that the above code fails at request time .The person attribute is stored at request scope ,so the <jsp:useBean> tag wont work since it specifies only a type.The container knows that if you have only a type specified,there must be an existing attribute of that name and scope.

Iam not clear with the answer .acn nyone clear my doubt?
Can
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As there is no class attribute, no new bean is created, and since no bean exists already, the code will fail at runtime.


The person attribute is stored at request scope ,so the <jsp:useBean> tag wont work since it specifies only a type.The container knows that if you have only a type specified,there must be an existing attribute of that name and scope.


you have also provided the answer above.

Hope this helps
[ July 17, 2008: Message edited by: Amit Ghorpade ]
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for clear understanding, you just remove the scope of the useBean and try
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the fix needed in servlet code to make it work?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what is the fix needed in servlet code to make it work?


None !

The problem is in the JSP, it needs to be fixed.


Hope this helps
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will Changing the Servlet as given below fix the issue?


servlet code:
Employee p =new Employee();
p.setName("Evan");
request.setAttribute("person",p);

Please clarify.
[ July 17, 2008: Message edited by: Shiaber Shaam ]
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I think this code will work, Let me try to expalain this, When you write this:


This is equivalent to following code:
Employerr person = null;
person = (Employee)request.getAttribute("person");
// Here person is already exist in the request, so it should work..!
if(person == null ){
person = new Employee();
//set the properties
//store this object in the request...
}

as person is not null, so this code should work fine.
Please correct me if I am wrong.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,


Both of your code is fine... it will run... i think your not understanding the scope issue...for that please try yourself... you will get it
 
thilakk kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all This helps me a lot
reply
    Bookmark Topic Watch Topic
  • New Topic