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