JSP <jsp:useBean ... /> tag: Difference between 'class' and 'type'

S

Sean Aitken

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
 
J

John C. Bollinger

Sean said:
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.
 
S

Sean Aitken

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-
--snip--
 
A

Alex Hunsley

Sean said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top