Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean question

 
Rekha Polisetty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether this is valid bean tag.
<jsp:useBean id = "MyBean" type = "java.lang.String" scope="page" />
I don't think this a valid, there should be either "class" or "beanName".
Thanks in advance.
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rekha,
You are right. You need either of them.

vasu
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not right. Either 'class' or 'type' will do.
In this case, when we just have 'type', what will happen is, the jsp container will try to locate an object of 'type' with the 'id' in the given 'scope'.
If it is found, then a scripting var is defined with a same name of 'id'. If not found, then an InstantiationException will be thrown. This makes sense because, if we give just a 'type' there can be more than one classes defined in the web app of this 'type'. How can a decision be made which class to instantiate?
On the other hand, if we give a 'class' attribute, then the container will attempt to create an object of this class , in case the object is not found in the given scope and given id.
For example :
<%@ page import='AddressBean' %>
<jsp:useBean id='address' type="AddressBean" scope='session'/>
For this code in a jsp, the container generated java file will have this code:


From Jsp Spec: Section 4.1
JavaServer Pages 1.2 Specification
The actions performed in a jsp:useBean action are:
1. An attempt to locate an object based on the attribute values id and scope. The
inspection is done synchronized per scope namespace to avoid non-deterministic
behavior.
1. A scripting language variable of the specified type (if given) or class (if type
is not given) is defined with the given id in the current lexical scope of the
scripting language.
2. If the object is found, the variable�s value is initialized with a reference to the
<jsp:useBean> 71
located object, cast to the specified type. If the cast fails, a java.lang.ClassCastException
shall occur. This completes the processing of this jsp:useBean action.
3. If the jsp:useBean element had a non-empty body it is ignored. This completes
the processing of this jsp:useBean action.
4. If the object is not found in the specified scope and neither class nor beanName
are given
, a java.lang.InstantiationException shall occur. This completes the
processing of this jsp:useBean action.

So the possible right combinations are:
1. class
2. type
3. class and type
4. beanName and type
and 'id' is a must attribute.
-Maha Anna
 
Mikalai Zaikin
Ranch Hand
Posts: 3371
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maha,
great explanation !
thanks.
 
Rekha Polisetty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maha
[ November 23, 2002: Message edited by: Rekha Polisetty ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic