File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFSJ Question in JSTL 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 "HFSJ Question in JSTL" Watch "HFSJ Question in JSTL" New topic
Author

HFSJ Question in JSTL

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
This is one of the questions in HFSj (pg492)

A JavaBean Person has a property called address.The value of this is property is another Java Bean Address with the following string properties : street1, street2, city, stateCode and zipeCode.
A Controller servlet creates a session-scoped attribute called customer that is an instance of the Person bean.

Which Jsp Code structures will set the city property of the customer attribute to the city request parameter?

A. ${sessionScope.customer.address.city = param.city}

B. <c:set target="${sessionScope.customer.address)" property="city" value="${param.city"} />

C. <c:set scope=session var="${customer.address}" property="city" value="${param.city"} />

D.<c:set target="${sessionScope.customer.address}" property="city"> ${param.city"} </c:set>

Ans. B,D

I am getting confused with the terms Property and Attribute of JavaBean.
I did figure out the right answer (only one) by the method of elimination rather then understanding


So a Java Bean Person has a property address would mean it has a getAddress() and setAddress() methods.
The value of this property Java Bean Address with the string properties..........................


I am losing it from here .. Can some one explain this programatically and how we arrived the answer?

Thanks!


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ashok Kurakula
Greenhorn

Joined: May 16, 2011
Posts: 15

I am getting confused with the terms Property and Attribute of JavaBean.


Just burn this in: beans have properties while tags have attributes

<c:set target="${sessionScope.customer.address)" property="city" value="${param.city"} />

In the above statement, “property” (the one in blue color) is an attribute of ‘set’ tag and “city” (one in red color) is a property of address bean.

What does the above tag do? It sets the city property of the address bean.

But in order to achieve this, you need to supply (at least) three things to the tag.
1. The bean (whose property you want to change)
2. The property of the bean that you are going to change
3. New value for that property

And the tag holds these values with the help of its attributes.

The “target” attribute: to hold the bean which you are targeting.
The “property” attribute: to hold the property of a bean which you are going to set.
The “value” attribute: to hold the value for the property.

The naming convention is such that the attribute’s name tells you what the attribute is for.


So a Java Bean Person has a property address would mean it has a getAddress() and setAddress() methods.


Yes, of course. The Person class may look like this


And the Address class may look like this


The above is equivalent to have this below code in a scriptelet (<% ... %>)

In fact, all this is taken care by the tag's supporting class. what we just need to do is to supply the tag with the necessary things. The chapter on Custom Tag Development will help you in getting a better idea on tag's supporting class and how the tag sends its attributes to its supporting class. I don't want to add more to your confusion. So I'll leave this here.


OCPJP 6, OCE Java EE 6 JSP and Servlets Developer, OCE Java EE 6 EJB Developer...
Ashok Kurakula
Greenhorn

Joined: May 16, 2011
Posts: 15

and how we arrived the answer?
A. ${sessionScope.customer.address.city = param.city}

B. <c:set target="${sessionScope.customer.address)" property="city" value="${param.city"} />

C. <c:set scope=session var="${customer.address}" property="city" value="${param.city"} />

D.<c:set target="${sessionScope.customer.address}" property="city"> ${param.city"} </c:set>


I think, now with the above explanation you can say, pretty confidently, that B and D are correct options. D is just another way of B, wherein the value is specified inside the body.

A is incorrect because Expression Language (just like java expressions <%= %>) is used for getting things and writing them directly to the page. You don't do assignments in EL. So, whenever you see an assignment operator inside EL, you can say that it is wrong without any hesitation.

C is incorrect. There are three things wrong about this.
1) We don't use "var" attribute when we want to set properties of a bean. "var" is used for setting attributes in one of the four scopes(page, request, session and application)
2) For "var" attribute, you just supply the name of the attribute you want to access(i.e. a String literal or an expression that results in a String), NOT the actual object
3) "property" attribute is only used in combination with the "target" attribute. Using it in the "var" version is just an incorrect combination.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks for all the time you have spent on the explanation . it helped !

I do have a question , regarding "customer"

So Customer is an instance of a Person.

${sessionScope.customer.address}

Which address is being mentioned here , is the Java Bean Address -OR- Person property address?
And if it's the Person property address , how is it accessing "city" which is a property of the Java Bean Address?

I hope I framed the question correctly. I guess I am missing the link between the Java Bean Person and the Java Bean Address .
Ashok Kurakula
Greenhorn

Joined: May 16, 2011
Posts: 15

Thanks for all the time you have spent on the explanation . it helped !

Happy to hear it
I do have a question , regarding "customer"
So Customer is an instance of a Person.

Yes, customer is an instance of a Person.
${sessionScope.customer.address}

Which address is being mentioned here , is the Java Bean Address -OR- Person property address?
And if it's the Person property address , how is it accessing "city" which is a property of the Java Bean Address?

In fact, it is both. Address is a property of the Person bean and it is a JavaBean by itself. There is nothing wrong in that. Beans can have properties which are beans.
After all, a JavaBean is nothing but a POJO(Plain Old Java Object) with some stricter naming conventions. Consider the below example.

If you run the above code, you will get the output as "This is a String object inside TestB which is inside TestA".
TestA has TestB and TestB has a String, right? And you are accessing TestB's String using the object of TestA.

In the same way, you can access the "city" property(String) of "Address" bean(Object) which is inturn a property of the "Person" bean(another Object). Because as I said earlier, a JavaBean is like any other Object.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Now i get it !
Thanks a ton !
Ashok Kurakula
Greenhorn

Joined: May 16, 2011
Posts: 15

you are welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFSJ Question in JSTL
 
Similar Threads
Clarification for answer on JSTL
jsp:useBean doubt
SCWCD prep Doubt
JSTL (c:set)
jsp question