Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

from HF page 356, Be the Container..

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,
Please go through the following code from HF Book Page 356,
BE the Container

We have an abstract class called Person,
whose methods are String getName()
and void setname(String)
We have an Employee class which extends Person class
and has methods int getEmpID() and void setEmpID(int)
and both the classes are in package foo.

and jsp code is as follows:
<jsp:useBean id ="person" type ="foo.Employee">
<jsp:setProperty name ="person" property ="name" value ="Fred" />
</jsp:useBean>
Names is: <jsp:getProperty name ="person" property ="name" />

In the Servlet If we have the code
1.
foo.person p = new foo.Employee();
p.setName("Evan");
request.setAttribute("Person",p);

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

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

Figure out what the JSP code above would do for each of the three different Servlet code Examples.

The answers given are
1.
fails at request time
2.
fails to compile
3.
works fine and prints Evan.

My doubt is in 1. "person" is being added to the request and in 3."person" is added to the request
and in jsp we did not specify the scope so it will take default scope that is of page.
so why 1. is not working and 3. is working


and another doubt is

what is the difference between request in request.setAttribute and and request in
<jsp:useBean id ="person" type ="foo.Employee" scope ="request">
Are they both same?

Can any one explain in detail

Thanks in advance.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The tag <jsp:useBean id ="person" type ="foo.Employee"> looks for a bean which is of type foo.Employee.

If you examine this code, you are setting the bean type foo.person (p) in request and NOT foo.Employee. OOPS says Employee is a person but person is NOT an Employee.

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


<jsp:useBean id ="person" type ="foo.Employee" scope ="request">
looks for bean of type foo.Employee in request alone.So if you have a bean set in request with key as "person" the above tag retrieves the same bean.

Hope it helps,

Thanks,
Vinod
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Even the 3rd option given in the book doesnt work,

It is an error, it will fail for the same reason as the first option fails.

Whenever you use just the type the bean will not be instantiated .
if there is no bean in the mentioned scope then the container will throw an exception.
Sami
 
Vinod Kumar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The 3rd option will work because the bean is in request. If you use type attribute in the useBean tag then that type should be in a scope.JSP servlet searches in all the available scopes for the bean from least restricted to most restricted scope, which is true in our case. If the bean is not in scope then only it will throw an exception.

Thanks,
Vinod
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am also having a problem with this Question.
1,2 & 3 are not working for me, but the Book says 3rd one is correct. Can someone clearly explain this please?


Thanks,
Nithya
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm also in a doubt with this exercise (page 356 exercise 3).
If I dont specify the scope in jsp tag, the container creates a new instance of bean and executes <jsp:useBean> body.
But, if I specify the scope request at jsp tag, the container found the bean created by my servlet and show the existing value of person's property.

So please, tell me if I am wrong or it is a book mistake.

Thanks,

Rodrigo
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was an error in the book. You can find that and others in http://www.oreilly.com/catalog/headservletsjsp/errata/

http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed
 
Rodrigo W Bonatto
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply Zhing.

Rodrigo
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! Solved my pbm also.
 
I once met a man from Nantucket. He had a tiny ad
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic