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 use Bean query 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 "use Bean query" Watch "use Bean query" New topic
Author

use Bean query

yeshwanth reddy
Ranch Hand

Joined: Mar 10, 2006
Posts: 38
Hi all,
Can u tell me why beanName is used in a useBean. Is anything mentioned about it hfsj?


<jsp:useBean id="softDrink" beanName="Drink.ser" type="com.baboon.interface.Drink" />

Regards,
Yashwanth
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
HFSJ didnt say anything about beanName ...... i think it is equivalent to 'class'.


God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Frederic Esnault
Ranch Hand

Joined: Feb 13, 2006
Posts: 284
Hi.

No, beanName is not equivalent to class. beanName specify the name of a bean (a REAL javaBean) as you would use in a instantiate() method of Java Beans. This attribute MUST go along with the type attribute, defining the object type. So, the only way to use beanName is like this :

<jsp:useBean id="foo" beanName="name" type="org.phoenix.bar" />

If you see beanName without type, this IS an error.

See specs for further explanations.


SCJP 5 - SCWCD 1.4 - SCBCD 1.3 - Certification study documents/resources: http://esnault.frederic.free.fr/certification
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
what is the difference between these two syntax?

<jsp:useBean id="foo" class="org.phoenix.bar" />

<jsp:useBean id="foo" beanName="name" type="org.phoenix.bar" />

can u plz point out a situation where we can use beanName?

Thanks...
yeshwanth reddy
Ranch Hand

Joined: Mar 10, 2006
Posts: 38
Hi all,

"instantiate() method of Java Beans"- what does this mean?

Can u please be more clear on what beanName is used for?Does it specify the name of the java bean (class name?) and the type here is used to specify what type the java bean is?


Regards,
Yashwanth
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
BeanName specify the name of a bean (a REAL javaBean).It means that beanName should follow java naming rule.
Look at this question,

Which of the following are correct JSP useBean declarations? [Check all correct answers]

1. <jsp:useBean id="softDrink" beanName="Drink.ser" type="com.baboon.interface.Drink" />
2. <jsp:useBean id="softDrink" beanName="Drink.ser" class="com.baboon.interface.Drink" />
3. <jsp:useBean id="card" class="com.baboon.cardgame.Card" />
4. <jsp:useBean beanName="instructor" class="com.baboon.users.Instructor" />
5. <jsp:useBean id="iDevice" type="com.baboon.types.Device" />

Answers given 1,3,5

In answer 1,beanName did'nt follow java naming rule.So how come it is correct?

my concusion is,

The beanName attribute can refer to a class name or the name of a serialized file containing the bean.We can either use beanName or class , it means the same but in addition beanName can also refer to a serialized bean file name.The beanName and class attributes cannot be used together.

Request anyone to clarify...


Thanks.
Frederic Esnault
Ranch Hand

Joined: Feb 13, 2006
Posts: 284
Sorry i forgot about the serialized file. Yes it works with a serialized bean.
And beanName may ONLY be used WITH the type attribute.

From the JSP spec (JSP 5) :
The attribute beanName specifies the name of a Bean, as specified in the
JavaBeans specification. It is used as an argument to the instantiate method in the java.beans.Beans class. It must be of the form a.b.c, which may be either a class,or the name of a resource of the form a/b/c.ser that will be resolved in the current ClassLoader. If this is not true, a request-time exception, as indicated in the semantics of the instantiate method will be raised. The value of this attribute can be a request-time attribute expression.


From Beans API, the instantiate method description :
instantiate

public static Object instantiate(ClassLoader cls,
String beanName)
throws IOException, ClassNotFoundException

Instantiate a JavaBean.

Parameters:
cls - the class-loader from which we should create the bean. If this is null, then the system class-loader is used.
beanName - the name of the bean within the class-loader. For example "sun.beanbox.foobah"

Throws:
ClassNotFoundException - if the class of a serialized object could not be found.
IOException - if an I/O error occurs.
[ March 14, 2006: Message edited by: Frederic Esnault ]
Kiran Joshi
Ranch Hand

Joined: Sep 04, 2005
Posts: 54
I think one more important difference is beanName can accept the jsp expression also,whereas class cannot.
Ref: Hanumant Deshmukh.
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Yes...the beanName attribute can accept a request-time attribute expression
as a value.

Thanks.
 
 
subject: use Bean query