4

What is the difference between class attribute and beanName attribute of jsp:useBean tag.

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
Raj
  • 2,463
  • 10
  • 36
  • 52

1 Answers1

12

Just read the <jsp:useBean> documentation (page 35). Here's an extract of relevance:

  • class="package.class" type="package.class"

    Instantiates a bean from the class named in class and assigns the bean the data type you specify in type. The value of type can be the same as class, a superclass of class, or an interface implemented by class.

    The class you specify in class must not be abstract and must have a public, no-argument constructor. The package and class names you use with both class and type are case sensitive.

  • beanName="{package.class | <%= expression %>}" type="package.class"

    Instantiates a bean from a class, a serialized template, or an expression that evaluates to a class or serialized template. When you use beanName, the bean is instantiated by the java.beans.Beans.instantiate method. The Beans.instantiate method checks whether the package and class you specify represents a class or a serialized template. If they represent a serialized template, Beans.instantiate reads the serialized form (which has a name like package.class.ser) using a class loader.

    The value of type can be the same as beanName, a superclass of beanName, or an interface implemented by beanName. The package and class names you use with both beanName and type are case sensitive.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    @Andres: JSP has been deprecated. Fortunately I already extracted the relevant part into the answer. – BalusC Apr 24 '14 at 12:20
  • 1
    @BalusC - how do you mean JSP has been deprecated? I think what is meant is just the link should be to Oracle and not Sun which is now 'deprecated'. – user3763130 Jun 21 '14 at 15:23
  • 2
    @user3763130: JSP was end of 2009 succeeded by Facelets and Oracle apparently didn't bother to maintain old JSP documentation for a lifetime. If you can find the right Oracle link containing exactly the extracted part in my answer, be my guest to fix the broken link in my answer. – BalusC Jun 21 '14 at 17:36
  • FYI example ... You want to use a bean that is stored in `com/enthu/GUI.ser` file. Which of following statements correctly defines the tag that accesses the bean? the answer is `` .ser file is nothing but a regular file in which any Java object is stored in a serialized format. This is usually done using ObjectOutputStream.writeObject(obj); and by convention the extension of such a file is .ser. – shareef Dec 18 '14 at 18:13
  • Cont..First the container looks for a .ser file under the directory as specified in the beanName attribute. If the file is available, it is used to instantiate the bean using `java.bean.Beans.intantiate(...)` method. Otherwise, the beanName is assumed to be a class name and if that class has a public no args constructor, an object of that class is instantiated. Otherwise an exception is thrown. – shareef Dec 18 '14 at 18:13
  • check : http://www-it.fmi.uni-sofia.bg/courses/WDB/references/JSP%20syntax/syntaxref08.html – Radu Toader Oct 14 '15 at 12:49