File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JavaBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JavaBean "type" attribute" Watch "JavaBean "type" attribute" New topic
Author

JavaBean "type" attribute

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I don't understand what the "type" attribute of a JavaBean does exactly.
Can someone give me an example of when I would use this?
Thanks,
Drew
Maha Annadurai
Ranch Hand

Joined: Oct 27, 2002
Posts: 87
The attribute 'type' is used as java type of the variable whose name is same as the value of 'id' attribute.
Type id_val = (Type)pageContext.getAttribute(id_val,PageContext.***_SCOPE);
For example,
<jsp:useBean id="address" type="AddressBean" scope="session" />
will generate a java code as
AddressBean address = (AddressBean)(pageContext.getAttribute("address",PageContext.SESSION_SCOPE);
Here AddressBean is the base class/ interface. There can be more than one concrete subclasses such as BussinessAddressBean, HomeAddressBean which extend the base class. So in MVC (Model View Control) model, depending upon the business logic decision made, the controller servler may instantiate any one of the 2 types (BussinessAddressBean/HomeAddressBean) and make session.setAttrbute("address",sessionBean) and forward to the UI jsp.
All the Jsp cares about is the base class /(interface) type AddressBean. Since we are typecasting to the super class, the code will work well without any ClassCastException.
In future , if you add one more type of AddressBean and set in session also, the JSP will work without any problems.
Regards,
Maha Anna
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Thanks, that is helpful, but it's still a little confusing.
Let me ask this: What do I need to know about the 'type' attribute with respect to the SCWCD exam?
Drew
Maha Annadurai
Ranch Hand

Joined: Oct 27, 2002
Posts: 87
FOr the SCWCD exam, we need to know that
1. 'type' attribute can be used in 3 combinations for <jsp:useBean /> tag. They are,
a. class and type
b. type
c. beanName and type
2. When used with 'class' attribute, it must be same class, a super class, or an interface implemnted by 'class'. If the particular object with 'id' name is not found in the 'scope', then a new object of 'class' is instantiated and typecasted to 'type' and stored in 'id' attribute.
3. When used alone, the bean with given id should exist in given scope. Otherwise InstantiationException will be thrown.
4. When used with beanName, a bean is instantiated from a serialized file.
Please refer to this link.
http://java.sun.com/products/jsp/tags/12/syntaxref1217.html#8865
type="package.class"
If the bean already exists in the scope, gives the bean a data type other than the class from which it was instantiated. The value of type must be a superclass of class or an interface implemented by class.
If you use type without class or beanName, no bean is instantiated. The package and class name are case sensitive.
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
Regards
Maha Anna
[ December 02, 2002: Message edited by: Maha Annadurai ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaBean "type" attribute