• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Bean related exercise question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DOUBT ON BEAN RELATED STANDARD ACTION EXERCISE(page 416 0f HFSJ)
Given:
<jsp:useBean id="person" type="foo.employee">
<jsp:setProperty name="person" property="name" value="Fred" ?>
</jsp:useBean>
Name is:<jsp:getProperty name="person" property="name" ?>
..............................................................................
[Here class Employee extends an abstract class Person]

and the related servlets are:

(1) foo.Person p=new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);


second servlet

(2) foo.Employee p=foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);

The answers given are:

(1) fails at request time. the person attribute is stored at request scope.

(2)
and the second one works fine and prints out "evan".

But i find them contradictory answers. Since, both attributes are request scope. Both should fail.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You are right, I tried the code with different combinations.

The following combination gives error @ runtime in both cases:
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You are right, I tried the code with different combinations.

The following combination gives error @ runtime in both cases:
JSP code:
<jsp:useBean id="person" type="com.enrolment.Employee" >
<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>
Name is:<jsp:getProperty name="person" property="name" />

Case 1: Servlet code:
Person p=new Employee();
p.setName("Evan");
req.setAttribute("person",p);

Result: Runtime error[bean person not found within scope ]

case 2: Servlet code
Employee p=new Employee();
p.setName("Evan");
req.setAttribute("person",p);

Result: Runtime error[bean person not found within scope ]

However if you add scope="request" in the useBean action like:
<jsp:useBean id="person" type="com.enrolment.Employee" scope="request">

Then the result is : Name is:Evan in both the cases.

Please correct me if I am wrong.

Thanks and regards,
Saurabh
 
Rajendra Nath
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi saurabh...
that's what i needed. our answers are correct.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check Errata

http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic