aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes useBean doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "useBean doubt" Watch "useBean doubt" New topic
Author

useBean doubt

Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

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





SCJP 5.0, SCWCD 5, preparing for SCDJWS
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
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

Joined: Apr 16, 2008
Posts: 110

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

Joined: Apr 21, 2008
Posts: 432
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).
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You mean


[My Blog]
All roads lead to JavaRanch
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Christophe. I am sorry, instead of this
in Tomcat, it is
Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

This has been extensively discussed in the past.
Here is one.
Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

Thanks Christophe for providing the link. That is useful to clarify my doubts and bring peace to my mind.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: useBean doubt