Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How type attribute in useBean be applied  RSS feed

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know <jsp:useBean > type attribute is to create a reference type to the bean created. But, why we ever want to do this? e.g.

<jsp:useBean id="person" class="com.example.Student" type="com.example.Person" scope="session"/>
<jsp:getProperty name="person" property="name" />

In the first line, whether the reference type is .Person or .Student (Student is-a Person) should not affect the jsp:getProperty line where a bean is searched from all 4 scopes with the name "person". Is that correct?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to set either type or class.
If you set the type attribute, the JSP will expect an object of that type to be present in the given scope when run.
If you set the class attribute (and the class specified is a proper bean with a zero argument constructor), the JSP will first look for an instance of that class in the given scope. If it can't find it, it will instanciate on itself and bind it to the given scope.

Section JSP.5.1 in the JSP Spec (link in my signature) will tell you everything you need to know about useBean. It's worth reading.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I understand all these, but why we want <jsp:useBean> to behave this way? I mean the class attribute seems enough for our needs. Whether the bean already existed, we can just put in class attribute (without bothering type).
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alec Lee:
Yes, I understand all these, but why we want <jsp:useBean> to behave this way? I mean the class attribute seems enough for our needs. Whether the bean already existed, we can just put in class attribute (without bothering type).




The type attribute expects an object of the given type to be present at the time the JSP is run. If it is not, a runtime exception will be thrown.
You would need to instanciate the object in either a servlet or another JSP and bind it to the given scope BEFORE the JSP is run.

The class attribute, on the other hand, will actually instanciate it for you if an instance is not already present when the JSP is run.

When using class, you would need to specify a concrete class that can be instanciated by the container (zero argument constructor).
With type on the other hand you could specify an abstract class or even an interface. The object that it points to can be any subclass or implementation as long as it IS an instance of that type.

[ October 01, 2005: Message edited by: Ben Souther ]
[ October 03, 2005: Message edited by: Ben Souther ]
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You CAN specify both in the same tag. And it is legal to do so.
The easiest way I can think of to explain it is to make up some generated java code.

<jsp:useBean id="person" class="com.example.Student" type="com.example.Person" scope="session"/>

would generate something along these lines as java code:



The JSP Reference explains it pretty well.

Why you would do this? The same reason you write code like:
List myList = new ArrayList().
ie you want the TYPE of the object to be an abstract class/interface, but you specify a default concrete type for instantiating if it is not already there.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Evans:
You CAN specify both in the same tag. And it is legal to do so.
The easiest way I can think of to explain it is to make up some generated java code.

<jsp:useBean id="person" class="com.example.Student" type="com.example.Person" scope="session"/>

would generate something along these lines as java code:



The JSP Reference explains it pretty well.

Why you would do this? The same reason you write code like:
List myList = new ArrayList().
ie you want the TYPE of the object to be an abstract class/interface, but you specify a default concrete type for instantiating if it is not already there.



I stand corrected.
The actual text in the spec (misread by myself) is:


At least one of type and class must be present, and it is not valid to provide both class and beanName. If type and class are present, class must be assignable to type (in the Java platform sense). For it not to be assignable is a translation-time error.




Thank you, Stefen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!