aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes <jsp:UseBean> can also CREATE a bean! Or can it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "<jsp:UseBean> can also CREATE a bean! Or can it?" Watch "<jsp:UseBean> can also CREATE a bean! Or can it?" New topic
Author

<jsp:UseBean> can also CREATE a bean! Or can it?

Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
Stupid beginner's question . It says in the Bert's book that

if the <jsp:useBean> can not find an attribute object named "person" it can make one!



After running/deploying the above in Tomcat, I get "The value for the useBean class attribute foo.Person is invalid."

I thought if the Person class does not exist, then it will be created. But I think I understood it wrong . Please clear up my understanding here...

however, if the class Person ALREADY exists in package foo, then everything feels fine.


Working on my SCWCD so I can be a J2EE consultant earning millions of dollars and showing everyone I can
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1731
    
  25

Hi Swagato,

It will create the instance of the class, but not the class itself.

Regards,
Frits
Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
Frits Walraven wrote:Hi Swagato,

It will create the instance of the class, but not the class itself.

Regards,
Frits


So you mean it will create an object! Am I right?

If that is so, then why am I getting the error?
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

Swagato,

You still need to write a bean class and place it in the proper place so that the container can find it. In the example you cite, you need to write a Person class in package Foo and place it in WEB-INF/classes.

What the container will do is create an instance of the class if it cannot find one in page scope named person and set the name property to 'Swagato'.


kf
SCJP 5.0 (preparing for SCWCD)
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1731
    
  25

yes, you are right it will create an object (that is instantiate) according to its blueprint (that is class definition).

You got this error because the class was not there.

Regards,
Frits
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Keith Flo wrote:
What the container will do is create an instance of the class if it cannot find one in page scope named person and set the name property to 'Swagato'.


true. jsp:useBean instantiate the bean(creation of Object) , only if the bean is not found in any of the scope , otherwise it locate the existing bean insteadof creating new Object
Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
Seetharaman Venkatasamy wrote:
Keith Flo wrote:
What the container will do is create an instance of the class if it cannot find one in page scope named person and set the name property to 'Swagato'.


true. jsp:useBean instantiate the bean(creation of Object) , only if the bean is not found in any of the scope , otherwise it locate the existing bean insteadof creating new Object


If that is the case,
then if you run my code without Person class
then why does it not create the object (person) file. Or do I need to write the Person bean class for the compiler/container to create the object?

I thought I can safely not write the bean class entirely and the container/compier can create it for me if it does not exist?!?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Swagato Bhatta wrote:[quote=Seetharaman Venkatasamy
Or do I need to write the Person bean class for the compiler/container to create the object?
I thought I can safely not write the bean class entirely and the container/compier can create it for me if it does not exist?!?


Oops! so, you dont have the class called Person under foo package?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1731
    
  25

The container can only create a bean for you if it knows its definition (that is class definition)

Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
Seetharaman Venkatasamy wrote:
Swagato Bhatta wrote:[quote=Seetharaman Venkatasamy
Or do I need to write the Person bean class for the compiler/container to create the object?
I thought I can safely not write the bean class entirely and the container/compier can create it for me if it does not exist?!?


Oops! so, you dont have the class called Person under foo package?

No because I thought under foo package compiler/container will create the Person class/bean for me automatically. Now it seems like it does not and I have to explicitly write the bean.
Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
Frits Walraven wrote:The container can only create a bean for you if it knows its definition (that is class definition)



And to inform the container the class definition, I have to explicitly write the whole bean class (in this case Person.java). So what exactly is implictly CREATED for me in such cases? Just compiling and making the object file?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Swagato Bhatta wrote:So what exactly is implictly done for me? Just compiling and making the object file?


Not object 'file' . create an *objcet* in memory(heap). object and class(template) both are different
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1731
    
  25

What the container does is creating an object for you (so you don't have to create it with new Person()).
The container also creates a servlet (from the .jsp file) for you (which extends GenericServlet and implements the HttpJspPage interface)

have a look at:
my summary page 41
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <jsp:UseBean> can also CREATE a bean! Or can it?