• Post Reply Bookmark Topic Watch Topic
  • New Topic

what's wrong with 'AbstractClass', if object is there

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Referring to page 354 of HFS&J book.

In case of <jsr:useBean> action:

"If class is used (with or without type) the class must NOT be abstract, and must have a public no-arg constructor."

My question is, if object is present in the scope then why it must NOT be an abstract class. Because if object is present then no need to instantiate class so doesn't make any difference whether the class is abstract or not...

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And mine is : What if the object is not present ?
The whole meaning of the class attribute is to instanciate this class if it does not exist.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the JSP compiler to know whether the object will be in scope or not?
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
How is the JSP compiler to know whether the object will be in scope or not?


On the same page,

<jsp:useBean id="person" type="foo.Person" scope="page" />

If the 'person' attribute exist in page scope then it works perfectly.
If not then gives java.lang.InstantiationException

How compiler knows here???

 
geeta lalchandani
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:useBean id="person" type="foo.Person" scope="page" />

If the 'person' attribute exist in page scope then it works perfectly.
If not then gives java.lang.InstantiationException


if the object is not presesnt, it actually tries to create a new instance.N hence the exception.

How at the compile time would you know if the object exists. and hence the class must be concrete.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:


How compiler knows here???



It doesn't. That's a run-time exception.
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall refer you to the JSP reference: useBean
There are two attributes for specifying the java class of a bean: type, and class. They are meant for different, complementary purposes.

type can be abstract
class may not be, as it is used to instantiate the bean if it does not exist in the required scope.

In effect you could do something like this:


Which would create a declaration something like



So the bean is of type "List" - which means it can accept ArrayList, Vector, LinkedList, MyCustomList...
If the bean is not in scope, it is created as of class "ArrayList"

Does that explain the difference between "type" and "class" for you?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good example. Furthermore, be aware that whatever class is referenced by the class attribute cannot be abstract as the generated code that creates the new instance if on does not already exist will fail to compile.

If you know that the scoped variable will already exist, don't use the class attribute; just use type.
[ May 22, 2006: Message edited by: Bear Bibeault ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it completely.

Whether the object exist or not, code always has *object creation* code:

So AbstractArrayList is an abstract class, compiler recognise and give a compilation error.

-----------------------
See this case:

<jsp:useBean type="AbstractClass" id="something" scope"request" />

Generated code would be:



Now in this case also, compiler should recognise that it's an abstract class so I should throw an error.

 
Mandar Max
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:

<jsp:useBean type="AbstractClass" id="something" scope"request" />

Generated code would be:




In the above example, if you don't specify the class attribute, the object must be already present in the given scope. In order to generate the code as you have shown above, you have to specify the class attribute, which should be a non-abstract class, otherwise JSP compilation will fail.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mandar.
But still have some doubts.


In the above example, if you don't specify the class attribute, the object must be already present in the given scope.


Which object must be already present in the given scope???


In order to generate the code as you have shown above, you have to specify the class attribute, which should be a non-abstract class, otherwise JSP compilation will fail.


What code will be generated if I don't give class attribute???

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He's talking about the bean. If you're certain that the bean is somewhere in a scope, you could use the "type" attribute.
If not, you should use the "class" attribute, if you want the compiler to create a new instance of the bean for you.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
He's talking about the bean. If you're certain that the bean is somewhere in a scope, you could use the "type" attribute.
If not, you should use the "class" attribute, if you want the compiler to create a new instance of the bean for you.


Which bean???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The one you get with <jsp:useBean>. Remember ? You're trying to retrieve a bean using <jsp:useBean>.

You've set it somewhere, probably in a servlet with request.setAttribute, or session.setAttribute, before displaying the jsp.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
The one you get with <jsp:useBean>. Remember ? You're trying to retrieve a bean using <jsp:useBean>.

You've set it somewhere, probably in a servlet with request.setAttribute, or session.setAttribute, before displaying the jsp.


But thats an interface. How would I set it into scope.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No no. What you set in the scope is an implementation of a given interface.
Here is a simple example:


In a servlet, you may have:

And then, in JSp, you can get the bean back:
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
No no. What you set in the scope is an implementation of a given interface.
Here is a simple example:


In a servlet, you may have:

And then, in JSp, you can get the bean back:


Okay, if I have one more class say MyClass1 implements MyInterface, and also in session, then which one container would find??

Thanks.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am damm confused now.









I feel, something is wrong somewhere (either in HFS&J or ...).

Well the basic concept, what I concluded from the above is:

If you are specifying only type attribute (not class attribute), then container just try to search from scope (doesn't try to create).

Please comments.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!