Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

useBean doubt

 
Ranch Hand
Posts: 110
Firefox Browser MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am reading the book Head First Servlets and Jsp. I came across the following useBean question in "scripless jsp" chapter.

jsp looks like (only relavant code is given)

foo.Person is abstract class
1) what happens if the servlet code looks like this

given answer: fails at request time
2) what happens if the servlet code looks like

given answer: sucess and prints "name is evan"
i don't understand the 2nd answer. The person attribute has request scope. As useBean didn't mention any scope page scope is considered as default.
Then 2nd scenario also should fail. Am i right?
In the 1st case object is of type Person. It won't match with the type attribute(foo.Employee) of useBean tag. Suppose if scopes match will this work.


Thanks,
Venkat




 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venkat.

Then 2nd scenario also should fail. Am i right?

Right. It should fail.

In the 1st case object is of type Person. It won't match with the type attribute(foo.Employee) of useBean tag. Suppose if scopes match will this work.

Yes, if scopes match this will work.
 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chinmaya.

As per my understanding the use of type field is that bean is of given type and we can call functions declared in the given type class

Please see the scwcd faq link http://faq.javaranch.com/java/JspParameterStandardActions

The bean "per" is of type bean.Person. How can we print a attribute of bean.Employee. Then what is the use of type attribute in jsp:usebean.


Thanks,
Venkat


 
Chinmaya Chowdary
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venkat.

Then what is the use of type attribute in jsp:usebean.

'type' is used if the corresponding bean object already available and it was set in particular scope. If our jsp invokes , container first sees is the bean is available or not? Like if it finds, it will use that, if not it will not create another bean object and body will not be executed(if present).
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean
 
Chinmaya Chowdary
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe. I am sorry, instead of this
in Tomcat, it is
 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused about inheritance effect on type field. I want to know which getter/setter methods are called if we put a bean object in request scope and mention only type (super class of actual bean object) in <jsp:useBean> tag

Suppose see this example. I took the code from http://faq.javaranch.com/java/JspParameterStandardActions and put it different way.


In the servlet code i create a bean object like this

jsp code looks like

Here Class attribute is not mentioned and person bean is available in request scope so container won't execute the usebean body.
Container looks for the person bean object like this

now can we get the property empID of bean.Employee class. will this work

As person is of type bean.Person can we access the properties of bean.Employee class in jsp?





 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has been extensively discussed in the past.
Here is one.
 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe for providing the link. That is useful to clarify my doubts and bring peace to my mind.

 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic