• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in use bean

 
thilakk kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for clear understanding, you just remove the scope of the useBean and try
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the fix needed in servlet code to make it work?
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for all This helps me a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic