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

Discussion in 'Java' started by Sean Aitken, Jun 3, 2004.

  1. Sean Aitken

    Sean Aitken Guest

    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
    Sean Aitken, Jun 3, 2004
    #1
    1. Advertising

  2. 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
    John C. Bollinger, Jun 3, 2004
    #2
    1. Advertising

  3. Sean Aitken

    Sean Aitken Guest

    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--
    Sean Aitken, Jun 3, 2004
    #3
  4. Sean Aitken

    Alex Hunsley Guest

    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
    Alex Hunsley, Oct 21, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anan H. Samiti
    Replies:
    1
    Views:
    7,624
    kaeli
    Sep 27, 2004
  2. Ulf Meinhardt
    Replies:
    1
    Views:
    4,599
    Tor Iver Wilhelmsen
    Oct 14, 2004
  3. iliad
    Replies:
    0
    Views:
    2,272
    iliad
    Sep 9, 2005
  4. Replies:
    3
    Views:
    1,353
  5. zalek
    Replies:
    1
    Views:
    362
Loading...

Share This Page