• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Make and use bean in same jsp

 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Why doesn't the below work? The container refuses to generate a servlet from it, complaining about the "Duplicate bean name".

<jsp:useBean id="piggy" class="pancakes.RealBean" />
<jsp:useBean id="piggy" type="pancakes.AbstractBean" />

AbstractBean is abstract. RealBean extends AbstractBean.

In my understanding, the first useBean action should instantiate a RealBean and store it in the piggy attribute, which it does if I remove the second line. The second useBean action, in my humble understanding, should assign the RealBean object stored in the piggy attribute to the local variable piggy, which is an AbstractBean reference.

Apparently my understanding is wrong. Can anyone explain why it won't work?

Thanks!
John
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
useBean's id must be unique within the translation unit.
 
Surajsingh Thakur
Ranch Hand
Posts: 124
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will give a Translation Error..

 
Vinay Belagavi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare a diffrent id for the usebean. They should have unique id values.
 
Lakshmi Narayanan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason behind is

If a JSP is properly compiled then these declarations goes in to _jspService as we all know this.

eg., NAMEs are different
<jsp:useBean id="piggy" class="pancakes.RealBean" />
<jsp:useBean id="mypiggy" type="pancakes.AbstractBean" />

then two variables are declared inside this service method
pancakes.RealBean piggy = null
if( piggy is null ) then create object

NAMES are same
then as JAVA standarad you cannot have same names anywhere
piggy will be declared twice and that is the reason translation error.

i hope i am clear
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason behind this is that the JSP specification states, JSP.5.1 <jsp:useBean>, The id Attribute :

The id=�name� attribute/value tuple in a jsp:useBean action has special meaning to a JSP container, at page translation time and at client request processing time. In particular: the name must be unique within the translation unit, and identifies the particular element in which it appears to the JSP container and page. Duplicate id�s found in the same translation unit shall result in a fatal translation error.

 
Lakshmi Narayanan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,

I have also given why it will fail practically.

Please correct me too.

Thanks
Lakshmi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic