JSP <jsp:useBean ... /> tag: Difference between 'class' and 'type'  
Author Message
Sean Aitken





PostPosted: 2004-6-3 23:11:00 Top

java-programmer, JSP <jsp:useBean ... /> tag: Difference between 'class' and 'type' Hello,

I'm trying to gain a better understanding of the included tag libraries
with JSP. The <jsp:useBean tag seems farily straightforward, in that it
sets a page variable from an object in any scope, with a variety of options.

I see examples listed that interchange the 'class' and 'type' attribute,
but to me it's not real clear as to their distinction. Can anyone
explain their difference?

One thing I have noticed is that if I don't yet have a variable declared
in the given scope, using the 'type' attribute causes an exception to be
thrown, whereas the 'class' attribute does not. It seems that when I
specify "class", it actually creates a new instance of the specified
variable / bean.

Thanks!!
-Sean
 
John C. Bollinger





PostPosted: 2004-6-4 3:12:00 Top

java-programmer >> JSP <jsp:useBean ... /> tag: Difference between 'class' and 'type' Sean Aitken wrote:

> Hello,
>
> I'm trying to gain a better understanding of the included tag libraries
> with JSP. The <jsp:useBean tag seems farily straightforward, in that it
> sets a page variable from an object in any scope, with a variety of
> options.
>
> I see examples listed that interchange the 'class' and 'type' attribute,
> but to me it's not real clear as to their distinction. Can anyone
> explain their difference?

The "class" attribute specifies the actual class of the bean instance.
The "type" attribute specifies the Java type by which the instance is
handled, which may be a superclass of the bean's class or an interface
implemented by it.

> One thing I have noticed is that if I don't yet have a variable declared
> in the given scope, using the 'type' attribute causes an exception to be
> thrown, whereas the 'class' attribute does not. It seems that when I
> specify "class", it actually creates a new instance of the specified
> variable / bean.

If there is not already an attribute of the specified id in the
specified scope then useBean attempts to create one. It can only do
that if it knows what class to instantiate. If there _is_ already a
matching attribute then useBean attempts to use it; it will be cast to
the type specified by "type" if that is provided, or to the type
specified by "class" otherwise.

You would benefit from reading the specifications for these behaviors.
The JSP spec is available from Sun as a free download, and it describes
all of this.

--
John Bollinger
email***@***.com
 
Sean Aitken





PostPosted: 2004-6-4 3:25:00 Top

java-programmer >> JSP <jsp:useBean ... /> tag: Difference between 'class' and 'type' Perfect!

That explanation makes sense. I actually spent about 20 minutes reading
through the applicable portions of the JSP spec PDF before making this
post. They seemed dodgy on the issue. Your explanation made perfect
sense. Guess I need to read better next time. :0)

Thanks!
-s-

John C. Bollinger wrote:

> Sean Aitken wrote:
>
>> Hello,
>>
>> I'm trying to gain a better understanding of the included tag
>> libraries with JSP. The <jsp:useBean tag seems farily
>> straightforward, in that it sets a page variable from an object in any
--snip--
 
 
Alex Hunsley





PostPosted: 2004-10-21 19:36:00 Top

java-programmer >> JSP <jsp:useBean ... /> tag: Difference between 'class' and 'type' Sean Aitken wrote:
> Perfect!
>
> That explanation makes sense. I actually spent about 20 minutes reading
> through the applicable portions of the JSP spec PDF before making this
> post. They seemed dodgy on the issue. Your explanation made perfect
> sense. Guess I need to read better next time. :0)
>
> Thanks!
> -s-

Hi Sean
Could you plase bottom-post, rather than top posting? It makes threads
much more easy to follow.
More info here:

http://www.cs.tut.fi/~jkorpela/usenet/brox.html

alex