Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

jsp:useBean and polymorphism

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In this example :



Suppose we have this code in the servlet (Person is an abstract class and Employee extends Person) :



The book where I took this says the following :

The “person” attribute is stored at request
scope, so the <jsp:useBean > tag won’t work since it specifies only a
type. The Container KNOWS that if you have only a type specified,
there MUST be an existing bean attribute of that name and scope.



I still don't understand how this could not work. The type is "foo.Employee" and the attribute already exists in the request, so when we use <jsp:useBean>, there is no need to specify a class. Did I misunderstood something ?
 
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I recall correctly, useBean with class will attempt to create the bean if it already doesn't exist. This precludes the possibility of polymorphism. What I believe you want is type which just acts likes a cast. So why do you think it is not working?
 
Bear Bibeault
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, useBean is pretty archaic. Why are you not using the JSTL and EL?
 
Dave Jovi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:If I recall correctly, useBean with class will attempt to create the bean if it already doesn't exist. This precludes the possibility of polymorphism. What I believe you want is type which just acts likes a cast. So why do you think it is not working?



In the book I had to figure out if this is working or not. For me, there is no reason that this will not work. But the book said that it will not work and they gave the explanation (in quotation). Maybe it's an error ?

Bear Bibeault wrote:By the way, useBean is pretty archaic. Why are you not using the JSTL and EL?



I'm reading Head First Servlets and JSP. They talked about this concept as an introduction but I will move to JSTL and EL in the next chapters.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Jovi wrote:I'm reading Head First Servlets and JSP. They talked about this concept as an introduction but I will move to JSTL and EL in the next chapters.



I wrote JSPs starting about 15 years ago and I don't know the answer to your question because I never used that kind of code. Even then, jsp:useBean was obsolete technology. Are you sure you're using a recent version of the book?

Even if you are, I'd just skip over that chapter and go on to the more current forms of JSP. Unless you're being tested on that knowledge, of course.
 
Dave Jovi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's an old book indeed (2008). But it's the only book I found easy to understand and well explained because I was completely lost on where to start learning J2EE.

I didn't know that it was obsolete so thank you for the advice, I will not spend much time on it.
 
Saloon Keeper
Posts: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Jovi wrote:
Suppose we have this code in the servlet (Person is an abstract class and Employee extends Person) :

The book where I took this says the following :

The “person” attribute is stored at request
scope, so the <jsp:useBean > tag won’t work since it specifies only a
type. The Container KNOWS that if you have only a type specified,
there MUST be an existing bean attribute of that name and scope.



I still don't understand how this could not work.



If I remember correctly, this line will not have any effect since markup enclosed in jsp:useBean are used to initialize new instances of the bean.In your example, the type attribute indicates that the instance already exists, so a new instance would not be created and no initialization would be performed.


If you want to change a property of an existing bean, you would do something like this:
To create and new bean and initialize the property, use the class attribute (this will only work if the bean is not already created in the servlet and added to the request context):


 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!