This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2447
96
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swagato,

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

Regards,
Frits
 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2447
96
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2447
96
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container can only create a bean for you if it knows its definition (that is class definition)

 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2447
96
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic