• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:getProperty action

 
sri lakshmi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from Deshmuk book chapter 14. review question number 8
<html>
<body>
<%@ page import='AddressBean' %>
<% { %>
<jsp:useBean id="address" class="AddressBean" scope="session"/>
<% } %>
// 1
</body>
</html>
which of the following can be placed at line 1 above to print the value of the street property
a. <jsp:getProperty name="address" propertyt="street"/>
b.<% out.print(address.getStreet()); %>
c. <%= address.getStreet() %>
d.<%= ((AddressBean)session.getAttribute("address")).getStreet() %>
e. none of the above.
the answer given was a and d.
I have no problem with d.
How does (a) work out? The default scope is "page" right? The scope of address is declared as "session". But how can a getProperty access address bean from the session scope? Am I missing something here?
 
Louise Haydu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line in answer a:
<jsp:getProperty name="address"
property="street"/>
is actually translated using the following method of PageContext (see page 222 in your book):
Object findAttribute(String name)
This method search for the named attribute in page, request, session (if valid) and application scope(s) in this order and returns the associated value.
The translator generated code to look for the address attribute and then accessed the street property.
I had the same question and only discovered this answer after creating and running a jsp to test this out. When I looked a the java code generated for the jsp page, I found it was using the "findAttribute" method, which is shown on page 222.
[ June 19, 2003: Message edited by: Louise Haydu ]
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct answer to the question is (d) only, not (a) and (d), right?
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check out the errata of the deshmuk book at http://www.manning.com/deshmuk It says the correct answers for this question are a and d.
 
Louise Haydu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, both a and d are correct.
You can verify this by installing TomCat and running a test using the sample code (supplied on the CD that comes with the book) as the source for AddressBean.
Since a jsp generates a servlet, you can find the actual servlet code in a *.java file in the work directory of TomCat. If you examine that file, you will see how the getProperty action is translated into Java code.
When I looked at the first scriptlet on page 267 of the book, I thought that the code would translate into java as:
out.print(address.getStreet());
In this case, you would get a compile error because "address" is out of scope.
However this is not the case... the translator uses the findAttribute method (page 222) instead and the code compiles and runs successfully.
Hope this helps!

[ June 19, 2003: Message edited by: Louise Haydu ]
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above code, the street property is not set. so the "d" option prints null . But the option "a" does not print anything. That means, findAttribute() method is returning an empty value even when the street property is not set?
 
Geoffrey Lo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Originally posted by nikita sri:
That means, findAttribute() method is returning an empty value even when the street property is not set?

I think the street property might be set to its default values.
<jsp:useBean id="address" class="AddressBean" scope="session"/>
The above line of code looks for the bean in the session scope. If not found, it will create a new instance and put it in the session.
So if we are creating a new instance then instance variables are set to its default values.
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default value for a String instance variable will be null. so the "d" option prints null. How come findAttribute() prints nothing? Can you please explain in detail?
 
Mi Bo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default value for String is null, but specific bean could have it's own default values.When you instance is created its values are set to certain values, not necessary null
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the AddressBean from the Hanumant book where the default value for the String is null.
Option "d" printed null but option "a" printed nothing.
 
Jian Su
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I thought answer b) & c) are both correct.
Can anybody let me know what's wrong with them?
Thanks.
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jian Su:
But I thought answer b) & c) are both correct.
Can anybody let me know what's wrong with them?
Thanks.

Notice that the jsp:useBean tag is embedded within Java curly braces (in JSP syntax: <% { %> BLOCK <% } %> . This sets up an intermediate "scope" similar to the code below:

Notice that // line 1 is outside of the code block in which the 'address' JSP variable is declared. Therefore, line 1 does not have access to the 'address' variable. This is why options (b) and (c) are incorrect.
HTH,
Bryan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic