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

Scriptless JSPs: Type without class

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is w.r.t pg 354 and pg 356 in HFSJ. I am not sure how the JSP code on on pg 356 works as the type is "foo.Employee". Will it work?

On page 354 ,it is said that if the container discovers that bean doesnot exist and it sees only a type attribute without a class, then it will never work. So how the code on pg 356 works?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are talking about the code within "BE the container" section right ?

If yes, than the code within the body of <jsp:useBean> gets executed only when container does not find any attribute existed in the request scope with the name of "person". But, as you can see that in the given two servlet code sample, they have already set an attribute named "person" at the request scope so, there is no question of executing a body of <jsp:useBean> tag.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes true.....my question was w.r.t "BE the container". I got the point that
<jsp:useBean> body will never execute when only a type is declared in the tag.

Bur my question is also related to pg 354, where there is one question
given in the book. Question given in book is:

Q) In your example,"foo.Person"...............AND the object type..

It is said there if the type is "foo.Employee", it will never work ..

What i understand is that since container discovers that bean doesnot exist and only type attribute exists, then it wont work at first place, forget about the body of <jsp:useBean> getting executed....

Also as you said, if i already have "person" set in my servlet code,but still if my JSP is

JSP
<jsp:useBean id="Person" type="foo.Employee" scope="Page"/>

then i guess the corresponding servlet code in _jspService() method will be,

foo.Employee person=null;
synchronized(request)
{
person=(foo.Employee).....

.....
}
Will above thing work properly without any errors
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maan

No it still won't work. See page 357. type == reference type and class == object type.

So,

then i guess the corresponding servlet code in _jspService() method will be,

foo.Employee person=null;
synchronized(request)
{
person=(foo.Employee).....

.....
}


will not be a correct assumption. And instead, it should be:
(foo.Employee person = new (???);

Since we have no idea what the (???) is supposed to be, then it will fail. But if we were to have class="foo.Employee" instead, then the code will become:

(foo.Employee person = new Employee();

and we get ourselves an Employee Object. Then it will work.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry felix ..iam not getting it....

See, as per your explanation, even the JSP code on page 356, i.e
"BE the container" must give the same error as told by you.

On Page 356, they have mentioned:

<jsp:useBean id="Person" type="foo.Employee">
<jsp:set.......
</jsp:useBean>

So as per your explanation Felix, even the above code should not work as here too, the object type is not mentioned as there is no "class" tag. However,the answer on pg 416 says something else. Can you please explain it clearly.....
 
Felix Li
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On P.356, the example works because it assumes the bean Person exists in the attribute. (See the bolded lines)

In summary,
If bean exists in scoped attributes, then you can have any one of the following for your useBean tag:
a) type only
b) class only
c) type AND class

if bean does not exists in scoped attributes, then you can have:
a) class only
b) type AND class

class is a MUST!

cheers
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maan,

Going back to your snippet of code from the _jspService it's almost as if you'd looked in the Tomcat Catalina work directory

Essentially with just a Type the container will try and retrieve an attribute of the specified name, and cast it to the type specified. Using normal servlet classes (as they are cleaner) the JSP becomes the equivalent of:

foo.Employee person = (foo.Employee) request.getAttribute("person");

If it can't find it, or the attribute it finds can't be cast that way, then it will throw an error!

With both a class and type specified i.e.

<jsp:useBean id="test" class="foo.Class" type="foo.Type" scope="request">
....

Your JSP becomes an equivalent of:

foo.Type test = (foo.Type) request.getAttribute("test");
if (test==null){
test = new foo.Class();
}

So again it tries first to find the attribute and cast it to the specified type. Failing that it creates a new instance of class!

To add to the confusion I believe in early prints of the 2nd edition the answer to example 1 in the Be the container question (pg 420) is incorrect! i.e. HFSJ says it shouldn't work, except that it should as it can find the bean in the request scope and cast it to the specified type!

Does this help, or just confuse things further.

Rufus.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot felix....

Rufus...really good explanation....thanks a lot....but if i stuck up on this i will get back to you.....
[ October 18, 2008: Message edited by: Maan Shenoy ]
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic